victorpacheco3107
victorpacheco3107

Reputation: 862

How to remove commit remotely after another commits

A git status gives me something like this:

commit XXXXX
Author: AAAA
Date:   Thu Feb 4 16:47:06 2016 -0500

commit YYYYY
Author: BBBB
Date:   Thu Feb 4 16:47:05 2016 -0500

commit ZZZZZ
Author: CCCC
Date:   Thu Feb 4 16:47:04 2016 -0500

I only want to remove changes introduced by YYYYY, that is, I want to keep changes introduced by XXXXX.

Upvotes: 1

Views: 51

Answers (2)

alextercete
alextercete

Reputation: 5151

You can revert the changes introduced by a commit:

git revert YYYYY

This will create a new commit, which is usually preferred over doing a rebase if you have already pushed your changes to a long-living remote branch (such as master).

Upvotes: 3

Jonathan.Brink
Jonathan.Brink

Reputation: 25383

An interactive rebase can be used in this situation.

Run this command:

git rebase -i YYYY~

You will be faced with a list of commits. Delete the line with commit YYYY. Then, save and exit.

That commit is now "deleted" locally. A better way to say what just happened is that the branch you are on has been rewritten, so all commits after where YYYY used to be will have a new SHA1 Hash.

At this point you need to force-push since history has been rewritten. If this is on the master branch:

git push -f origin master

Beware that if others are also pulling from this branch a typical pull will not work correctly for them since the tip of that branch will not be reachable with their local copy of the branch.

They can rebase their local branch on top of the new master with:

git fetch
git rebase origin/master

If there are others on this project you should make them aware of what you are doing prior to the operation.

Edit: if you want to actually remove the commit the interactive rebase is a good option. But @alextercete's approach using revert is superior since you don't have to re-write history.

Upvotes: 0

Related Questions