简体   繁体   中英

Git - How to track a remote branch that you created from a local branch?

Here's the scenario.

I have a series of commits in branch my_new_branch

I want to push this branch up to the remote repo as a different name and track it.

If I do:

git push origin my_new_branch:different_name

it pushes the branch up fine.

But if I stay in my_new_branch and make another commit, if I just do

git push

it says 'Everything up to date'

Further, if I look at .git/config, I can see it hasn't set up a tracking branch.

Of course I can just do

git push origin my_new_branch:different_name 

again, but how do I get it to track?

If I do

git checkout -b my_new_branch origin/different_name

it complains that the branch name already exists.

Thanks!

As you mention, the question " How do you make an existing git branch track a remote branch? " indicate the possibility to set the upstream branch (the one to where you push) for a local branch (since Git1.7.0).

git branch --set-upstream my_new_branch origin/different_name

which replaces:

git config branch.my_new_branch.remote origin
git config branch.my_new_branch.merge refs/heads/different_name

By default, git push uses a special refspec ' : '

The special refspec : (or +: to allow non-fast-forward updates) directs git to push "matching" branches :
for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side.
This is the default operation mode if no explicit refspec is found (that is neither on the command line nor in any Push line of the corresponding remotes file).

Hence your troubles with " git push " (no arguments).

If you remember to use -u when you push, git will set this up for you automatically.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM