Return git last commit and save it in another branch

Is there a way to undo the last commit and put it on a separate branch for later testing? I made some changes that I do not want to completely throw away, I just want to postpone them in another branch for further testing.

Can anyone help me with this?

+4
source share
3 answers

Yes, you can achieve this - separate from the current branch and create a new branch to save the commit, return to the original branch, and then roll back the commit in the original branch.

, ( current), separate

git checkout -b separate

separate, .

git checkout current

git reset --hard HEAD~1

, git checkout separate, .

+8

. .

  • , :

    git branch feature_maybe
    
  • :

    git reset --hard HEAD^
    
+3

Yes - check the new branch with git checkout -b <new branch name>, switch to the original branch with git checkout <original branch name>, then execute git reset --hard HEAD~1to transfer the original branch to commit. (As always when moving branches, it’s safe to visualize what you are doing step by step using a program such as gitk.)

0
source

All Articles