git 本地分支、本地跟踪分支、远程分支和远程跟踪分支有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16408300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:05:53  来源:igfitidea点击:

What are the differences between local branch, local tracking branch, remote branch and remote tracking branch?

gitversion-controlgit-branchgit-remote

提问by eded

I just started using Git and I got really confused between different branches. Can anyone help me to figure out what the following branch types are?

我刚开始使用 Git,我在不同的分支之间感到非常困惑。谁能帮我弄清楚以下分支类型是什么?

  • local branches
  • local tracking branches
  • remote branches
  • remote tracking branches
  • 当地分行
  • 当地跟踪分支机构
  • 远程分支
  • 远程跟踪分支

What is the difference between them? And how do they work with each other?

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

A quick demo code will be really helpful I guess.

我想快速演示代码将非常有帮助。

采纳答案by SNce

A local branchis a branch that only you (the local user) can see. It exists only on your local machine.

一个地方分支的一个分支,只有你(本地用户)可以看到。它仅存在于您的本地机器上。

git branch myNewBranch        # Create local branch named "myNewBranch"

A remote branchis a branch on a remote location (in most cases origin). You can push the newly created local branch myNewBranchto origin. Now other users can track it.

远程分支是上远程位置处的分支(在大多数情况下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"

A remote tracking branchis a local copy of a remote branch. When myNewBranchis pushed to originusing the command above, a remote tracking branch named origin/myNewBranchis created on your machine. This remote tracking branch tracks the remote branch myNewBranchon origin. You can update your remote tracking branchto be in sync with the remote branchusing git fetchor git pull.

一个远程跟踪分支是远程分支的本地副本。当使用上面的命令myNewBranch推送到时originorigin/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"

A local tracking branchis a local branchthat is tracking another branch. This is so that you can push/pull commits to/from the other branch. Local tracking branches in most cases track a remote tracking branch. When you push a local branch to originusing the git push commandwith a -uoption (as shown above), you set up the local branch myNewBranchto track the remote tracking branch origin/myNewBranch. This is needed to use git pushand git pullwithout specifying an upstream to push to or pull from.

一个地方跟踪分支本地分支,其跟踪的另一个分支。这样您就可以向/从另一个分支推送/拉取提交。大多数情况下,本地跟踪分支会跟踪远程跟踪分支。当您将本地分支推送到origin使用git push commandwith-u选项(如上所示)时,您将本地分支设置myNewBranch为跟踪远程跟踪分支origin/myNewBranch。这需要使用git push并且git 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.

回答by Flimm

Here's the long answer.

这是长答案。

Remotes:

遥控器:

If you're using Git collaboratively, you'll probably need to sync your commits with other machines or locations. Each machine or location is called a remote, in Git's terminology, and each one may have one or more branches. Most often, you'll just have one, named origin. To list all the remotes, run git remote:

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

$ git remote
bitbucket
origin

You can see which locations these remote names are shortcuts for, by running git remote -v:

您可以通过运行以下命令查看这些远程名称是哪些位置的快捷方式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)

Each remote has a directory under git/refs/remotes/:

每个遥控器下都有一个目录git/refs/remotes/

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

Branches on your machine:

您机器上的分支:

TLDR: on your local machine, you've got three types of branches: local non-tracking branches, local tracking branches, and remote-tracking branches. On a remote machine, you've just got one type of branch.

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

1. Local branches

1. 当地分支机构

You can view a list of all the local branches on your machine by running git branch:

您可以通过运行git branch以下命令查看机器上所有本地分支的列表:

$ git branch
master
new-feature

Each local branch has a file under .git/refs/heads/:

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

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

There are two types of local branches on your machine: non-tracking local branches, and tracking local branches.

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

1.1 Non-tracking local branches

1.1 非跟踪本地分支机构

Non-tracking local branches are not associated with any other branch. You create one by running git branch <branchname>.

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

1.2. Tracking local branches

1.2. 追踪当地分支机构

Tracking local branches are associated with another branch, usually a remote-tracking branch. You create one by running git branch --track <branchname> [<start-point>].

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

You can view which one of your local branches are tracking branches using git branch -vv:

您可以使用以下命令查看您的本地分支机构之一正在跟踪分支机构git branch -vv

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

From this command's output, you can see that the local branch masteris tracking the remote-tracking branch origin/master, and the local branch new-featureis not tracking anything.

从这个命令的输出中可以看出,本地分支master正在跟踪远程跟踪分支origin/master,本地分支new-feature没有跟踪任何东西。

Another way to see which branches are tracking branches is by having a look at .git/config.

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

Tracking local branches are useful. They allow you to run git pulland git push, without specifying which upstream branch to use. If the branch is not set up to track another branch, you'll get an error like this one:

跟踪本地分支很有用。它们允许您运行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. Remote-tracking branches (still on your machine)

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

You can view a list of all the remote-tracking branches on your machine by running git branch -r:

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

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

Each remote-tracking branch has a file under .git/refs/<remote>/:

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

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

Think of your remote-tracking branches as your local cache for what the remote machines contain. You can update your remote-tracking branches using git fetch, which git pulluses behind the scenes.

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

Even though all the data for a remote-tracking branch is stored locally on your machine (like a cache), it's still never called a local branch. (At least, I wouldn't call it that!) It's just called a remote-tracking branch.

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

Branches on a remote machine:

远程机器上的分支:

You can view all the remote branches (that is, the branches on the remote machine), by running git remote show <remote>:

您可以通过运行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)

This git remotecommand queries the remote machine over the network about its branches. It does not update the remote-tracking branches on your local machine, use git fetchor git pullfor that.

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

From the output, you can see all the branches that exist on the remote machine by looking under the heading "Remote branches" (ignore lines marked as "stale").

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

If you could log in to the remote machine and find the repository in the filesystem, you could have a look at all its branches under refs/heads/.

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

Cheat sheet:

备忘单:

  • To delete a local branch, whether tracking or non-tracking, safely:

    git branch -d <branchname>
    
  • To delete a local branch, whether tracking or non-tracking, forcefully:

    git branch -D <branchname>
    
  • To delete a remote-tracking branch:

    git branch -rd <remote>/<branchname>
    
  • To create a new local non-tracking branch:

    git branch <branchname> [<start-point>]
    
  • To create a new local tracking branch: (Note that if <start-point>is specified and is a remote-tracking branch like origin/foobar, then the --trackflag is automatically included)

    git branch --track <branchname> [<start-point]
    

    Example:

    git branch --track hello-kitty origin/hello-kitty
    
  • To delete a branch on a remote machine:

    git push --delete <remote> <branchname>
    
  • To delete all remote-tracking branches that are stale, that is, where the corresponding branches on the remote machine no longer exist:

    git remote prune <remote>
    
  • 要安全地删除本地分支,无论是跟踪还是非跟踪:

    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>
    


You may have noticed that in some commands, you use <remote>/<branch>, and other commands, <remote> <branch>. Examples: git branch origin/hello-kittyand git push --delete origin hello-kitty.

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

It may seem arbitrary, but there is a simple way to remember when to use a slash and when to use a space. When you're using a slash, you're referring to a remote-tracking branch on your own machine, whereas when you're using a space, you're actually dealing with a branch on a remote machine over the network.

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

回答by Eric Mathison

Local Branch:

本地分行:

A branch on your machine that you can work in and add commits to. You can list these branches with git branch.

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

Local Branch (With Tracking):

本地分支机构(带跟踪):

An ordinary local branch configured to correspond to a remote branch. This has benfits like the ability to git pulland git pushwithout having to specify the repository and branch name. Tracking also causes git statusto inform you when your branch is ahead or behind the remote.

一个普通的本地分支,配置为对应一个远程分支。这有很多好处,比如可以指定存储库和分支名称,git pull也可以git push不必指定。跟踪还会导致git status在您的分支在遥控器之前或之后通知您。

Remote Branch:

远程分支:

Simply a branch on a remote repository—typically on a server such as GitHub etc.

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

Remote Tracking Branch:

远程跟踪分支:

A local copy of a remote branch. This branch should never be edited. Its purpose is to keep track of the current state of a remote branch. Remote tracking branches can be viewed with git branch -rand typically look something like origin/master(repo name followed by a slash followed by the branch name). Running git fetchwill update the remote tracking branches to reflect the state of the corresponding remote branches.

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

git branch -avvis my personal favorite for displaying a quick overview of which branches are on my machine, which branches are on the remote, and the latest commit in each. The -apart specifies that all branches should be shown (remote and local). The v's on the end stand for verbose (it shows the last commit hash and message). Thanks to @Flimm for pointing out that the second vadds info about which local branch is tracking which remote.

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