$ git checkout -b testbranch --track master
Branch testbranch set up to track local branch master.
Switched to a new branch 'testbranch'
这些分支的用例是什么?$ git checkout -b testbranch --track master
Branch testbranch set up to track local branch master.
Switched to a new branch 'testbranch'
这些分支的用例是什么?将一个本地分支设置为跟踪另一个本地分支通常并不实用,但我可以想到一些情况下它可能是有用的:
You may have some aliases or scripts that do stuff with upstream branches (e.g., via the @{u}
shorthand; see git help revisions
). If so, configuring a local branch to track another local branch would allow you to test your aliases or scripts without affecting the remote repository or creating a temporary dummy repository.
Suppose you are working on a new local branch named foo
off of your local master
branch (which tracks origin/master
) but you aren't ready to publish foo
to origin
yet. Or maybe you don't ever want to push to origin
-- perhaps the commits added a bunch of obnoxious printf()
calls you sprinkled in to help with debugging. Anyway, whenever master
is updated, you want to rebase foo
onto master
. Normally you would just type git rebase master
, but if you're like me you may find yourself frequently typing just git rebase
out of habit. If you do, you'll get this error message:
$ git rebase
There is no tracking information for the current branch.
Please specify which branch you want to rebase against.
See git-rebase(1) for details
git rebase <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> foo
You may be tempted to do this:
git branch --set-upstream-to=origin/master foo
but that can have devastating effects if you're in the habit of typing git push
every few commits (you'll end up pushing your work-in-progress commits on your local foo
branch to origin
's master
branch, assuming you have push.default
set to upstream
like I do).
Instead, you can configure the local foo
branch to track your local master
branch. Now your habit of typing git rebase
does what you want, and an absent-minded git push
will only mess up your local master
branch, not origin
's master
branch.
But it's usually not worthwhile to configure a local branch to track another local branch just for git rebase
. I think it's easier to live with remembering to type git rebase master
instead of git rebase
. If I forget, I get an error message that jogs my memory and I try again. No big deal.
[branch "testbranch"]
remote = .
merge = refs/heads/master
你可以使用$git pull
从主分支拉取代码,使用
$git push . testbranch:master
将代码推送到主分支。
或者你可以在.gitconfig中添加以下配置后,直接输入$git push
即可:
[push]
default = upstream
merge
(或 rebase
)。 - Eli Bendersky