Ben Aston
Ben Aston

Reputation: 55729

Removing commits from a remote

I have a number of commits I would like to remove from a git remote. Can I do this by performing an interactive rebase and skipping the commits I don't want?

...and then force pushing to the remote?

Or can revert accept a list of commits to revert?

Upvotes: 1

Views: 87

Answers (1)

isomarcte
isomarcte

Reputation: 1981

So you can do this, by doing a rebase or anything else for that matter, on your local branch and then forcing the push.

git push --force origin master

But this is a really dangerous thing to do if this is a shared repo. In particular, if anyone has made a checkout, their tree is going to be all messed up when they try to pull again.

A better way to do this is to use revert which does the following (from the man page),

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

Or in layman's terms, it makes new commits that undo the old ones. You can then push them to the remote without using --force. There will still be a record of all the old commits, and you can still revert or reset back to them, but it will be safe.

And yes, revert can take a range of commits to revert. The example from the man page is,

git revert -n master~5..master~2

Which will revert fifth to the third last commit. Using revert with a range does not create a new commit however, it just modifies the working tree. So you will have to make your own commit after.

It still might make someone grumpy when they have to merge with you though, as merging with a revert can sometimes make files and edits that were in use go away.

Upvotes: 1

Related Questions