There are cases when after committing on a particular branch you realize that you want to commit on another one. In such occasions, Git comes to the rescue allowing to move your commit to another existing branch or a new one.
Below, we will show you how to do it.
Here, we will discuss the scenario of moving the current state of your branch to a new branch. Follow the steps below, to do that.
Create a new branch, containing all the current commits using the git branch command, which, on contrast to git checkout -b doesn't switch to the newly created branch:
git branch <new-branch-name>
Move the current branch back by the number of the mistaken commits:
git reset --keep HEAD~N
Alternatively, instead of HEAD~N, you can use the hash of the commit you want to revert back:
git reset --keep <sha1-commit-hash>
So, your recent mistaken commits have been moved to a new branch.
Here, we will discuss a scenario of working on a feature on a particular branch but making a commit on another one, by mistake. Now, the task is to bring back the commit to the right branch. Suppose that the two branches are synchronized.
Check out the existing branch with the git checkout command:
git checkout <existing-branch>
Run the git merge command to bring back the commits to the wrong branch:
git merge <wrong-branch>
Then, check out to the branch, on which you have made the mistaken commits:
git checkout <wrong-branch>
Move the current branch back by the number of the mistaken commits by using the git reset command:
git reset --keep HEAD~N
There are cases when you want to move a particular commit, instead of merging the wrong branch with the existing one.
Below, we will demonstrate how to move commits in such cases:
Check out the existing branch with the git checkout command:
git checkout <existing-branch>
The next step is bringing back the particular commit by using the git cherry-pick command:
git cherry-pick <sha1-commit-hash>
To complete you need to checkout to the wrong branch and reset the commits. To do that, see the third and fourth steps of the section above.
At first sight, the git reset and git checkout commands have some similarities since they both operate on HEAD. If git checkout operates exclusively on the HEAD reference pointer, git reset passes the HEAD reference pointer and the current branch reference pointer.
One of the most commonly used options of git reset is --hard. With --hard, the commit history reference pointers start pointing to the stated commit. After, the staging area and the working directory are reset to correspond to the stated commit. Changes that have been previously pending to the staging area and the working directory, are reset to match the commit tree state. Any pending commit in the staging index and working directory will be lost.
The --keep option resets index entries and updates files in the working tree that are different between commit and HEAD. When a file is different between commit and HEAD has local changes, reset is aborted.