Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git push: set target for branch

Tags:

git

I'd like to push my current branch (hp1) with

git push 

and not

git push origin hp1:team/hp1 

The remote branch already exists.

My local branches:

develop master * hp1 

git remote show origin tells me:

Remote branches:   develop  tracked   master   tracked   team/h2  tracked   team/hp1 tracked   team/n1  tracked Local branches configured for 'git pull':   develop  merges with remote develop   master   merges with remote master   hp1 merges with remote team/hp1 Local refs configured for 'git push':   master   pushes to master   (up to date) 

I already tried

git branch --set-upstream hp1 origin/team/hp1 

and

git branch --set-upstream hp1 refs/remotes/origin/team/hp1 

but both don't work.

My colleague has a local branch called as the remote branch (team/hp1) and the code above works for him. He gets at the end an additional

Local refs configured for 'git push':   develop  pushes to develop  (up to date)   master   pushes to master   (up to date)   team/hp1 pushes to team/hp1 (up to date) 

So maybe you can tell me what's wrong and how to fix it.

EDIT my config:

[core]     repositoryformatversion = 0     filemode = true     bare = false     logallrefupdates = true [remote "origin"]     fetch = +refs/heads/*:refs/remotes/origin/*     url = ***@***:***.git [branch "master"]     remote = origin     merge = refs/heads/master [branch "hp1"]     remote = origin     merge = refs/heads/team/hp1 
like image 775
m1schka Avatar asked Nov 17 '11 16:11

m1schka


People also ask

How do I push data to a specific branch?

In some cases, you may want to push your changes to another branch on the remote repository. 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.

How do I push changes to a remote branch?

In case you are using the Tower Git client, pushing to a remote is very easy: simply drag your current HEAD branch in the sidebar and drop it onto the desired remote branch - or click the "Push" button in the toolbar.

What is git push Setupstream?

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.

Does git push push automatically branch?

If you run the simple command git push , Git will by default choose two more parameters for you: the remote repository to push to and the branch to push. By default, Git chooses origin for the remote and your current branch as the branch to push.


1 Answers

First of all, when pushing for the first time, do:

git push -u origin hp1:team/hp1 

About -u option:

-u
--set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch..merge in git-config(1).

Note from the manual that, this in itself will not determine what happens when you do git push the next time. When you do git pull while in this branch, it will fetch it from the upstream that you have set. But when you push, it will push to a matching branch ( in this case hp1 and not team/hp1)

For that to work, you have to set push.default config value to upstream. Once you set that, when you push from a branch ( just do git push), it will push to the upstream as mentioned by branch.<name>.merge

So do:

git config push.default upstream 

About push.default:

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

nothing - do not push anything.

matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.

upstream - push the current branch to its upstream branch.

tracking - deprecated synonym for upstream.

current - push the current branch to a branch of the same name.

like image 168
manojlds Avatar answered Oct 25 '22 05:10

manojlds