3

I want to drop a certain commit completely.

I did this experiment:

Init a repo

bash-3.2$ git init
Initialized empty Git repository in /Users/*****/test/.git/

create a file

bash-3.2$ touch readme
bash-3.2$ ls
readme

commit this creation

bash-3.2$ git add .
bash-3.2$ git commit -m "first commit"
[master (root-commit) b9f8e60] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme

edit that file (some mistake happened)

bash-3.2$ echo WrongMsg > readme
bash-3.2$ cat readme
WrongMsg

carelessly commit that modification

bash-3.2$ git add .
bash-3.2$ git commit -m "careless commit"
[master 1eec681] careless commit
1 file changed, 1 insertion(+)

check the log

bash-3.2$ git log --oneline

will give

1eec681 careless commit
b9f8e60 first commit

Now, I recognized that mistake and want to fix it

bash-3.2$ git rebase -i HEAD^

will prompt

pick 1eec681 careless commit

# Rebase b9f8e60..1eec681 onto b9f8e60 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

change pick to drop and then :wq to save, then it will output

Successfully rebased and updated refs/heads/master.

check the log

bash-3.2$ git log --oneline
b9f8e60 first commit

correct the mistake

bash-3.2$ echo CorrectMsg > readme
bash-3.2$ cat readme
CorrectMsg

commit

bash-3.2$ git add .
bash-3.2$ git commit -m "correction"
[master f224289] correction
1 file changed, 1 insertion(+)

log

bash-3.2$ git log --oneline
f224289 correction
b9f8e60 first commit

It seems that drop is succeed, but if I execute this:

bash-3.2$ git reset --hard 1eec681
HEAD is now at 1eec681 careless commit

bash-3.2$ git log --oneline
1eec681 careless commit
b9f8e60 first commit

BANG, the history is still somewhere in the repo and it doesn't really get erased.

Can I erase this history completely?

Nandin Borjigin
  • 2,094
  • 1
  • 16
  • 37
  • 2
    FWIW, in this case, you should have been using ``git commit --amend`` instead of that rebase dance. – Jonas Schäfer Feb 08 '17 at 18:14
  • I marked this as a duplicate because the duplicate has the correct answer (you must expire, or otherwise delete, the reflog entry first, *then* use `git gc` as in Andreas Wederbrand's answer; or you can clone the repository and delete the original, which is even more drastic). However, I upvoted exussum's answer since it's generally the most applicable. – torek Feb 09 '17 at 02:55

3 Answers3

4

git is lazy, all of these commits are kept around you can see them using

git reflog

this will show all of the history changes you have made (Move branch, commit, rebase etc)

They do get cleaned up bu the garbage collector but honestly this feature is more helpful that a nuisance. For the tiny amount of space it will use I wouldn't worry about it.

exussum
  • 18,275
  • 8
  • 32
  • 65
1

It will get erased eventually but if you really need the commit removed you can run git gc manually

You need to run it with --aggressive and --prune=all to really make sure the dangling commit is removed.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
0

Type git reflog to show your various commit messages and head number. Type git reset --hard HEAD{Head Number goes in here}. The head number is where you want you the project to revert to. Warning: Once reverted, you can't go back to that point in time.

Intelligent
  • 127
  • 1
  • 12
  • But the commit will still be there. I can go back to that wrong commit as long as I know the SHA of it. I want to completely delete that commit. – Nandin Borjigin Feb 08 '17 at 18:21
  • Follow this link. It might help you. https://github.com/blog/2019-how-to-undo-almost-anything-with-git – Intelligent Feb 08 '17 at 18:27