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?

- 741
- 2
- 7
- 7
-
1Have you looked at this answer? - http://stackoverflow.com/questions/1773731/in-git-how-do-i-remove-a-commit-from-one-branch-and-apply-it-to-a-different-bra?rq=1 – Dan Hoerst Mar 05 '13 at 22:26
-
look at this: [move-recent-commit-to-a-new-branch][1] [1]: http://stackoverflow.com/questions/1628563/move-recent-commit-to-a-new-branch – D-E-N Mar 05 '13 at 22:27
-
since there's a "this question may already have an answer here" block at the top, just want to point out my question is not about moving to a _new_ branch, it's about moving to a different _existing_ branch – gitq Mar 05 '13 at 23:31
-
@gitq: See Dan Hoerst's question for a solution. It's called cherry picking and works with existing branches. – guerda Mar 12 '13 at 10:12
4 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.

- 1,564
- 12
- 11
You can cherry-pick
your commits over to the develop
and afterwards interactively rebase
your master
branch:
git checkout develop
git cherry-pick aabbcc
git cherry-pick ddeeff
- ....
git checkout master
git rebase 123456 -i
where 123456
is a commit before you made a mistake. This will open an editor that shows every commit that will be affected by the rebase. Delete the lines that correspond to the commits you want to discard and quit the editor.

- 34,832
- 7
- 76
- 98
For coping into another branch you can use cherry picking:
git cherry-pick <commit>
Deleting is not that easy. You can use rebase and squash or edit the commit:
git rebase -i <commit>~1
But I am not sure when chosing edit during rebase if you can edit the files also rather than the commit message only.

- 2,075
- 1
- 22
- 30
-
1`edit` during an interactive rebase lets you edit commit-message and files, `reword` only lets you modify the commit-message. – Nils Werner Mar 05 '13 at 22:32
-
After this, my head is detached at this commit. How should I make these changes to reflect on the remote branch? – Anand Aug 06 '20 at 09:17
There are usually several ways to do the same thing in git one possible way:
git checkout develop
git cherry-pick XXX // XXX being the sha1 of the commit you want to grab
git checkout master
git rebase --interactive HEAD~IDX // IDX being the position of the last "good" commit compared to HEAD
The last command will display all review from HEAD to the last good commit and all you have to do is deleted the line of the commit to moved to branch develop

- 2,958
- 1
- 28
- 36