NeuroAgent

How to Move Commits to New Branch in Git: Complete Guide

Learn how to move recent commits from master to a new branch and reset master to its previous state in Git. Complete guide with multiple methods and safety tips.

Question

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?

NeuroAgent

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

The Git workflow you’re looking to accomplish involves two main operations:

  1. Creating a new branch that will contain the recent commits you want to move
  2. 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 HEAD
  • git reset --hard HEAD~<n> - moves the branch pointer back n commits and discards changes
  • git 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:

  1. Create a new branch at the current HEAD:

    bash
    git checkout master
    git branch feature-branch
    
  2. 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:

    bash
    git 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:

bash
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:

  1. Create and checkout the new branch:

    bash
    git checkout -b feature-branch
    
  2. Cherry-pick the specific commits you want to move:

    bash
    git cherry-pick C..E  # Moves commits C, D, and E to the new branch
    
  3. Reset master back to the previous state:

    bash
    git 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:

  1. Create the new branch:

    bash
    git checkout -b feature-branch
    
  2. Rebase onto the target commit:

    bash
    git rebase --onto B C..E
    
  3. Reset master back:

    bash
    git 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:

  1. Create the new branch at the current HEAD (E):

    bash
    git checkout master
    git branch feature-branch
    
  2. Reset master back to commit B (2 commits back):

    bash
    git checkout master
    git reset --hard HEAD~2
    
  3. Switch to the new branch to continue working:

    bash
    git 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 log to verify you’re resetting to the correct commit
  • Consider using --keep: If you’re not sure about losing changes, use git reset --keep instead 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:

bash
# 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:

  1. Basic approach: Use git branch to create the new branch and git reset --hard HEAD~n to reset master back
  2. Cherry-pick method: Use git cherry-pick for more granular control over which commits to move
  3. Rebase method: Use git rebase --onto for automated series movement

The key commands are:

  • git branch <branch-name> - creates new branch
  • git reset --hard HEAD~<n> - resets branch back n commits
  • git checkout <branch-name> - switches to the new branch

Always remember to:

  • Verify your commit history before resetting
  • Be careful with --hard resets 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

  1. Move the most recent commit(s) to a new branch with Git - Stack Overflow
  2. Move Git commits from master to a new branch - 30 seconds of code
  3. How to Move the Most Recent Commit(s) to a New Branch With Git? - GeeksforGeeks
  4. Moving commits between branches · GitHub
  5. Move the most recent commits to a new branch with git | Sarunw
  6. How to move a commit to another branch in Git - Graphite
  7. Git - git-reset Documentation
  8. How to Make the Development Branch Identical to the Master Branch | Delft Stack