Explore Courses Blog Tutorials Interview Questions
+5 votes
1 view
in DevOps and Agile by (19.4k points)
edited by

I'd like to move the last several commits I've committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not strong enough yet, any help?

I.e. How can I go from this

master A - B - C - D - E

to this?

newbranch      C - D - E
master  A - B

1 Answer

+6 votes
by (27.5k points)
edited by

Move the commit to a new branch:

The following commands will create a new branch called ‘feature’, then hard resets staging and working tree the existing branches HEAD to the previous commit.

git branch feature

git reset --hard HEAD~1   #or commit SHA1

Move the commit to an existing branch:

Merge new commits in first and then follow the steps given below:

git checkout existingbranch

git merge master

git checkout master

git reset --hard HEAD~2  #Go back 2 commits, Note that You will lose uncommitted work.

git checkout existingbranch

For more commands like this please go through the following tutorial that will help you understand the git


Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories