0

I have the following graph when I run the command git log --oneline --decorate --graph --all:

* 7ee76a4 (HEAD) added 2.txt
| * 2ff2a0c (master) changed line of 1.txt
|/
* 9a0825b Initial commit

Since head is not in any branch, When I checkout to lets say the 'master' branch with git checkout master the 'HEAD' goes vanished and I have as a graph result the:

* 2ff2a0c (HEAD -> master) changed line of 1.txt
* 9a0825b Initial commit

The question that I have is the following:
Shouldn't the 7ee76a4 commit really get deleted? Although I don't see it in the graph (and it seems deleted), when I do git checkout 7ee76a4, Git takes me back to the previous state (which means that the commit isn't deleted).

Am I forgetting something?

  • It's just hidden. With `git log --oneline --decorate --graph --all 7ee76a4` you can still see it. If you forget the value `7ee76a4`, you can find it in `git reflog`. – ElpieKay Sep 19 '17 at 11:09

1 Answers1

1

It's not so easy to delete a commit in git (see here). If a commit is no longer reachable from any branch, it becomes unreachable. It will still be there and you can still check it out by it's id. Git's reflog will still keep a reference for some time, depending on your reflog settings. Only when the reflog no longer references the commit, it becomes a dangling commit. It will, however, only be deleted when the next garbage collection really does delete it.

This process, however, takes several days up to weeks, depending on your settings and work speed (since the more often you perform git operations, the faster old commits will fall out of your reflog).

The commit is not shown in the graph because it is not reachable from your current HEAD (i.e. not reachable from any branch at all). This does not mean it's deleted.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • So, the common action that all developers should choose is that they should not bother that the commit is there (hidden of course) and they should just forget it? Since the garbage collector may delete it in the future, it should be the go-to action. Right? – Efthymios Kalyviotis Sep 19 '17 at 12:26
  • Right, that's how it's usually done. Since the commit does not appear anywhere unless you explicitly look for it, you don't have to care about it and it will be garbage collected eventually. – kowsky Sep 20 '17 at 07:08
  • If the commit contains sensitive informaion like passwords, you should see that it gets completely purged from the repository. There are several questons (like [this](https://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history)) about this matter. – kowsky Sep 20 '17 at 07:11