本地分支、本地跟踪分支、远程分支和远程跟踪分支有什么区别? [英] What are the differences between local branch, local tracking branch, remote branch and remote tracking branch?

查看:22
本文介绍了本地分支、本地跟踪分支、远程分支和远程跟踪分支有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

  • 当地分支机构
  • 本地跟踪分支机构
  • 远程分支
  • 远程跟踪分支

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

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

我想快速演示代码会很有帮助.

A quick demo code will be really helpful I guess.

推荐答案

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

A local branch is 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"

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

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

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 的远程跟踪分支.此远程跟踪分支跟踪 origin 上的远程分支 myNewBranch.您可以使用 git fetchgit pull 更新您的远程跟踪分支以与远程分支同步.

A remote tracking branch is a local copy of a remote branch. When myNewBranch is pushed to origin using the command above, a remote tracking branch named origin/myNewBranch is created on your machine. This remote tracking branch tracks the remote branch myNewBranch on origin. You can update your remote tracking branch to be in sync with the remote branch using git fetch or git 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"

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

A local tracking branch is a local branch that 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 origin using the git push command with a -u option (as shown above), you set up the local branch myNewBranch to track the remote tracking branch origin/myNewBranch. This is needed to use git push and git pull without specifying an upstream to push to or pull from.

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.

这篇关于本地分支、本地跟踪分支、远程分支和远程跟踪分支有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆