How to revert multiple git commits?

2018-12-31 18:09发布

I have a git repository that looks like this:

A -> B -> C -> D -> HEAD

I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A.

It sounds like I can either try to rebase (doesn't apply, since I've pushed changes in between), or revert. But how do I revert multiple commits? Do I revert one at a time? Is the order important?

12条回答
余生无你
2楼-- · 2018-12-31 18:39
git reset --hard a
git reset --mixed d
git commit

That will act as a revert for all of them at once. Give a good commit message.

查看更多
只若初见
3楼-- · 2018-12-31 18:41

If you want to temporarily revert the commits of a feature, then you can use the series of following commands.

Here is how it works

git log --pretty=oneline | grep 'feature_name' | cut -d ' ' -f1 | xargs -n1 git revert --no-edit

查看更多
几人难应
4楼-- · 2018-12-31 18:46

None of those worked for me, so I had three commits to revert (the last three commits), so I did:

git revert HEAD
git revert HEAD~2
git revert HEAD~4
git rebase -i HEAD~3 # pick, squash, squash

Worked like a charm :)

查看更多
路过你的时光
5楼-- · 2018-12-31 18:50

In my opinion a very easy and clean way could be:

go back to A

git checkout -f A

point master's head to the current state

git symbolic-ref HEAD refs/heads/master

save

git commit
查看更多
情到深处是孤独
6楼-- · 2018-12-31 18:51

Clean way which I found useful

git revert --no-commit HEAD~3..

This command reverts last 3 commits with only one commit.

Also doesn't rewrite history.

查看更多
人间绝色
7楼-- · 2018-12-31 18:53

For doing so you just have to use the revert command, specifying the range of commits you want to get reverted.

Taking into account your example, you'd have to do this (assuming you're on branch 'master'):

git revert master~3..master

This will create a new commit in your local with the inverse commit of B, C and D (meaning that it will undo changes introduced by these commits):

A <- B <- C <- D <- BCD' <- HEAD
查看更多
登录 后发表回答