开发者

Why does git checkout behave inconsistently?

开发者 https://www.devze.com 2023-03-19 15:33 出处:网络
If I delete the entire content of the working directory, then attempt to restore it back using git checkout I received two different behaviors, depending on the command line argument I provide:

If I delete the entire content of the working directory, then attempt to restore it back using git checkout I received two different behaviors, depending on the command line argument I provide:

git checkout master

I just receive a list of all deleted files and directories, but nothing is really checked out.

But when I do:

git checkout <directory 1 in master>
git checkout <directory 2 in master>
git checkout <directory 3 in master>
git checkout <directory 4 in master>
git checkout <directory 5 in ma开发者_JAVA技巧ster>
git checkout <directory 6 in master>
git checkout <directory 7 in master>

Only then does git manage to restore what's missing.

Why is that?

Why does "checkout" have two meanings in git?


It doesn't, however you are giving it two different commands:

git checkout <branch>

or

git checkout <directory>

When you do the second, git assumes the default branch (in your case it appears to be "master"), when you do the first, it won't checkout because there are local changes (all the files you wiped out) that need to be committed to that branch before you change it.

if you want to check out everything (and blow away local changes), you can do:

git checkout -f

which forces the checkout.

Hope this helps


Quicker way to restore what's missing is to do a git reset --hard

git checkout will not overwrite changes you've made in the working tree unless you tell it to.

If you give it a path you are telling it to update the working tree at that path.

If you don't give it a path, you are telling it to change the index to the specified branch without updating the working tree at all. It assumes that all the missing files are changes you've made to the working tree that you might want to make in the index and then commit.


From manual page of git-checkout:

Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch.

When <paths> or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named <tree-ish> (most often a commit). ...

0

精彩评论

暂无评论...
验证码 换一张
取 消