0

What will happen if I delete the HEAD and FETCH_HEAD under directory .git?

I did delete these files and and made some changes to README.txt tried pulling and pushing(without creating any branches). It worked fine.
Will it create problems if I do more serious things? like rebasing and resetting?

How Git keeps tracks of which current branch I am working on? I thought HEAD and FETCH_HEAD supposed to signify those things. What are all the files and directories under .git that I can delete and still the git will continue to work?

In other words, what are the bare essential things under .git necesssary for git to work locally and with remote flawlessly?

$ 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
vanangamudi
  • 673
  • 1
  • 8
  • 21
  • 3
    If you want everything to work flawlessly, assume that you should not touch `.git` yourself. Any answer that fully documents the results of any manual changes is far too broad for Stack Overflow. – chepner Dec 21 '15 at 13:58
  • That means you must have another .git folder in `/home/arulmozhi/_` or `/home/arulmozhi`: the folder `remote-repo` could be a nested git repo. – VonC Dec 21 '15 at 21:18

2 Answers2

1

I just deleted HEAD, and it did "create problem" (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

I had to restore the content of HEAD (ref: refs/heads/master) in order for the repo to be recognized again.

FETCH_HEAD, on the other hand, is regenerated at the next git fetch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    @vanangamudi If you delete `HEAD` you no longer have a git repository (by definition). Whether that's a problem or not, I suppose, is left up to the reader. – Edward Thomson Dec 21 '15 at 15:06
1

On 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).

So, apparently there has been something different in your test case that cannot be reproduced.

1615903
  • 32,635
  • 12
  • 70
  • 99