本地分支,本地跟踪分支,远程分支和远程跟踪分支之间有什么区别?

问题描述 投票:132回答:3

我刚刚开始使用Git,我在不同的分支之间真的很困惑。任何人都可以帮我弄清楚以下分支类型是什么?

  • 当地分公司
  • 本地跟踪分支
  • 远程分支
  • 远程跟踪分支

他们之间有什么区别?他们如何相互合作?

我想,快速演示代码将非常有用。

git version-control git-branch git-remote
3个回答
93
投票

本地分支是只有您(本地用户)才能看到的分支。它仅存在于您的本地计算机上。

git branch myNewBranch        # Create local branch named "myNewBranch"

远程分支是远程位置的分支(在大多数情况下是origin)。您可以将新创建​​的本地分支myNewBranch推送到origin。现在其他用户可以跟踪它。

git push -u origin myNewBranch   # Pushes your newly created local branch "myNewBranch"
                                 # to the remote "origin".
                                 # So now a new branch named "myNewBranch" is
                                 # created on the remote machine named "origin"

远程跟踪分支是远程分支的本地副本。当使用上面的命令将myNewBranch推送到origin时,会在您的机器上创建名为origin/myNewBranch的远程跟踪分支。这个远程跟踪分支跟踪myNewBranch上的远程分支origin。您可以使用git fetchgit pull更新远程跟踪分支以与远程分支同步。

git pull origin myNewBranch      # Pulls new commits from branch "myNewBranch" 
                                 # on remote "origin" into remote tracking
                                 # branch on your machine "origin/myNewBranch".
                                 # Here "origin/myNewBranch" is your copy of
                                 # "myNewBranch" on "origin"

本地跟踪分支是跟踪另一个分支的本地分支。这样您就可以向/从另一个分支推送/拉出提交。在大多数情况下,本地跟踪分支跟踪远程跟踪分支。当您使用带有origin选项的git push command将局部分支推送到-u时(如上所示),您可以设置本地分支myNewBranch来跟踪远程跟踪分支origin/myNewBranch。这需要使用git pushgit pull而不指定上游来推或拉。

git checkout myNewBranch      # Switch to myNewBranch
git pull                      # Updates remote tracking branch "origin/myNewBranch"
                              # to be in sync with the remote branch "myNewBranch"
                              # on "origin".
                              # Pulls these new commits from "origin/myNewBranch"
                              # to local branch "myNewBranch which you just switched to.

164
投票

这是一个很长的答案。

Remotes:

如果您正在协作使用Git,则可能需要将提交与其他计算机或位置同步。每个机器或位置在Git的术语中称为远程,并且每个机器或位置可以具有一个或多个分支。大多数情况下,你只会有一个名为origin。要列出所有遥控器,请运行git remote

$ git remote
bitbucket
origin

通过运行git remote -v,您可以查看这些远程名称的快捷方式位置:

$ git remote -v
bitbucket [email protected]:flimm/example.git (fetch)
bitbucket [email protected]:flimm/example.git (push)
origin [email protected]:Flimm/example.git (fetch)
origin [email protected]:Flimm/example.git (push)

每个远程都有一个git/refs/remotes/下的目录:

$ ls -F .git/refs/remotes/
bitbucket/ origin/

Branches on your machine:

TLDR:在您的本地计算机上,您有三种类型的分支:本地非跟踪分支,本地跟踪分支和远程跟踪分支。在远程计算机上,您只有一种类型的分支。

1.地方分支机构

您可以通过运行git branch查看计算机上所有本地分支的列表:

$ git branch
master
new-feature

每个本地分支都有一个.git/refs/heads/下的文件:

$ ls -F .git/refs/heads/
master new-feature

您的计算机上有两种类型的本地分支:非跟踪本地分支和跟踪本地分支。

1.1 Non-tracking local branches

非跟踪本地分支不与任何其他分支相关联。您可以通过运行git branch <branchname>来创建一个。

1.2. Tracking local branches

跟踪本地分支与另一个分支相关联,通常是远程跟踪分支。您可以通过运行git branch --track <branchname> [<start-point>]来创建一个。

您可以使用git branch -vv查看哪个本地分支机构正在跟踪分支:

$ git branch -vv
master      b31f87c85 [origin/master] Example commit message
new-feature b760e04ed Another example commit message

从该命令的输出中,您可以看到本地分支master正在跟踪远程跟踪分支origin/master,而本地分支new-feature没有跟踪任何内容。

查看哪些分支跟踪分支的另一种方法是查看.git/config

跟踪本地分支是有用的。它们允许您运行git pullgit push,而无需指定要使用的上游分支。如果分支未设置为跟踪另一个分支,则会出现如下错误:

$ git checkout new-feature
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream new-feature <remote>/<branch>

2.远程跟踪分支机构(仍在您的机器上)

您可以通过运行git branch -r查看计算机上所有远程跟踪分支的列表:

$ git branch -r
bitbucket/master
origin/master
origin/new-branch

每个远程跟踪分支都有一个.git/refs/<remote>/下的文件:

$ tree -F .git/refs/remotes/
.git/refs/remotes/
├── bitbucket/
│   └── master
└── origin/
    ├── master
    └── new-branch

将远程跟踪分支视为远程计算机包含的本地缓存。您可以使用git fetch更新远程跟踪分支,git pull在幕后使用。

即使远程跟踪分支的所有数据都本地存储在您的计算机上(如缓存),它仍然永远不会被称为本地分支。 (至少,我不会这么称呼!)它只是被称为远程跟踪分支。

Branches on a remote machine:

您可以通过运行git remote show <remote>来查看所有远程分支(即远程计算机上的分支):

$ git remote show origin
* remote origin
  Fetch URL: [email protected]:Flimm/example.git
  Push  URL: [email protected]:Flimm/example.git
  HEAD branch: master
  Remote branches:
    io-socket-ip            new (next fetch will store in remotes/origin)
    master                  tracked
    new-branch              tracked
  Local ref configured for 'git pull':
    master     merges with remote master
    new-branch merges with remote new-branch
  Local ref configured for 'git push':
    master     pushes to master     (up to date)
    new-branch pushes to new-branch (fast-forwardable)

git remote命令通过网络查询远程计算机的分支。它不会更新本地计算机上的远程跟踪分支,为此使用git fetchgit pull

从输出中,您可以通过查看标题“远程分支”(忽略标记为“陈旧”的行)查看远程计算机上存在的所有分支。

如果您可以登录到远程计算机并在文件系统中找到存储库,则可以查看refs/heads/下的所有分支。

Cheat sheet:

  • 要安全地删除本地分支,无论是跟踪还是非跟踪: git branch -d <branchname>
  • 要强制删除本地分支,无论是跟踪还是非跟踪: git branch -D <branchname>
  • 要删除远程跟踪分支: git branch -rd <remote>/<branchname>
  • 要创建新的本地非跟踪分支: git branch <branchname> [<start-point>]
  • 要创建一个新的本地跟踪分支:(请注意,如果指定了<start-point>并且是origin/foobar之类的远程跟踪分支,则自动包含--track标志) git branch --track <branchname> [<start-point] 例: git branch --track hello-kitty origin/hello-kitty
  • 要删除远程计算机上的分支: git push --delete <remote> <branchname>
  • 要删除所有过时的远程跟踪分支,即远程计算机上相应分支不再存在的位置: git remote prune <remote>

您可能已经注意到在某些命令中,您使用<remote>/<branch>和其他命令<remote> <branch>。示例:git branch origin/hello-kittygit push --delete origin hello-kitty

它看似随意,但有一种简单的方法可以记住何时使用斜杠以及何时使用空格。当你使用斜杠时,你指的是你自己机器上的远程跟踪分支,而当你使用空间时,你实际上是通过网络处理远程机器上的分支。


11
投票

当地分行:

机器上的一个分支,您可以在其中工作并添加提交。您可以使用git branch列出这些分支。

本地分支(跟踪):

配置为对应于远程分支的普通本地分支。这有点像git pullgit push的能力,而无需指定存储库和分支名称。当您的分支位于遥控器的前方或后方时,跟踪还会导致git status通知您。

远程分支:

只是远程存储库上的一个分支 - 通常在诸如GitHub等服务器上。

远程跟踪分支:

远程分支的本地副本。永远不应该编辑该分支。其目的是跟踪远程分支的当前状态。可以使用git branch -r查看远程跟踪分支,通常看起来像origin/master(repo name后跟斜杠后跟分支名称)。运行git fetch将更新远程跟踪分支以反映相应远程分支的状态。

git branch -avv是我个人最喜欢的,可以快速浏览我的机器上哪些分支机构,远程机架上的哪些分支机构以及每台分支机构的最新提交。 -a部分指定应显示所有分支(远程和本地)。最后的v代表详细(它显示了最后一次提交哈希和消息)。感谢@Flimm指出第二个v添加了关于哪个本地分支正在跟踪哪个遥控器的信息。

© www.soinside.com 2019 - 2024. All rights reserved.