Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set upstream branch to the same name as the branch

Tags:

git

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?

like image 631
PortMan Avatar asked Jun 08 '17 21:06

PortMan


People also ask

How do you set upstream on a branch?

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.

How push local branch to remote with same name?

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.

What is git branch -- set upstream to?

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.

How do I set up Forstream without pushing?

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.


3 Answers

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.

like image 58
Sajib Khan Avatar answered Sep 22 '22 08:09

Sajib Khan


[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.

like image 41
torek Avatar answered Sep 21 '22 08:09

torek


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:

  • OG answers on getting the name of the current branch newer post and OG post
like image 21
Keego Avatar answered Sep 22 '22 08:09

Keego