Roll back to old commit with repeat

I want a rollback to commit with git revert to keep the story. Is there an easier way to do this to run git revert for every hash I want to return? Is there a faster way to go back from a specific commit to another commit hash?

On the man page, they state that you can do something like this git revert -n master~5..master~2 , but when I try to do this, I get this error: fatal: Cannot find 'master~5..master~2'

+11
git
Jan 17 '11 at 17:35
source share
2 answers

git revert should accept one commit, so the error complains that you give it a list of changes where it expects it. (Update: thanks to Jefromi for the fact that, since 1.7.2, git revert can actually accept multiple commits.) I suggest three possible ways to proceed below:

Create multiple return commits

I'm not sure that you can do this in one command, but if you know that db0bc is the last good commit (i.e. you want to return every commit after it), and the history will be linear, you can do

 for s in $(git rev-list --reverse db0bc..) do git revert --no-edit $s done 

Going back to the old state, but still on the same branch

On the other hand, if you just make sure that the story is there, you could always make the next commit with the state of the source tree back with the last good commit of db0bc without introducing return commits - you still have a story that was entered between you and yours new commit, but without a lot of retries between them. In addition, this will not have problems if the story is non-linear:

 # Check that "git status" is clean: git status # Set the index (staging area) to be as it was at commit db0bc: git read-tree db0bc # Create a commit based on that index: git commit -m "Going back to the state at commit db0bc" # Your working tree will still be as it was when you started, so # you'll want to reset that to the new commit: git reset --hard 

Create a new branch for old commits

If you are the only person working on the project, or you have not yet clicked the wizard, or you know that resetting the main branch will not cause problems, you can use the alternative line and create a new branch in your current position, and then reset back, as described on github "undoing" page . In short, you can do:

 # Make sure you're on master: git checkout master # Check that "git status" is clean, since later you'll be using "git reset --hard": git status # Create a new branch based on your current HEAD: git branch unwanted-work # Reset master back to the last good commit: git reset --hard db0bc 

The diagrams on this github page make it nice and clear, I think.

+21
Jan 17 '11 at 18:19
source share
— -

IMHO the most natural approach is to do (if you have a clean working copy):

git reset --hard $lastnicecommit; git merge -s ours @{1}

and then git commit --amend to add a description. (In newer versions of git, git-merge already allows you to specify a description.)

( @{1} only refers to committing your branch specified before reset.)

This makes a quick switch and therefore fully documents the story. However, changes made to commits with $lastnicecommit do not make any changes.

+8
Jan 17 '11 at 19:37
source share



All Articles