git签出- b#分支名称#原点/#分支名称#。

时间:2021-04-11 23:43:05

Usually when I want to switch to another branch that is on the remote repository but not on my local, I used command: git checkout origin/#branch name# instead of using git checkout –b #branch name# origin/#branch name# which I see this usage on the internet.

通常,当我想切换到远程存储库上的另一个分支,而不是本地的分支时,我使用的是命令:git checkout origin/#branch名#,而不是使用git checkout - b# branch name# origin/#branch name#,我在internet上看到这种用法。

What's the difference between these two commands?

这两个命令有什么不同?

2 个解决方案

#1


1  

The latter creates a local branch tracking the remote branch and should be used for editing. The former should only be used for quick glances at the remote branch. Got will only allow you to make changes on a local branch so you will not be able to commit if you use the former. Recent versions of git allow you to use the shorthand git checkout #branchname# which will automatically create a branch tracking origin/#branchname# if you have a single remote.

后者创建一个本地分支,跟踪远程分支,并应该用于编辑。前者只能用于远程分支的快速浏览。get只允许您在本地分支上进行更改,因此如果您使用前者,您将无法提交。git的最新版本允许您使用简写的git checkout #branchname#,它将自动创建一个分支跟踪源/#branchname#,如果您有一个远程。

#2


1  

The former will checkout the remote branch, this usually results in the following warning:

前者将签出远程分支,这通常会导致以下警告:

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

你处于“分离的头部”状态。您可以四处查看,进行实验更改并提交它们,并且您可以丢弃在此状态下的任何提交,而不影响任何分支,执行另一个签出。

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

如果您想要创建一个新的分支来保留您创建的提交,那么您可以使用-b再次使用-b来完成(现在或以后)。例子:

 git checkout -b new_branch_name

You end up with a detached HEAD when you check out a commit that has not a local branch pointing to it. This means that while you can do whatever you want with it, when you commit changes, there is no branch pointing to the commits you create. The only pointer pointing at it is HEAD which is a transient pointer that changes whenever you switch the branch.

当您签出一个没有指向它的本地分支的提交时,您会得到一个独立的头。这意味着,尽管您可以使用它来做任何事情,但当您提交更改时,并没有指向您创建的提交的分支。指向它的唯一指针是HEAD,它是一个瞬时指针,每当你切换分支时,它就会改变。

So committing with a detached HEAD essentially means that all commits you create will be lost, if you don’t create a branch later that points to them.

因此,使用一个独立的头意味着您创建的所有提交都将丢失,如果稍后您不创建一个分支指向它们。

You can do that, as the warning points out, with git checkout -b branch_name. This creates a new branch that points to HEAD and checks out that branch, putting you back into a safe situation.

您可以使用git checkout -b branch_name来实现这一点。这就创建了一个新的分支,指向该分支,并检查该分支,使您回到安全的状态。

Now, when you do git checkout -b <branch> origin/<branch> you basically do both at once: You check out origin/<branch> and also create a local branch called <branch> that points to that checked out version (and you also switch to that branch). So you end up in a safe situation immediately.

现在,当您执行git checkout -b origin/ 时,您基本上同时做这两种工作:检查origin/ ,并创建一个名为 的本地分支,该分支指向该检查版本(并且您也切换到该分支)。所以你马上就会处于安全的状态。

For that purpose, the command is identical to this:

为此目的,该命令与以下内容相同:

# check out the remote branch into a detached HEAD
git checkout origin/<branch>
# create a new branch from the HEAD and check that branch out
git checkout -b <branch>

And also identical to this:

同样的,

# create a new local branch pointing to the remote branch
git branch <branch> origin/<branch>
# check out that branch
git checkout <branch>

Btw. there is a useful shortcut when dealing with new remote branches. For example if the remote has a branch foo and you don’t have a local branch foo yet, you can just do git checkout foo and Git will recognize that you probably want to create a local branch foo that tracks the remote branch origin/foo.

顺便说一句。在处理新的远程分支时,有一个有用的快捷方式。例如,如果远程有一个分支foo,而您还没有本地分支foo,那么您可以只执行git checkout,而git将会识别您可能想要创建一个本地分支foo,来跟踪远程分支源/foo。

#1


1  

The latter creates a local branch tracking the remote branch and should be used for editing. The former should only be used for quick glances at the remote branch. Got will only allow you to make changes on a local branch so you will not be able to commit if you use the former. Recent versions of git allow you to use the shorthand git checkout #branchname# which will automatically create a branch tracking origin/#branchname# if you have a single remote.

后者创建一个本地分支,跟踪远程分支,并应该用于编辑。前者只能用于远程分支的快速浏览。get只允许您在本地分支上进行更改,因此如果您使用前者,您将无法提交。git的最新版本允许您使用简写的git checkout #branchname#,它将自动创建一个分支跟踪源/#branchname#,如果您有一个远程。

#2


1  

The former will checkout the remote branch, this usually results in the following warning:

前者将签出远程分支,这通常会导致以下警告:

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

你处于“分离的头部”状态。您可以四处查看,进行实验更改并提交它们,并且您可以丢弃在此状态下的任何提交,而不影响任何分支,执行另一个签出。

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

如果您想要创建一个新的分支来保留您创建的提交,那么您可以使用-b再次使用-b来完成(现在或以后)。例子:

 git checkout -b new_branch_name

You end up with a detached HEAD when you check out a commit that has not a local branch pointing to it. This means that while you can do whatever you want with it, when you commit changes, there is no branch pointing to the commits you create. The only pointer pointing at it is HEAD which is a transient pointer that changes whenever you switch the branch.

当您签出一个没有指向它的本地分支的提交时,您会得到一个独立的头。这意味着,尽管您可以使用它来做任何事情,但当您提交更改时,并没有指向您创建的提交的分支。指向它的唯一指针是HEAD,它是一个瞬时指针,每当你切换分支时,它就会改变。

So committing with a detached HEAD essentially means that all commits you create will be lost, if you don’t create a branch later that points to them.

因此,使用一个独立的头意味着您创建的所有提交都将丢失,如果稍后您不创建一个分支指向它们。

You can do that, as the warning points out, with git checkout -b branch_name. This creates a new branch that points to HEAD and checks out that branch, putting you back into a safe situation.

您可以使用git checkout -b branch_name来实现这一点。这就创建了一个新的分支,指向该分支,并检查该分支,使您回到安全的状态。

Now, when you do git checkout -b <branch> origin/<branch> you basically do both at once: You check out origin/<branch> and also create a local branch called <branch> that points to that checked out version (and you also switch to that branch). So you end up in a safe situation immediately.

现在,当您执行git checkout -b origin/ 时,您基本上同时做这两种工作:检查origin/ ,并创建一个名为 的本地分支,该分支指向该检查版本(并且您也切换到该分支)。所以你马上就会处于安全的状态。

For that purpose, the command is identical to this:

为此目的,该命令与以下内容相同:

# check out the remote branch into a detached HEAD
git checkout origin/<branch>
# create a new branch from the HEAD and check that branch out
git checkout -b <branch>

And also identical to this:

同样的,

# create a new local branch pointing to the remote branch
git branch <branch> origin/<branch>
# check out that branch
git checkout <branch>

Btw. there is a useful shortcut when dealing with new remote branches. For example if the remote has a branch foo and you don’t have a local branch foo yet, you can just do git checkout foo and Git will recognize that you probably want to create a local branch foo that tracks the remote branch origin/foo.

顺便说一句。在处理新的远程分支时,有一个有用的快捷方式。例如,如果远程有一个分支foo,而您还没有本地分支foo,那么您可以只执行git checkout,而git将会识别您可能想要创建一个本地分支foo,来跟踪远程分支源/foo。