问题描述
我刚开始使用Git,我在不同分支之间感到困惑。任何人都可以帮助我弄清楚以下分支类型是什么?
- 本地分行
- 本地跟踪分行
- 远程分行
- 远程跟踪分行
他们之间有什么区别?他们如何相互协作?
一个简单的演示代码对我来说是非常有用的。
本地分支是只有您(本地用户)可以看到的分支。它只存在于你的本地机器上。
git branch myNewBranch#创建名为myNewBranch的本地分支
远程分支是远程位置的分支(大多数情况下都是
原点
)。您可以将新创建的本地分支 myNewBranch
推送到 origin
。
git push -u origin myNewBranch#将新创建的本地分支myNewBranch推送
#远程起源。
#现在在名为origin的远程机器上创建一个名为myNewBranch的新分支是
#
远程追踪分支是远程分支的本地副本。当使用上面的命令将
。 myNewBranch
推送到 origin
时,远程追踪分支名为 origin / myNewBranch
是在您的机器上创建的。这个远程追踪分支追踪 origin
上的远程分支 myNewBranch
。您可以使用 git fetch
或<$ c $更新远程追踪分行以与远程分行同步c> git pull
git pull origin myNewBranch#从分支myNewBranch中提取新的提交
#远程起源到您的机器origin / myNewBranch的远程跟踪
#分支。
#这里的origin / myNewBranch是您在origin上的
#myNewBranch的副本
一个本地跟踪分支是一个跟踪另一个分支的本地分支。这样你可以推/拉提交到/从其他分支。大多数情况下,本地跟踪分支会跟踪远程跟踪分支。当您使用 git push命令
将一个本地分支推送到 origin
时,使用 -u
选项(如上所示),设置本地分支 myNewBranch
来跟踪远程跟踪分支 origin / myNewBranch
。这需要使用
git push
和 git pull
,而不指定上游来推或拉。
git checkout myNewBranch#切换到myNewBranch
git pull#更新远程追踪分支origin / myNewBranch
#to与origin上的远程分支myNewBranch
#同步。
#将这些新的提交从origin / myNewBranch
#提取到本地分支myNewBranch,您只需切换到该位置即可。
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?
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.
解决方案
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"
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"
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"
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.
这篇关于本地分支,本地跟踪分支,远程分支和远程跟踪分支之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!