oshirowanen
oshirowanen

Reputation: 15965

Deleting a local commit?

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>

Upvotes: 9

Views: 22151

Answers (4)

isca
isca

Reputation: 1144

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

Upvotes: 2

Boris Brodski
Boris Brodski

Reputation: 8715

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.

Upvotes: 15

jbr
jbr

Reputation: 6258

What you are suggesting will only remove your third commit, and retain commit one and two. This is one of those cases where you can use git rebase.

The syntax is like this:

git rebase --onto <branch name>~<first commit number to remove> \
<branch name>~<first commit to be kept> <branch name>

I'd suggest you try it out on a copy of your reop first.

Upvotes: 0

El Bert
El Bert

Reputation: 3026

Commit #3 will be updated with a new parent commit in your case commit #1 and it will get a new sha1. If the changes in you commit #2 and #3 are independent you shouldn't have any problem/conflict.

Upvotes: 0

Related Questions