Code in Git projects is stored in branches. Each branch is an independent line of development in which you can make changes to files.
You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.
In this guide, we discuss how to move a commit to a different branch. We walk through two methods of moving a commit to a different branch: moving to an existing branch, and a new branch.
Git Branching: A Refresher
Branches let you work on two different versions of a project at the same time.
This is useful if you want to fix a bug without changing the main version of a project until that bug is properly fixed. Branches are commonly used when implementing features to keep those features away from the main project until they are ready to be released.
You can move a commit to another branch using the Git command line.
Git: Move a Commit to a New Branch
We’re working on a project and we want to work on a new feature.
We want to move a commit we’ve made to the master branch to a new branch called new-feed. This is because the commit will likely be part of a series of commits about a feature and the feature is not ready for a wide release.
To move the commit to a new branch, we must first create a new branch:
git branch new-feed
This command creates a branch containing all the code currently on the “master” branch.
Now that we have a new branch, we can move our master branch back by one commit so it does not contain the code we’ve written toward the new feature:
git reset --hard HEAD~1
We are resetting the master branch to the state it was in before the last commit. We can change the number 1 to reflect any number of commits we want to move back.
Alternatively, you can specify the hash of a commit if you are not sure how many commits you want to move back relative to the current state of the repository.
The next step is to checkout to the new branch to see the version of our project with the features we’ve written:
git checkout new-feed
Our commit has been moved to the new branch.
Git: Move a Commit to an Existing Branch
You can move a commit to an existing branch. To start, navigate to the branch to which we want to move our commit using the git checkout command:
git checkout new-feed-2
We are now viewing the new-feed-2 branch. Next, we’re going to merge all of the changes we have made to the master branch into the new-feed-2 branch. This will ensure both branches contain the same code:
git merge master
Now that the new-feed-2 branch contains our changes, we can move back to master and reset our repository to the commit before the one where we pushed our changes:
git checkout master git reset --hard HEAD~1
These commands move our master branch back by one commit. This means that our new branch, new-feed-2, contains the code from our changes. The master branch appears in the way it did before we created that commit and pushed it to the master branch.
You can move a commit to another branch, whether it’s a new branch or an existing one. This is because branches let you maintain independent versions of a project. A new branch contains all of the history of an existing branch and then can create its own history.
Now you’re ready to move commits to another branch in Git like a professional developer.