Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move git commits from master to a different existing branch [duplicate]

Tags:

git

branch

I checked in a few commits to master that should have been checked into develop. What git commands do I used to remove those commits from the master branch and include in the develop branch?

like image 587
gitq Avatar asked Mar 05 '13 22:03

gitq


People also ask

Can I copy a commit to another branch?

git cherry-pick <commit-hash> will apply the changes made in an existing commit to another branch, while recording a new commit. Essentially, you can copy commits from branch to branch.


1 Answers

If I'm not mistaken, you had two synchronized branches, master and dev, and simply forgot to switch the branch, before your commits.

If that is the case, we have:

---------------- git log in dev  xxx yyy ... ---------------- 

and:

---------------- git log in master  ccc bbb aaa         <---- here you forgot to switch branch xxx yyy ... ---------------- 

The solution is:

First, make sure, that:

git status -s 

returns empty results.

Next, get all your new commits from master to dev with:

git checkout dev git merge master 

Now return to you master:

git checkout master 

Remove unnecessary commits:

git reset --hard HEAD~3 

The number ~3 is the number of commits you want to remove.

Remember: git status -s have to return empty results. Otherwise, git reset --hard can cause data loss.

like image 113
Włodzimierz Gajda Avatar answered Sep 27 '22 02:09

Włodzimierz Gajda