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?

Senthil A Kumar
  • 8,034
  • 13
  • 41
  • 53

2 Answers2

26

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

Cascabel
  • 422,485
  • 65
  • 357
  • 307
  • 2
    "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) – Senthil A Kumar Nov 05 '10 at 21:10
  • 1
    @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](http://www.kernel.org/pub/software/scm/git/docs/git-config.html). – Cascabel Nov 05 '10 at 21:18
  • 2
    what about git branch --set-upstream? – Adam Dymitruk Nov 08 '10 at 22:21
  • 1
    @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`. – Cascabel Nov 08 '10 at 22:29
  • 1
    `git config push.default tracking` seems to have worked for me. +1. – brice Sep 13 '11 at 14:32
  • --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. – cjs Oct 21 '11 at 04:44
  • 1
    @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. – Cascabel Oct 21 '11 at 04:58
0
git checkout --track origin/branchname

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

meager
  • 209,754
  • 38
  • 307
  • 315
Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • 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 different – Senthil A Kumar Nov 05 '10 at 19:27
  • tracking should help you out. Edited the response. – Adam Dymitruk Nov 05 '10 at 19:37
  • 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. – Cascabel Nov 05 '10 at 20:49