How do I move recent commits from the master branch to a new branch and reset master to its previous state? For example, transform this Git history:
master A - B - C - D - E
Into this:
newbranch C - D - E
/
master A - B
What are the Git commands to accomplish this workflow?
To move recent commits from the master branch to a new branch and reset master to its previous state, you need to create a new branch at the current HEAD and then reset the master branch back to the desired commit. The basic workflow involves using git branch to create the new branch and git reset --hard or git reset --keep to reset master back.
Contents
- Understanding the Workflow
- Method 1: Using Git Branch and Reset
- Method 2: Using Git Cherry-Pick
- Method 3: Using Git Rebase
- Step-by-Step Example
- Important Considerations
- Handling Remote Repositories
- Conclusion
Understanding the Workflow
The Git workflow you’re looking to accomplish involves two main operations:
- Creating a new branch that will contain the recent commits you want to move
- Resetting the master branch back to its previous state before those commits
As explained in the 30 seconds of code guide, this process is particularly useful when you realize that certain commits should have been made on a separate branch instead of the current branch.
The key commands you’ll need are:
git branch <branch-name>- creates a new branch at the current HEADgit reset --hard HEAD~<n>- moves the branch pointer back n commits and discards changesgit reset --keep HEAD~<n>- moves the branch pointer back but keeps changes in working directory
Method 1: Using Git Branch and Reset
This is the most straightforward approach and recommended for most scenarios.
Step-by-step process:
-
Create a new branch at the current HEAD:
bashgit checkout master git branch feature-branch
-
Reset master back to the previous state. If you want to keep the last 3 commits (C, D, E) on the new branch and reset master to B:
bashgit checkout master git reset --hard HEAD~2 # This moves master back 2 commits (to commit B)
As the GeeksforGeeks guide explains, you can use the git reset command to move commits to a new branch. The --hard option will reset both the branch pointer and the working directory.
Alternative with --keep:
If you want to reset the branch pointer but keep the changes in your working directory:
git reset --keep HEAD~2
Method 2: Using Git Cherry-Pick
This method is useful when you want more control over which commits to move.
Step-by-step process:
-
Create and checkout the new branch:
bashgit checkout -b feature-branch
-
Cherry-pick the specific commits you want to move:
bashgit cherry-pick C..E # Moves commits C, D, and E to the new branch -
Reset master back to the previous state:
bashgit checkout master git reset --hard C~1 # Resets master to commit B
As mentioned in the Graphite guide, cherry-picking can be useful when you want more granular control over which commits to move between branches.
Method 3: Using Git Rebase
This approach is useful for moving a series of commits in a more automated fashion.
Step-by-step process:
-
Create the new branch:
bashgit checkout -b feature-branch
-
Rebase onto the target commit:
bashgit rebase --onto B C..E
-
Reset master back:
bashgit checkout master git reset --hard C~1 # Resets master to commit B
The Graphite guide notes that rebase can be useful if you want to move a series of commits from one branch to another in a more automated fashion.
Step-by-Step Example
Let’s walk through the complete workflow using the example from your question:
Initial state:
master A - B - C - D - E
Final desired state:
newbranch C - D - E
/
master A - B
Complete commands:
-
Create the new branch at the current HEAD (E):
bashgit checkout master git branch feature-branch
-
Reset master back to commit B (2 commits back):
bashgit checkout master git reset --hard HEAD~2
-
Switch to the new branch to continue working:
bashgit checkout feature-branch
As the Stack Overflow answer explains, if you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3. However, in our case, we’re creating a new branch first.
Important Considerations
Safety First:
- Backup your work: Before performing any reset operations, make sure you have no uncommitted changes that you might need
- Check the commit history: Use
git logto verify you’re resetting to the correct commit - Consider using --keep: If you’re not sure about losing changes, use
git reset --keepinstead of--hard
Common Pitfalls:
- Don’t reset shared branches: Never reset a branch that other team members are working on
- Force push carefully: After resetting a remote branch, you’ll need to force push
- Verify before committing: Double-check that you’re on the correct branch before making new commits
As the Sarunw guide warns, to remove wrong commits from the develop branch, we use git reset, so be very careful about what you’re resetting.
Handling Remote Repositories
If you’re working with a remote repository, you’ll need to update the remote branch after resetting:
# After resetting master locally
git push --force origin master
As mentioned in the Graphite guide, if you are working on a remote repository and need to update the branches, you may need to force push the changes.
Warning: Force pushing will overwrite the remote history, so make sure all team members are aware of this change and have pulled the latest changes before you force push.
Conclusion
Moving recent commits from master to a new branch and resetting master is a common Git workflow that can be accomplished using several approaches:
- Basic approach: Use
git branchto create the new branch andgit reset --hard HEAD~nto reset master back - Cherry-pick method: Use
git cherry-pickfor more granular control over which commits to move - Rebase method: Use
git rebase --ontofor automated series movement
The key commands are:
git branch <branch-name>- creates new branchgit reset --hard HEAD~<n>- resets branch back n commitsgit checkout <branch-name>- switches to the new branch
Always remember to:
- Verify your commit history before resetting
- Be careful with
--hardresets as they discard changes - Consider team collaboration when working on shared repositories
- Force push carefully when updating remote branches
This workflow is particularly useful when you realize that certain commits should have been made on a separate branch instead of the current branch, helping maintain clean and organized Git history.
Sources
- Move the most recent commit(s) to a new branch with Git - Stack Overflow
- Move Git commits from master to a new branch - 30 seconds of code
- How to Move the Most Recent Commit(s) to a New Branch With Git? - GeeksforGeeks
- Moving commits between branches · GitHub
- Move the most recent commits to a new branch with git | Sarunw
- How to move a commit to another branch in Git - Graphite
- Git - git-reset Documentation
- How to Make the Development Branch Identical to the Master Branch | Delft Stack