I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.
For example:
$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name
Now if someone updates remote_branch_name, I can:
$ git pull
And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:
$ git push
Instead, I have to:
% git push origin newb:remote_branch_name
Seems a little silly. If git-pull
uses git-config branch.newb.merge
to determine where to pull from, why couldn't git-push
have a similar config option? Is there a nice shortcut for this or should I just continue the long way?
Here's the process that has worked for me.
Now your new repo will be ‘origin’ and the original repo is ‘upstream’. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).
git push origin master
Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.
Rebase is a smart merge. Then push to master again and you’ll see the selected feature branch as master on the new repo.
Optional:
When you do the initial push add the -u parameter:
Subsequent pushes will go where you want.
EDIT:
As per the comment, that only sets up pull.
should do it.
Sure. Just set your
push.default
toupstream
to push branches to their upstreams (which is the same thatpull
will pull from, defined bybranch.newb.merge
), rather than pushing branches to ones matching in name (which is the default setting forpush.default
,matching
).Note that this used to be called
tracking
notupstream
before Git 1.7.4.2, so if you're using an older version of Git, usetracking
instead. Thepush.default
option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.The command by Adam is now deprecated. You can use:
to set the upstream branch of
my_local_branch
toorigin/my_remote_branch
.