gpt4 book ai didi

git - git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别

转载 作者:IT王子 更新时间:2023-10-29 01:16:40 30 4
gpt4 key购买 nike

有人知道这两个切换和跟踪远程分支的命令之间的区别吗?

git checkout -b branch origin/branch
git checkout --track origin/branch

我想两者都会跟踪远程分支,这样我就可以将我的更改推送到原始分支,对吧?

有什么实际差异吗?

最佳答案

这两个命令具有相同的效果(thanks to Robert Siemer’s answer for pointing it out)。

当使用名为不同的本地分支时,实际的区别就来了:

  • git checkout -b mybranch origin/abranch将创建 mybranch和跟踪origin/abranch
  • git checkout --track origin/abranch只会创建' abranch ',而不是名称不同的分支。

(即 as commented by Sebastian Graf ,如果本地分支已经存在。
如果是这样,您将需要 git checkout -B abranch origin/abranch )


注意:对于 Git 2.23(2019 年第三季度),将使用 new command git switch :

git switch -c <branch> --track <remote>/<branch>

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we'll use that one for the purposes of disambiguation, even if the <branch> isn't unique across all remotes.
Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if <branch> is ambiguous but exists on the 'origin' remote.

在这里,'-c ' 是新的 ' -b '.


首先,一些背景知识:Tracking 意味着本地分支将其上游设置为远程分支:

# git config branch.<branch-name>.remote origin
# git config branch.<branch-name>.merge refs/heads/branch

git checkout -b branch origin/branch将:

  • 创建/重置 branchorigin/branch 引用的点.
  • 创建分支branch (使用 git branch )并跟踪远程跟踪分支 origin/branch .

When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the branch.<name>.remote and branch.<name>.merge configuration entries) so that git pull will appropriately merge from the remote-tracking branch.
This behavior may be changed via the global branch.autosetupmerge configuration flag. That setting can be overridden by using the --track and --no-track options, and changed later using git branch --set-upstream-to.


git checkout --track origin/branch将与 git branch --set-upstream-to 相同):

 # or, since 1.7.0
git branch --set-upstream upstream/branch branch
# or, since 1.8.0 (October 2012)
git branch --set-upstream-to upstream/branch branch
# the short version remains the same:
git branch -u upstream/branch branch

它还会为“branch”设置上游'.

(注意:git1.8.0 将弃用 git branch --set-upstream 并将其替换为 git branch -u|--set-upstream-to :参见 git1.8.0-rc1 announce )


将上游分支注册为本地分支将:

  • 告诉git 显示git status中两个分支之间的关系和 git branch -v
  • 指导 git pull 没有参数在 checkout 新分支时从上游 pull

有关更多信息,请参阅“How do you make an existing git branch track a remote branch?”。

关于git - git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10002239/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com