If I create a new branch and try to push it, I am told that I have to explicitly say what the name of the upstream branch needs to be.
> git checkout -b feature/long-branch-name-I-dont-want-to-have-to-type-out
Switched to a new branch 'feature/long-branch-name-I-dont-want-to-have-to-type-out'
> git push
fatal: The current branch feature/long-branch-name-I-dont-want-to-have-to-type-out has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/long-branch-name-I-dont-want-to-have-to-type-out
Is there some way to do that without having to type out the name of the upstream branch? I virtually always want it to be the same name on the server as it is locally.
Is there any way to do something like git push --set-upstream <current_branch_name>
, independent of what the current branch name happens to be?
The easiest way to set the upstream branch is to use the “git push” command with the “-u” option for upstream branch. Alternatively, you can use the “–set-upstream” option that is equivalent to the “-u” option. As an example, let's say that you created a branch named “branch” using the checkout command.
In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.
Git set-upstream. The git set-upstream allows you to set the default remote branch for your current local branch. By default, every pull command sets the master as your default remote branch.
Set Upstream If you don't want to push anything, you can also do it using git-branch command. A local branch can track a remote branch using git-branch with long option --set-upstream-to=<upstream> or short option -u <upstream> . The command sets up branchname 's tracking information.
Configure git config
$ git config --global push.default current
Now, after checkout to a branch, you should use simply git push
$ git checkout -b new-branch
$ git push # similar to git push -u origin new-branch
If you want to set upstream for the future then use --set-upstream
(-u) flag:
$ git push -u origin HEAD
N.B. HEAD and local current branch normally stay in the same state.
[Edit] sajib khan's first answer, setting push.default
to current
will enable pushing, but does not actually set the upstream. This means that after a future git fetch
, your Git won't report ahead/behind counts, and your Git won't know the upstream to use for git rebase
or git merge
(or git pull
either though I advise avoiding git pull
).
You can use [edit, as in the second part of his answer]:
git push -u origin HEAD
If needed, this creates the branch on the other Git, so that your Git acquires the origin/
variant. Then in any case it sets that (maybe new) remote-tracking branch you have as your branch's upstream. But until origin/feature/long-branch-name-I-dont-want-to-have-to-type-out
actually exists, you can't set it as the upstream.1
1Actually, you can, you just can't use git branch --set-upstream
to do it. And, you don't want to type it in again anyway. To do it "manually" you would need:
git config \
branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.remote origin
git config \
branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.merge \
feature/long-branch-name-I-dont-want-to-have-to-type-out
which means typing it out three times (!), or writing yourself a script.
There's some handy git commands that can help out here.
git rev-parse --abbrev-ref HEAD
-> returns the current branch name
git branch -u <remote>/<branch>
-> sets the current branch to track <remote>/<branch>
I do this sort of thing a lot and leverage aliases to help me out.
alias gph='git push origin $(git rev-parse --abbrev-ref HEAD)'
alias gbuh='git branch -u origin/$(git rev-parse --abbrev-ref HEAD)'
Reference:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With