Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a local commit?

Tags:

git

I have 3 commits (I am currently only using GIT on my local machine).

If I delete commit 2, will it affect any of the changes in commit 3 as commit 3 was followed on from commit 2?

I was planning on using:

git reset --hard <commit 2 id here>
like image 648
oshirowanen Avatar asked Mar 13 '14 14:03

oshirowanen


2 Answers

The command

git reset --hard <commit 2 id here>

doesn't delete commit 2. This will just put your current branch on the commit 2. If no other branches point to the commit 3 you may loose it during garbage collection.

What you need is interactive rebase:

git rebase -i HEAD~2

Then you will get editor started with commit 2 and commit 3 listed. Just remove the line with the commit 2, save and exit the editor. This will remove commit 2 leaving commit 3 in tact. The parent of the commit 3 will be commit 1. All changes introduced with commit 2 will done.

like image 61
Boris Brodski Avatar answered Sep 28 '22 20:09

Boris Brodski


I came here by the title is saying delete. So, just would like to mention if you DON'T want to keep the commits, and want to just delete them and overlap, it's also possible. If you know exactly what you're doing and would like to just purge the commit, you can overlap it by forcing the HEAD on top of the commit e.g:

git reset --hard HEAD~2

to move the HEAD by two commits behind, then push the HEAD with:

git push origin HEAD --force
like image 33
isca Avatar answered Sep 28 '22 20:09

isca