Configure a local branch for push to specific branch
Asked Answered
C

2

15

Sorry if this question has been asked already.

Am cloning from a repo named "git_lab" which has a branch named "test" When cloning i use "-b myname_test" to create a local branch named "myname_test" and local clone is named "myname_git_lab"

When i do "git pull" it automatically fetches and merges changes from "test" to "myname_test", but for git push, i need to specify the repo and branch name.

$>git remote show git_lab

Local branch configured for 'git pull': myname_test merges with remote test

Is there a way where i can configure "local branch configured for 'git push'" so that i dont need to specify the branch and repo name?

Crosslet answered 5/11, 2010 at 18:57 Comment(0)
K
27

There are two things you can do here.

  • Set push.default to tracking, so that it will push all branches to the remote branches they track, not the ones they have the same name as, then configure your branch with appropriate tracking information. (e.g. set branch.master.remote to origin and branch.master.merge to refs/heads/foo.)

  • Push manually. git push origin master:foo will push your local master branch to the branch foo on the remote origin.

However, I'd suggest that what you really want to do is just make the branch names the same.

(You can set config parameters either with git config, e.g. git config push.default tracking, or by directly editing the .git/config file.)

Koons answered 5/11, 2010 at 20:48 Comment(7)
"git config push.default tracking" worked....thanks a ton, just curious to know....why do we need to specify this config setting, i thought when we clone using -b option everything is set. (branch.master.remote and branch.master.merge was set after the clone automatically)Crosslet
@Senthil: Those settings are indeed made automatically, but the default behavior of push is to push matching branches, not tracking branches. The settings therefore normally only affect fetch/pull, not push. See push.default in the git config manpage.Koons
what about git branch --set-upstream?Inenarrable
@adymitruk: That is indeed a smoother way than manually setting the pair of config parameters, in the parentheses in the first bullet. It's a fairly recent feature, so I forgot about it. The thrust of the question, however, was making it work with push - and the OP appeared to have already configured the tracking branch - so the important part was push.default.Koons
git config push.default tracking seems to have worked for me. +1.Pinnate
--set-upstream doesn't help. That just sets the local tracking branch that's the upstream for your local branch, but does not change the defaults for what push uses for names.Jus
@CurtSampson: Read the first bullet of my answer, and my comment responding to Adam. It does change what branch push pushes to, if you've set push.default to tracking like I said. And it's misleading to say it only sets an upstream local tracking branch; it sets a tracking branch which corresponds to a remote branch, and pull does fetches from that remote branch, and push if configured like this will push to that remote branch.Koons
I
0
git checkout --track origin/branchname

Alternatively, you can edit the config file in the .git folder.

Inenarrable answered 5/11, 2010 at 19:14 Comment(3)
Thanks but setting the remote url is to specify the remote repository to push, how can i specify the remote-branch to push to? Since my local branch name and remote branch name are differentCrosslet
tracking should help you out. Edited the response.Inenarrable
Tracking does not affect pushing by default. And if the branch already exists, git checkout --track is definitely not the way to set up tracking information.Koons

© 2022 - 2024 — McMap. All rights reserved.