带点的 Git 结帐

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

Git checkout with dot

git

提问by Pepelac

What difference between next gitcommands:

下一个git命令之间有什么区别:

git  checkout branch
git  checkout branch .
git  checkout  .  #<-- used at the branch

Why when I checkout different branches into different folders with first one I missed some files.
But when I am using second command, everything is ok?

为什么当我用第一个分支将不同的分支签出到不同的文件夹时,我错过了一些文件。
但是当我使用第二个命令时,一切正常吗?

回答by Jan Hudec

git checkout(1)does very different things whether given path specifier or not.

git checkout(1)无论是否给定路径说明符,都会做非常不同的事情。

  1. With branch specifier only(git checkout branch) it will switch current working directory to specified branch, keeping local changes if possible and failing otherwise. If you already are on branch, it will do nothing at all. It only modifies files in the working directory that differ between HEADand branchand fails if any of them has local modifications (indexed or not).
  2. With path specifierit will overwrite the all matching files (all files match .) with specified content:
    1. With path specifier only(git checkout .) it writes content from index. That it is undoes unstaged local modification. To undo staged modifications, use git resetwith path specifier.
    2. With both branch and path specifiers(git checkout branch .) it writes content in specified revision. It will notmodify where HEADpoints, so if branchis different from HEAD, there will be unstaged changes afterwards.
  1. 使用分支说明符( git checkout branch) 它将当前工作目录切换到指定分支,如果可能保持本地更改,否则失败。如果你已经在branch,它什么都不做。它只修改工作目录中在HEAD和之间不同的文件,branch如果其中任何一个具有本地修改(索引或未索引),则失败。
  2. 使用路径说明符,它将.用指定的内容覆盖所有匹配的文件(所有文件匹配):
    1. 使用路径说明符( git checkout .) 它从索引写入内容。它撤消未暂存的本地修改。要撤消分阶段修改,请使用git reset路径说明符。
    2. 使用分支和路径说明符( git checkout branch .),它将内容写入指定的修订版。它不会修改 whereHEAD点,所以如果branch与 不同HEAD,之后会有非阶段性的变化。

Note, that the man pagedistinguishes additional cases for use of the -b/--branch option and -p/--patch option, but those are mostly straightforward extensions of the above cases.

请注意,手册页区分了使用 -b/--branch 选项和 -p/--patch 选项的其他情况,但这些大多是上述情况的直接扩展。

回答by bawa g

The above explanation is fine but let me explain with examples.

上面的解释很好,但让我用例子来解释。

git checkoutcan be used with files folders and branches.

git checkout可以与文件夹和分支一起使用。

Let say there is index.htmlfile and devfolder.

假设有index.html文件和dev文件夹。

If I accidentally change index.htmland i want to undo that I will simply run git checkout index.html. It will get my file back to its original form.

如果我不小心更改index.html并且我想撤消该更改,我将简单地运行git checkout index.html. 它会让我的文件恢复到原来的形式。

Now let say I made some changes inside dev folder and and want those changes back inside dev folder. If I will use git checkout devand if there is any devbranch then it will checkout that dev branchrather than folder named dev.

现在假设我在 dev 文件夹中进行了一些更改,并希望将这些更改放回 dev 文件夹中。如果我将使用git checkout dev并且如果有任何dev分支,那么它将签出该dev 分支而不是名为dev.

So rather I would run git checkout -- dev

所以我宁愿跑 git checkout -- dev

Now this bare double dash stands for current branch. So above command is asking from git is that please give me 'dev' named folder from current branch.

现在这个裸双破折号代表当前分支。所以上面的命令是从 git 询问的是,请给我当前分支的“dev”命名文件夹。

Lets talk about your use case.

让我们谈谈您的用例。

git checkout branchthis command will simply checkout the branch named 'branch'

git checkout branch此命令将简单地检出名为“branch”的分支

git checkout branch .The second command will tell git that please checkout .or current folder name from the branch named 'branch'

git checkout branch .第二个命令将告诉 git 请.从名为“branch”的分支签出或当前文件夹名称

git checkout . #<-- used at the branchas you are saying first you switched to branch named 'branch' using git checkout branchthen you are simply doing git checkout .Now there is no branch named .so it will simply pull down current folder name from current branch.

git checkout . #<-- used at the branch正如您首先所说,您切换到名为“branch”的分支,git checkout branch然后您只是在做git checkout .现在没有命名分支,.因此它只会从当前分支中拉下当前文件夹名称。