简体   繁体   English

GIT - 跟踪分支与克隆之间的差异

[英]GIT - Difference between tracking a branch versus cloning

I've seen this command floating around on various sites. 我看到这个命令漂浮在各个站点上。

git checkout --track -b <...>

If I create a bare repo on a remote server and work from two different locations, what is the quickest and "approved" way of doing so? 如果我在远程服务器上创建一个裸仓库并在两个不同的位置工作,那么最快和“批准”的方式是什么?

What I did was, I created the initial repo on my laptop and then pushed the changes to the "origin" where my VPS repo is (the bare repo). 我做的是,我在笔记本电脑上创建了初始仓库,然后将更改推送到我的VPS仓库所在的“原产地”(裸仓库)。 Now, on my desktop, should I be cloning my repo? 现在,在我的桌面上,我应该克隆我的回购? I ask because I have two branches, "dev" and "master." 我问,因为我有两个分支,“dev”和“master”。 Once I'm on my desktop, I wasn't sure if I should be "tracking" the repo or should I be cloning first? 一旦我在桌面上,我不确定是否应该“跟踪”回购或者我应该先克隆? What if I wanted to work on the dev branch, is that when I checkout using the --track directive? 如果我想在dev分支上工作,那么当我使用--track指令结账时该怎么办?

Here's what I've done so far. 这是我到目前为止所做的。

On laptop 在笔记本上

cd devproject
git init
git add .
git commit -m "My first commit"

On VPS Repo 关于VPS回购

mkdir /home/sam/devproject.git
cd /home/sam/devproject.git
git --bare init
exit

Back to laptop 回到笔记本电脑

cd devproject
git remote add origin ssh://myserver.com/home/sam/devproject.git

On Desktop (??) 在桌面上(??)

git clone <..>

You clone a repository, but you track a branch. 克隆存储库,但跟踪分支。 The checkout command you posted is not complete: 您发布的结帐命令未完成:

git checkout --track -b new_local_branch_name origin/remote_branch_name

Thus the required steps would be: 因此,所需的步骤是:

  1. Clone the remote repository. 克隆远程存储库。
  2. Track the remote branches. 跟踪远程分支。

The command above will not work if you're not in a repository. 如果您不在存储库中,则上述命令将不起作用。 To work with git, you must always first create a repository, either by cloning one which already exists or using git-init and starting from scratch. 要使用git,您必须始终首先创建一个存储库,方法是克隆已存在的存储库或使用git-init并从头开始。

git checkout --track -b <branch> <remote-branch>
git checkout --track <remote-branch>

These two commands create a new local branch to track <remote-branch> . 这两个命令创建一个新的本地分支来跟踪<remote-branch> The first one manually names it <branch> ; 第一个手动命名为<branch> ; the second uses the same name as the remote. 第二个使用与遥控器相同的名称。

Remember that tracking doesn't mean automatic update - it simply does things like specifying where the branch should push/pull from and letting git status give those "your branch is behind origin/master by 5 commits, and can be fast-forwarded" messages. 请记住,跟踪并不意味着自动更新 - 它只是指定分支应该从哪里推/拉,让git状态给出那些“你的分支在原始/主人之后通过5次提交,并且可以快速转发”的消息。

When you use 当你使用

git checkout --track -b local_branch_name origin/remote_branch_name

(usually with 'local_branch_name' the same as 'remote_branch_name', for which shortcut exists: (通常使用'local_branch_name'与'remote_branch_name'相同,存在快捷方式:
"git checkout -b --track origin/branch_name"), it means that you create local branch named 'local_branch_name', on which you can create commits, for which the upstream branch would be remote-tracking branch named 'remote_branch_name' (which tracks/follows this remote-tracking branch). “git checkout -b --track origin / branch_name”),这意味着您创建名为“local_branch_name”的本地分支 ,您可以在其上创建提交,其上游分支将是名为“remote_branch_name”的远程跟踪分支 (其中跟踪/跟踪此远程跟踪分支)。

You need to remember that you can't commit directly on 'origin/remote_branch_name'; 您需要记住,您不能直接在'origin / remote_branch_name'上提交; this remote-tracking branch is meant to track progress of 'remote_branch_name' branch in remote 'origin' ('origin' is default name of remote you cloned from). 此远程跟踪分支用于跟踪远程“origin”中“remote_branch_name”分支的进度(“origin”是您从中克隆的远程的默认名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM