There are great chances of making mistakes while committing our changes in git. One of the common mistake that we might make is that we might commit in wrong branch. While working with different branches in a project, there is a chance that we might commit in wrong branch. This can cause lots of effort and time to fix them manually one by one, especially when there are commits for lots of changes. But thankfully it is git that we are talking about. There are different ways to fix this problem. Each has their own advantages and disadvantages. I am going to talk about one of the best one.
Step1:
Checkout to the branch where you have the commits that needs to be moved.
Step2:
Note the commit’s hash(or initial hash and final hash if all commits are that needs to be moved to the correct branch)
Step3:
Use hard reset to get the current branch to the initial state
Step4:
Checkout to the correct branch where the commits needs to be moved
Step5:
if single commit needs to be added:
if multiple continous commits needs to be added:
And you are done. The commit(s) will now be in correct branch.
This method is one the best one as this method does not require rewriting commit message(s). But the hash value(s) must me remembered for this method.