Question

Is there a way to rollback the last commit and put it into a separate branch for later testing? I did some changes which I don't want to entirely throw away, I just want to keep them aside in a different branch for further testing.

Can anyone help me with this?

Was it helpful?

Solution

Yes you can achieve this - branch away from the current branch and create a new branch to preserve the commit, checkout back to the original branch, and then rollback the commit in the original branch.

So from your current branch, (lets call it current), create and checkout a new branch separate

git checkout -b separate

This will create a new branch separate which will have the new commit. Now go back to the original branch

git checkout current

On this branch, you can now rollback the last commit

git reset --hard HEAD~1

If you later want to access that older commit, you have to do a git checkout separate and the commit should be available in that branch.

OTHER TIPS

You can do this in two steps and without switching between branches. Here we go.

  1. Create a new branch from the current branch, to set aside your current state:

    git branch feature_maybe
    
  2. Revert the last commit in the current branch:

    git reset --hard HEAD^
    

Yes - check out a new branch using git checkout -b <new branch name>, switch to the original branch using git checkout <original branch name>, then do git reset --hard HEAD~1 to move the original branch back a commit. (As always when moving branches around, it's safest to visualise what you're doing step-by-step using a program like gitk.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top