How to Move the Most Recent Commit(s) to a New Branch With Git?
Last Updated :
31 May, 2024
Git offers powerful features for managing your codebase efficiently. Sometimes, you may find yourself needing to move the most recent commit(s) to a new branch in Git. This could be due to various reasons, such as realizing that changes were made on the wrong branch or wanting to isolate experimental work. Fortunately, Git provides simple ways to accomplish this task. In this article, we’ll explore how to move the most recent commit(s) to a new branch with Git.
Why Move Commits to a New Branch?
Before diving into the steps, let’s briefly discuss why you might want to move commits to a new branch:
- Correcting Mistakes: You may have accidentally committed changes to the wrong branch and need to move them to the correct one.
- Isolating Changes: You might start working on a feature and realize it’s better suited as a separate branch for better organization and testing.
- Collaborative Workflow: Moving commits can help maintain a clean and organized workflow, especially in collaborative environments where each branch represents a specific task or feature.
Some terminological commands to do the task:
- HEAD: Head pointer is the pointer that points to the most recent commit, which is reflected in the working tree. It stands for the currently checked out commit we’re working on.
- git commit -m <message>: The current contents or changes in the repository are recorded through git committing. After git add command, all the updated files reach the staging area, after which git commit fetch the changes to the repository with the commit message.
- git branch <branch>: This command creates a new branch. This branch is nothing but a pointer to the snapshot of our changes from our main repository.
- Relative references: Specifying commits by their hashes is a tedious job. So we use relative commits. One of them is ^ which means moving upwards one commit and ~<num> means moving upwards a number of times.
- git hard reset to head: The purpose of the ‘git reset’ command is to move the current head to the commit specified. The –hard option is used in order to reset the files of the index or the staging area and the changes in those files are lost, thus untracking them.
- git checkout <branch>: The Git checkout command lets us navigate between the branches created by git branch. Checking out a branch updates the files in the working directory to match the version stored in that branch.

Steps to Move Recent Commits to a New Branch
Step 1: Check Current Status
Before making any changes, it’s a good practice to check the current status of your repository. Open your terminal or command prompt and navigate to the repository where you want to move the commits. Use the following command to view the current status:
git status
Step 2: Create a New Branch
Next, create a new branch where you want to move the recent commit(s). Use the following command to create a new branch and switch to it:
git checkout -b new-branch-name
Replace new-branch-name with the desired name for your new branch.
Step 3: Move the Commits
Now, let’s move the most recent commit(s) to the newly created branch. There are a couple of ways to achieve this, depending on whether you want to move just the most recent commit or multiple recent commits.
Moving the Most Recent Commit
If you only need to move the most recent commit, you can use the git reset command. Switch back to the original branch:
git checkout original-branch-name
Then, use the following command to reset the original branch to the previous commit, effectively removing the latest commit from this branch:
git reset --hard HEAD~1
Now, switch back to the new branch:
git checkout new-branch-name
And cherry-pick the removed commit onto the new branch:
git cherry-pick original-branch-name
Moving Multiple Recent Commits
If you need to move more than one recent commit, you can adjust the reset command to match the number of commits. For example, to move the last three commits:
git checkout original-branch-name
git reset --hard HEAD~3
git checkout new-branch-name
git cherry-pick original-branch-name..HEAD@{1}
In this command, HEAD~3 indicates the last three commits, and HEAD@{1} refers to the state of the branch before the reset.
Step 4: Verify Changes
After moving the commits, it’s essential to verify that everything is in order. Use the git log command on both branches to ensure the commits have been moved correctly:
git log
Check the log for both the original and new branches to confirm the commits are where they should be.
Similar Reads
How To Find The Most Recent Common Ancestor of Two Branches in Git?
In software development, managing code changes through version control systems is crucial. Git, one of the most popular version control systems, allows developers to create branches for features, bug fixes, or experiments. These branches can diverge from the main codebase and evolve independently. E
3 min read
How to Restore a Deleted Branch or Commit with Git?
Deleting a branch or a commit in Git might seem like a disaster, especially if it was done unintentionally. However, Git provides robust tools to help you recover deleted branches or commits. Hereâs a detailed guide on how to restore a deleted branch or commit in Git. Table of Content Understanding
3 min read
How To Move Tag On a Git Branch To Different Commit?
Git is a powerful version control system that allows you to manage and track changes in your codebase. One useful feature of Git is tagging, which allows you to mark specific points in your project's history as important. Sometimes, you might need to move a tag from one commit to another, perhaps du
2 min read
How to Get List of Git Branches, Ordered By Most Recent Commit?
Git is a widely used version control system that helps manage code changes across different branches. Sometimes, you may need to see a list of branches sorted by the most recent commit. This can be useful for understanding the current state of development and prioritizing which branches need attenti
3 min read
How To Move Branch Pointer To Different Commit Without Checkout?
Git pros frequently utilize this strong approach to rebase branches, correct errors, and modify branch histories: shifting the branch pointer to a different commit without checking out. With this approach, you can modify the commit that a branch points to without affecting the status of your working
1 min read
How To Revert A Commit With Git Revert?
Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. It is free, open-source, and used by almost 95% of developers across the worl
5 min read
How to Undo the Most Recent Local Commits in Git?
When working with Git, it's common to make mistakes or realize that recent commits need to be revised. Whether you've made an error in your code, committed sensitive information, or simply want to restructure your commits, knowing how to undo the most recent local commits is an essential skill. This
3 min read
How To Revert The Last Merge Commit in Git?
Managing a Git repository often involves merging branches. Sometimes, after completing a merge, you might realize that the merge was a mistake or that it introduced issues. Fortunately, Git provides a simple way to revert the last merge commit. In this article, we will walk through the steps to safe
4 min read
How To Reset Remote Repository to a Certain Commit in Git?
Resetting a remote repository to a specific commit in Git can be an important task, especially when you need to revert changes or roll back to a stable state. This article will guide you on how To Reset Remote Repository to a Certain Commit in Git. Table of Content Approach 1: Using `git reset` and
2 min read
How to Move Existing, Uncommitted Work to a New Branch in Git?
Git is a widely used distributed version control and source code management system. A common scenario developers face is needing to move their uncommitted work to a new branch. This can be important for maintaining organized workflows, especially when realizing that ongoing changes belong to a separ
2 min read