2

谁能帮我理解我在 git 中发现的这个怪癖?

以下是如何重现此怪癖:

$ mkdir git-test && cd git-test
$ git init
Initialized empty Git repository in /tmp/git-test/.git/
$ echo hello > world
$ git add world
$ git commit -m'first commit'
[master (root-commit) 5f68103] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 world

酷,到目前为止还好;现在让我们分支:

$ git checkout -b a_branch
$ mkdir a_dir
$ echo foo > a_dir/bar
$ git add a_dir/bar
$ git commit -m message
[a_branch (root-commit) 2fbef71] message
 1 file changed, 1 insertion(+)
 create mode 100644 a_dir/bar

好的,怪癖来了!

$ cd a_dir
$ pwd
/tmp/git-test/a_dir
$ git checkout master
Switched to branch 'master'
$ pwd
/tmp/git-test/a_dir

哇!?此分支中不存在此路径!

$ ls
total 0

甚至ls似乎工作...

$ cd ..
$ ls
world

目录“a_dir”现在神奇地消失了!

这怎么可能?

4

2 回答 2

4

If I understand correctly, this has not much to do with git.

Your current working directory is being removed by some process (in this case the git branch switch). The same thing would happen if you rmdir the current working directory.

$ cd /tmp
$ mkdir test
$ cd test
$ rmdir ../test/
$ pwd
/tmp/test
$ ls /tmp/test
ls: /tmp/test: No such file or directory

You can see similar effect when deleting files that are open: They will disappear from the file system, but the program that opened them can still access them, and disk space will only be reclaimed after the program closes them. Note that this behaviour is completely different between UNIXy systems and Windows. Windows would not be let you delete them in the first place.

于 2013-04-20T01:13:49.110 回答
0

Seems like the directory is locked because you are cd'd into it... So the change to branch master cannot remove the dir while it is in use...

于 2013-04-20T01:14:00.840 回答