Remove a commit and related changes from a branch
will list all the commits on the current branch, suppose I got three commits, and I want to remove the one in the middle, you may suggest the following command:
git reset --hard <sha1-commit-id>
However, I don’t want to revert to that commit id, I want just to remove it from the branch so next time when I do
git log it will not figure in the list. Thanx in advance.
EDIT, sorry for my miss explanation, when I said removing the commit id, I meant removing all related changes as well.
3 Solutions collect form web for “Remove a commit and related changes from a branch”
To effectively undo the changes in that commit and remove it from history, you could do something like this:
git rebase --onto HEAD^^ HEAD $BRANCH
$BRANCH is the name of the branch you are currently on.
Alternatively, you could check out the older commit and then cherry-pick the newer one:
TIP=`git rev-parse HEAD` git reset --hard HEAD^^ git cherry-pick $TIP
Keep in mind that both operations change history, and this will require a
git push --force operation if you have already published these commits.
Is the commit also on a remote branch? Depending on the answer to this will determine what you should do.
If it isn’t, do
git rebase -i <sha before the one you want to remove> This will allow you to delete the commit.
If it is on a remote, you don’t want to remove it. You will end up causing problems with pushing and pulling. Rewriting history after you have pushed is a bad thing. So for that case you want to
git revert <sha that you want to remove>. This creates a new commit that undoes the changes.
git rebase -i HEAD~N
Suppose you dont want 2nd commit, N will be 2.. once you run this command with value of N=2 git will prompt you to take action.. just remove the line for the commit you dont want , it will get deleted…
And to delete tag use
git tag -d [ tag name ]