git checkouts 的真正含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15296473/
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 15:41:59  来源:igfitidea点击:

What do git checkouts really mean?

gitgit-checkout

提问by daehaai

What are checkouts in git?

checkoutgit中的s 是什么?

I know once you do checkoutto a particular branch, the HEADpoints to that branch. But what does it really mean? Does it mean I can then work on that branch? If yes, then, without checking out a branch, I am not able to work on it?

我知道一旦你checkout对一个特定的分支做了,HEAD指向那个分支的点。但这到底是什么意思?这是否意味着我可以在那个分支上工作?如果是,那么,如果不检查分支,我就无法处理它?

Also, what does remote checkoutmean? How is it useful?

还有,是什么remote checkout意思?它如何有用?

采纳答案by David Culp

As you noted, HEADis a label noting where you are in the commit tree. It moves with you when you move from one commit to another. git checkout <commit>is the basic mechanism for moving around in the commit tree, moving your focus (HEAD) to the specified commit.

正如您所指出的,HEAD是一个标签,指出您在提交树中的位置。当您从一个提交移动到另一个提交时,它会与您一​​起移动。 git checkout <commit>是在提交树中移动的基本机制,将您的焦点 ( HEAD)移动到指定的提交。

The commit can be specified by any of a number of ways, commit hash, branch name, tag name, the relative syntax (HEAD^, HEAD~1, etc.) and so on. It is often useful to consider a checkout to be changing branches, and there are some options that work from that perspective, but they all reference commits.

提交可以通过任何多种方式来指定,提交散列,分行名称,标签名称,相对语法(HEAD^HEAD~1等)等。将 checkout 视为更改分支通常很有用,从这个角度来看,有一些选项是有效的,但它们都引用了提交。

To checkout a commit has some side affects other than moving HEADaround.

结帐提交除了HEAD四处移动之外还有一些副作用。

  • The working directory is updated to the state of the checked out commit.
  • if a branch name is specified, checkout makes that branch active. The active branch will move along with any new commits that are added.
    • with the -boption a new branch will be created based on the current commit and then made active.
    • with the --trackoption the checked out branch can be made aware of a remote branch
    • with the --orphanoption a new branch is created (like with -b) but will not be based on any existing commit.
  • 工作目录更新为签出提交的状态。
  • 如果指定了分支名称,则 checkout 会激活该分支。活动分支将与添加的任何新提交一起移动。
    • 使用该-b选项,将根据当前提交创建一个新分支,然后使其处于活动状态。
    • 使用--track选项可以使签出的分支知道远程分支
    • 使用该--orphan选项创建一个新分支(如 with -b),但不会基于任何现有提交。

There are a few more options, which you can read about in the git checkout man-page, all of which revolve around moving from one commit to another -- just varying in what effect that move has in addition to moving HEAD.

还有更多选项,您可以在git checkout 手册页中阅读,所有这些都围绕从一个提交移动到另一个提交 - 除了移动之外,移动的效果有所不同HEAD

回答by bawa g

Let me explain some use cases of checkout with file, folder and branches so that it might be helpful in understanding.

让我解释一些使用文件、文件夹和分支结帐的用例,以便它可能有助于理解。

Let say we have folder named devand index.htmlalso Everything is tracked and working directory is clean.

假设我们有文件夹命名devindex.html并且一切都被跟踪并且工作目录是干净的。

If I accidentally change file name index.htmland I want to undo that i will simply use git checkout index.htmlit will recover that file state from repository currently selected branch.

如果我不小心更改了文件名index.html并且我想撤消该操作,我将简单地使用git checkout index.html它将从存储库当前选择的分支中恢复该文件状态。

Now if I made some change in devfolder and want to recover that. I can use git checkout devbut what if there is already branch named devinstead of checking out that folder it will pull down that branch. To avoid that I would rather do git checkout -- dev.

现在,如果我对dev文件夹进行了一些更改并想恢复它。我可以使用,git checkout dev但是如果已经有分支命名dev而不是检查该文件夹,它将拉下该分支。为了避免这种情况,我宁愿这样做git checkout -- dev

Now here bare double dash stands for current branch and asking git for folder devfrom currently selected branch.

现在这里裸双破折号代表当前分支,并dev从当前选择的分支向 git 询问文件夹。

Similarly If I do git checkout alpha devit will pull down dev folder from alpha branch.

同样,如果我这样做git checkout alpha dev,它将从 alpha 分支下拉 dev 文件夹。

This answer is for your first question 'git checkout really mean'.

这个答案是针对你的第一个问题“git checkout 真的意味着”。

回答by Michael Wild

"To check out" means that you take any given commit from the repository and re-create the state of the associated file and directory tree in the working directory.

“签出”意味着您从存储库中获取任何给定的提交并在工作目录中重新创建关联文件和目录树的状态。

When you check out a commit that is nota branch head (e.g. git checkout HEAD~2), you are on a so-called detachedhead. You can create commits here, but once you switch to a different branch, those commits will not be recoverable by a branch name and might even get removed by the garbage collector after some time.

当您检出不是分支头的提交(例如git checkout HEAD~2)时,您处于所谓的分离头上。您可以在此处创建提交,但是一旦切换到不同的分支,这些提交将无法通过分支名称恢复,甚至可能在一段时间后被垃圾收集器删除。