How to Move Commit to Another Branch in Git?
Overview
Branching is a central concept in the Git workflow, allowing multiple developers to work on a single project simultaneously in a distributed environment. Branching in Git helps developers work independently while maintaining stability. A developer can work on any feature of the software by creating different branches and finally merging all the branches to get the resulting software product. In this article, we'll show you how to move a commit from one branch to another branch and have the commit's changes propagate to that branch.
Introduction
As we know that each branch is an isolated independent line of development and each branch can have multiple commits having the code changes for that branch. Branching is primarily useful for fixing bugs without changing the major version of the project until the bug is properly fixed. It's also often used when implementing functionality to keep those features out of the main project until they're ready for release.
However, you can move commits from one branch to another if you want your changes to be reflected on a different branch than the one you pushed the changes to. After that, that commit will no longer appear in the original branch's history. This can happen in a situation where you have done some work and need to commit to a specific branch, but after committing your changes you realize that your commit was accidentally committed to the wrong branch. In situations like this, you should move the commit to another branch. Let's see a workaround to fix this by moving the commits to another branch.
How to Move a Commit to Another Branch in Git?
Let's suppose you have a testing_folder inside which you have an index.cpp file.
Initially, there is only one master branch. Let's verify the comment history by using the git log command in our master branch:
Doing this for a New Branch
Now let's say you want to transfer some of the commits you made on the master branch to a newly created branch called a dummy branch. This is likely because the commit was part of some sequence of commits to a feature that wasn't still released yet, or you accidentally committed changes to the wrong branch.
To move commits to a newly created branch, you first need to create a new branch:
In our case, you can run the following command by replacing the branch name with dummy:
This will create a new branch named dummy having all the code changes currently present on the original master branch.
Now, we have created the new branch and now we can move the master branch backward by 1 commit so that it no longer contains the code we wrote for the new dummy branch or the commit which we wrongly committed. To remove the commit we need to reset the branch state using the below command:
This will revert the master branch to the code state it was before we committed the top-most commit. You can provide any number in the above command (which represents the number of commits you want to revert back). Alternatively, if you don't know how many commits to push backward compared to the present commit state of the Git repository, you can specify a hash of specific commits which you want to move.
Now you can check your logs to see that you have removed the latest commit from the master branch:
As you can see in the image above, the last commit has been deleted. Now, you can switch to a new branch to see the state of the repository containing the features we just moved:
You can check the logs of the dummy branch:
As you can see, the new branch has the required commit, but the master branch currently does not have that commit, so the commit was successfully moved to the new branch.
Doing this for an Existing Branch
Now let's see how to move commits to an existing branch. First, let's check the commit histories of the two branches, master and dummy.
Our master branch has one commit and the dummy branch has two commits.
Now, let's add a few more commits to the master branch and see the scenario.
After adding a few more files to the folder and committing them, let's check the current status of the master branch:
The existing dummy branch still looks like this:
Now, we are going to combine all the code changes we have made to the original master branch into the existing dummy branch. This will make sure that the code changes in both the branches are same:
Now, let's look at the state of the dummy branch:
It has all its commits along with the code changes of the master branch, now you can go back to master and revert your master to the commit before you pushed your changes with the git reset command:
We have used the reset command with the number 2, as we have done our task of moving those commits to our required dummy branch and now we can remove those two commits.
So the new commit history for the master branch looks like this:
This means that a new dummy branch will contain the code for your changes. The master branch looks just like it did before you made this commit and pushed it to the master branch.
Now let's assume the case where you don't want to move a set of commits and rather just want to move a specific commit. We've seen that the branch merge option merges all commits, but if you only want to move a specific commit to a dummy branch and not move any other commits after it that exist in the master branch's sequence, you can use the cherry-pick command in git.
This command will select that one commit specified by commit id and apply it to the branch you plan to move to, the command for that is:
where commit_id is a unique id associated with each commit and is automatically generated at the commit time. A commit ID is an encrypted number generated using the Secure Hash Algorithm (also known as SHA). You can use the Git log command to view the history of commits for a particular repository. Let's see an example of how we can find the commit id using the git log command:
The output of the above command returns a list of commits in the Git repository history as shown below:
where this alphanumeric string at the beginning represents the commit ID of a particular commit.
Conclusion
- Branching in Git allows developers to work in an isolated manner and provides a different version of project history.
- You can use branches to deviate from your main production branch, making it easier to work in an isolated and stable environment.
- Branching is primarily used when multiple developers are working on the same project, each working on independent features of the software in their local branch. Then you can merge changes from all branches to get the complete software.
- In this article we saw the steps to Git move commit to another branch.
- You can move one or many commits from one branch to another branch if you want your changes on one branch to be shown on a different branch.
- You can move a commit to another branch, whether it's a new branch or an existing branch.
- git reset --hard HEAD~1 command is used to revert the branch to the state it was in before the last commit.
- git log command returns a list of commits in the Git repository.
- git cherry-pick <commit_id> command will select one commit specified by commit id from one branch and apply it to another branch you plan to move to.
- If you only want to move a specific commit to a new branch and not move any other commits after it that exist in the branch's sequence, then you can use the cherry-pick command in git.
- A commit_id is a unique id associated with each commit and is automatically generated at the time of commit.
- A commit ID is an encrypted number generated using the Secure Hash Algorithm (also known as SHA).
- You can use the Git log command to view the history of commits for a particular repository.
- The need of moving commits might come in a situation where you have done some work and need to commit to a specific branch, but after committing your changes you realize that your commit was accidentally committed to the wrong branch.