How to Move Recent Commits to a New Branch in Git

Quick Answers: Moving Commits to a New Branch

  1. Create a New Branch with Recent Commits:
   git branch new-branch
  1. Reset the Current Branch to Remove Commits:
   git reset --hard HEAD~n

Replace n with the number of commits to move.

  1. Switch to the New Branch:
   git checkout new-branch
  1. Verify the Commits:
   git log --oneline

Introduction

Sometimes in Git, you realize that your recent commits belong on a separate branch—perhaps you started working on a feature or bug fix directly on main or another branch by mistake. Git makes it easy to move those commits to a new branch without losing your work. In this guide, we’ll walk you through the steps to move the most recent commits to a new branch, with clear examples and best practices to keep your repository organized. Whether you’re a Git beginner or refining your workflow, this post will help you manage your commits like a pro.

Why Move Commits to a New Branch?

You might need to move commits to a new branch to:

  • Organize work: Separate feature development or bug fixes from the main branch.
  • Correct mistakes: Fix commits made on the wrong branch (e.g., main instead of a feature branch).
  • Collaborate effectively: Create a dedicated branch for code reviews or pull requests.
  • Maintain a clean history: Keep the main branch stable and focused on production-ready code.

This process ensures your commits are in the right place without duplicating or losing changes.

Detailed Explanation: Moving Recent Commits to a New Branch

Assume you’ve made n commits (e.g., 3) on the current branch (e.g., main) that should be on a new branch. Follow these steps:

Step 1: Check the Commit History

Verify the commits you want to move.

  • Command:
  git log --oneline
  • Example Output:
  abc123 Fix login bug
  def456 Add user profile page
  ghi789 Update styles
  jkl012 Initial commit

Here, the top 3 commits (abc123, def456, ghi789) need to move to a new branch.

Step 2: Create a New Branch

Create a new branch that includes all commits from the current branch.

  • Command:
  git branch feature-branch
  • What it does:
  • Creates feature-branch pointing to the same commit as main (e.g., abc123).
  • Keeps you on the current branch (main).
  • Example:
  git branch feature-login
Step 3: Reset the Current Branch

Remove the recent commits from the current branch by resetting it to an earlier commit.

  • Command:
  git reset --hard HEAD~n

Replace n with the number of commits to move (e.g., 3).

  • Example:
  git reset --hard HEAD~3
  • What it does:
  • Moves main back 3 commits (to jkl012 in the example).
  • Discards the last 3 commits from main’s history, but they remain in feature-branch.
  • Caution: --hard discards uncommitted changes. Stash or commit them first:
  git add .
  git stash
Step 4: Switch to the New Branch

Move to the new branch to continue working or push the commits.

  • Command:
  git checkout feature-branch
  • Example:
  git checkout feature-login
Step 5: Verify the Commits

Confirm the new branch has the moved commits and the original branch is reset.

  • On the New Branch:
  git log --oneline

Output:

  abc123 Fix login bug
  def456 Add user profile page
  ghi789 Update styles
  jkl012 Initial commit
  • On the Original Branch:
  git checkout main
  git log --oneline

Output:

  jkl012 Initial commit
Step 6: Push the New Branch (Optional)

If you need to share the new branch with a remote repository:

  • Command:
  git push origin feature-branch
  • Example:
  git push origin feature-login

Alternative Approach: Using git cherry-pick

If you’ve already pushed the commits to the remote main branch or want to move specific commits, you can use cherry-pick:

  1. Create and switch to the new branch:
   git checkout -b feature-branch
  1. Cherry-pick the desired commits:
   git cherry-pick <commit-hash>

Example:

   git cherry-pick abc123 def456 ghi789
  1. Reset the original branch:
   git checkout main
   git reset --hard HEAD~3
  1. Force-push if already pushed:
   git push origin main --force

Caution: Coordinate with your team, as force-pushing rewrites history.

Best Practices for Moving Commits

  • Check the log first: Use git log --oneline to confirm which commits to move.
  • Backup your work: Create a backup branch before resetting:
  git branch backup-main
  • Communicate with your team: If the original branch is shared, warn collaborators before resetting or force-pushing.
  • Use descriptive branch names: Name branches clearly (e.g., feature-login, bugfix-auth) to reflect their purpose.
  • Stash uncommitted changes: Save uncommitted work with git stash to avoid losing it during reset --hard.
  • Test after moving: Verify the new branch has all expected changes and the original branch is correctly reset.

Common Pitfalls and How to Avoid Them

  • Losing uncommitted changes: Always commit or stash changes before git reset --hard.
  • Resetting too many commits: Double-check n in HEAD~n to avoid removing unintended commits.
  • Force-pushing shared branches: Avoid force-pushing to shared branches like main without team coordination.
  • Incorrect branch state: Use git log on both branches to ensure commits are where you expect.
  • Cherry-pick conflicts: Resolve conflicts during cherry-pick carefully, using git cherry-pick --continue or --abort.

Conclusion

Moving recent commits to a new branch in Git is a straightforward process using git branch, git reset, and git checkout. By creating a new branch to preserve your commits and resetting the original branch, you can reorganize your work without losing changes. For more complex scenarios, cherry-pick offers flexibility. With these steps and best practices, you’ll keep your Git repository clean and your workflow efficient, whether you’re fixing a mistake or preparing a feature for review.

Got a Git question or a branch management tip? Share it in the comments or explore our Git tutorials for more version control insights!

Call to Action

Loved this guide? Subscribe to our newsletter for more Git tips and tricks, or check out our version control resources to level up your skills. Let’s keep your Git branches organized and productive!


Previous Article

How to Change the URL for a Remote Git Repository

Next Article

Is There an Unsafe Way to Forcefully Kill a Thread in Java 20+?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨