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?
-
1Have you looked at this answer? - stackoverflow.com/questions/1773731/…Dan Hoerst– Dan Hoerst2013-03-05 22:26:13 +00:00Commented Mar 5, 2013 at 22:26
-
look at this: [move-recent-commit-to-a-new-branch][1] [1]: stackoverflow.com/questions/1628563/…D-E-N– D-E-N2013-03-05 22:27:23 +00:00Commented Mar 5, 2013 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 branchgitq– gitq2013-03-05 23:31:27 +00:00Commented Mar 5, 2013 at 23:31
-
@gitq: See Dan Hoerst's question for a solution. It's called cherry picking and works with existing branches.guerda– guerda2013-03-12 10:12:39 +00:00Commented Mar 12, 2013 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.
Comments
You can cherry-pick your commits over to the develop and afterwards interactively rebase your master branch:
git checkout developgit cherry-pick aabbccgit cherry-pick ddeeff- ....
git checkout mastergit 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.
Comments
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 Comments
edit during an interactive rebase lets you edit commit-message and files, reword only lets you modify the commit-message.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