如果我在.git下删除HEAD和FETCH_HEAD会发生什么

时间:2015-12-21 13:55:52

标签: git version-control metadata

如果删除目录.git下的HEADFETCH_HEAD会怎样?

我确实删除了这些文件,并对README.txt进行了一些尝试拉动和推动(不创建任何分支)的更改。它工作得很好。
如果我做更严肃的事情,它会产生问题吗?比如变基和重置?

Git如何跟踪我正在处理的当前分支?我认为HEADFETCH_HEAD应该表示这些事情。 .git下我可以删除的所有文件和目录是什么,git还能继续工作吗?

换句话说,.git git在本地和远程完美无缺地工作的必要条件是什么?

$ pwd
/home/arulmozhi/_/remote-repo/.git
$ rm HEAD
$ cd ..
$ git pull
Already up-to-date.

$ cat README.md
this stuff is from dummy branch
First file in repo which is to be synced across multiple remotes - gitlab and github

$ emacs -nw README.md

$ git commit -am "what happens when we rename/delete .git/HEAD"
[master 34d647b] what happens when we rename/delete .git/HEAD
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

$ git push
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 401 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@gitlab.com:remote-repo/remote-repo.git
   fa76250..34d647b  master -> master

$ uname -arn
Linux koparakesari 3.19.0-31-generic #36~14.04.1-Ubuntu SMP Thu Oct 8 10:21:08 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ ls -1 .git
branches
COMMIT_EDITMSG
config
description
hooks
index
info
logs
objects
packed-refs
refs

2 个答案:

答案 0 :(得分:1)

我刚刚删除了 HEAD ,它确实“创建了问题”(git 2.6.4):

C:\Users\VonC\prog\git\test>rm .git\HEAD

C:\Users\VonC\prog\git\test>git st
fatal: Not a git repository (or any of the parent directories): .git

我必须恢复HEAD(ref: refs/heads/master)的内容才能再次识别回购。

另一方面,

FETCH_HEAD 会在下一个git fetch重新生成。

答案 1 :(得分:1)

在Ubuntu上:

$ git init
Initialized empty Git repository in /test/.git/
$ git status
On branch master
...
$ rm .git/HEAD 
$ git status
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

所以,显然你的测试用例中有一些不同的东西是无法再现的。