Commits are a snapshot of our repository at any instant in time. Sometimes we may commit changes to the wrong branch. Or we may forget to create a new branch before adding commits. We need to move these commits to their correct location. Let's learn how to move the most recent commits from one branch to another.
The Git Reset command rewrites the history of our project. We can use this command to move a branch back to an older commit.
To understand the entire process, let's take an example. Consider a repository with two branches - master and feature. We accidentally added two commits(D and E) to the master branch. We need to move these commits to the feature branch.
A -- B -- C -- D -- E <- master <- HEAD
↑
feature
First, we need to update the feature branch with the two new commits. Check out the feature branch(using the Git Checkout command) and merge it with the master branch.
git checkout feature
git merge master
A simple fast-forward merge takes place. The state of our repository is shown below.
A -- B -- C -- D -- E <- master
↑
feature <- HEAD
Next, we need to move the master branch back to commit C. Checkout the master branch and use the Git Reset --hard command to go back to commit C. We need to pass a reference to commit C. We can either use HEAD~N notation. Or we can pass the hash of commit C.
Note that we will lose any uncommitted changes. Use the Git Stash command to store(or stash) the uncommitted changes.
git checkout master
git reset --hard HEAD~2
git reset --hard <hash-of-commit-C> # equivalent to above command
Finally, we can move to the feature branch and keep adding new commits at the correct location.
git checkout feature
A -- B -- C -- D -- E <- feature <- HEAD
↑
master
In the above scenario, we had an existing branch called feature. Now, let's suppose we don't have that branch.
A -- B -- C -- D -- E <- master <- HEAD
So, first, we need to create a new feature branch. This feature branch will point to the same commit as the master branch, so we don't need to merge them. Use the Git Branch command to create a new branch.
git branch feature # creating the feature branch
A -- B -- C -- D -- E <- master <- HEAD
↑
feature
Now, we just need to reset the master branch to commit C.
git reset --hard HEAD~2
git reset --hard <hash-of-commit-C> # equivalent to above command
Finally, we can checkout the feature branch and keep adding new commits.
git checkout feature
A -- B -- C -- D -- E <- feature <- HEAD
↑
master
The Git Cherry-pick command selects commits from one branch and adds them to another branch. It will not remove the commits from the original branch and only adds a copy of the commits to the destination branch. Let's learn how to move commits using the Git Cherry-pick command.
Consider a repository with two branches - master and feature. We accidentally added two commits(D and E) to the master branch. We need to move these commits to the feature branch.
A -- B -- C -- D -- E <- master <- HEAD
↑
feature
The Git Cherry-pick command needs the hashes of the commits that we want to move. First, run the Git Log command to find out the hash of commit D and commit E.
git log
Next, check out the feature branch by using the Git Checkout command.
git checkout feature
Now, let's use the Git Cherry-pick command. We need to pass the hash of commit D and commit E. Make sure you follow the correct order of passing the hashes(first commit D, then commit E).
git cherry-pick <hash-of-commit-D> <hash-of-commit-E>
A -- B -- C -- D -- E <- master
\
\
D' -- E' <- feature <- HEAD
As we can see, a copy of commit D and commit E(D' and E') are added to the feature branch. Also, note that the original commits D and E are still present on the feature branch. We need to reset the master branch to commit C.
Check out the master branch and use the Git Reset command to take the master branch back to commit C.
git checkout master
git reset --hard <hash-of-commit-C>
Finally, we can check out the feature branch and add new commits to it.
git checkout feature
A -- B -- C <- master
\
\
D' -- E' <- feature <- HEAD
Sometimes, we may add commits to the wrong branch or forget to create new branches. Moving commits from one branch to another is a complex process. We can use the Git Merge command or the Git Cherry-pick command to add the commits to the right branch. Next, we need to use the Git Reset command to remove the commits from the wrong branch. If we have pushed the branch to a remote repository, use the Git Revert command in place of Git Reset.