Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git remove root commit

Tags:

git

commit

root

I can't do a lot of things with git and I want to remove a commit from my repo, because I uploaded wrong stuff.

I used git revert <the_commit> but since the commit is root and I can't remove it. fatal: Cannot revert a root commit

What to do in that case ?

Please don't give me links to other topics here, I read them and I don't understand what to do, I need some basic example about removing some commit.

like image 680
Alex Emilov Avatar asked May 27 '11 08:05

Alex Emilov


People also ask

How do I remove a commit in git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.

How do I remove a middle commit?

Deleting the "Middle" Commit from the History. All you need to do is typing "drop" at the beginning of each commit you want to delete. Be careful while using the git rebase command, as it may cause sudden problems. So, it is more recommended to use the git revert command.


1 Answers

You can do this using git filter-branch. First, identify the commit ID at the root that you want to remove. I'll represent that with <the_commit>. Then, run git filter-branch using --parent-filter and a sed command that snips off that parent:

git filter-branch --parent-filter "sed 's/-p <the_commit>//'" HEAD

Here's a transcript of an example I just tried:

$ git log
commit 7e1ba37b51fc2cc6289cf66367c9aedc74c664a8
Author: Greg Hewgill <[email protected]>
Date:   Fri May 27 20:54:27 2011 +1200

    three

commit a8a410d2361824cbd518a48225e9402a691be93f
Author: Greg Hewgill <[email protected]>
Date:   Fri May 27 20:54:17 2011 +1200

    two

commit 3171d512d98f6bc5f3c2469312930c0d32d3aa07
Author: Greg Hewgill <[email protected]>
Date:   Fri May 27 20:54:00 2011 +1200

    one
$ git filter-branch --parent-filter "sed 's/-p 3171d512d98f6bc5f3c2469312930c0d32d3aa07//'" HEAD
Rewrite 7e1ba37b51fc2cc6289cf66367c9aedc74c664a8 (3/3)
Ref 'refs/heads/master' was rewritten
$ git log
commit 489ec1ee20e0dd20cd835ceebf157f628cd75a44
Author: Greg Hewgill <[email protected]>
Date:   Fri May 27 20:54:27 2011 +1200

    three

commit a6f5ee410c9ea4fca6fbff265149b7fc555241eb
Author: Greg Hewgill <[email protected]>
Date:   Fri May 27 20:54:17 2011 +1200

    two
$ 
like image 64
Greg Hewgill Avatar answered Sep 29 '22 09:09

Greg Hewgill