When working on a project, it’s common to realize that some recent commits should be moved to a new branch. This can happen when you’ve made changes that don’t belong in the current branch or when you want to create a separate feature branch. In this tutorial, we’ll explore how to move recent commits to a new branch using Git.
Understanding Git Branching
Before diving into the process, let’s briefly review how Git branching works. A branch in Git is simply a pointer to a commit. When you create a new branch, you’re creating a new pointer that points to the same commit as the current branch. As you make changes and commit them, the branch pointer moves forward.
Moving Commits to a New Branch
To move recent commits to a new branch, you’ll need to follow these steps:
- Create a new branch: First, create a new branch that will contain the commits you want to move. You can do this using
git branch <new-branch-name>. - Reset the current branch: Next, reset the current branch (usually
master) to the commit before the ones you want to move. You can usegit reset --hard HEAD~<number-of-commits>to achieve this. - Checkout the new branch: After resetting the current branch, checkout the new branch using
git checkout <new-branch-name>. - Verify the commits: Finally, verify that the commits have been moved successfully by checking the commit history using
git log.
Example Use Case
Let’s say you’re working on a project and you’ve made three commits (C, D, and E) to the master branch. However, you realize that these commits should be part of a new feature branch. To move these commits to a new branch, you would follow these steps:
# Create a new branch
git branch new-feature
# Reset the master branch to before the commits
git checkout master
git reset --hard HEAD~3
# Checkout the new branch
git checkout new-feature
After completing these steps, the new-feature branch will contain the commits C, D, and E, while the master branch will be reset to before these commits.
Alternative Method Using Cherry-Pick
Another way to move commits to a new branch is by using git cherry-pick. This method involves applying the commits to the new branch individually. Here’s an example:
# Checkout the new branch
git checkout new-feature
# Cherry-pick the commits
git cherry-pick <commit-hash-1>
git cherry-pick <commit-hash-2>
git cherry-pick <commit-hash-3>
Replace <commit-hash-1>, <commit-hash-2>, and <commit-hash-3> with the actual commit hashes you want to move.
Best Practices
When moving commits to a new branch, keep in mind the following best practices:
- Always verify that the commits have been moved successfully by checking the commit history.
- Use
git reset --hardwith caution, as it will delete any uncommitted changes. - Consider using
git cherry-pickinstead ofgit reset --hardif you need to move a large number of commits.
By following these steps and best practices, you can easily move recent commits to a new branch using Git. This helps keep your project organized and makes it easier to manage different features or tasks.