1

I had a branch other than 'master', say abc, checked out, and mistakenly made changes which I committed locally (no git push). The branch abc was merged into master at origin, prior to my two commits. Additional commits were made to master

Now, I really want the changes of my two commits applied to the branch master starting with HEAD. What commands would I use?

Related question How to fix committing to the wrong Git branch? really misses what I need, as it just throws away the errant commit, but does not apply it to the correct branch.

Kevin Buchs
  • 2,520
  • 4
  • 36
  • 55
  • 1
    Possible duplicate of [How to fix committing to the wrong Git branch?](https://stackoverflow.com/questions/2941517/how-to-fix-committing-to-the-wrong-git-branch) – phd Mar 30 '18 at 19:06
  • The second answer in the linked question answers your question. – 1615903 Mar 31 '18 at 08:18

3 Answers3

2

In case it is not the latest commit in branch abc:

git checkout master
git cherry-pick <commit in abc> // 2 times
checkout abc
git revert <commit in abc> // 2 times

If they are the latest two commits in abc: after cherry-picking it to master reset your branch abc to the commit before:

git checkout abc
git reset HEAD^^
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236
milbrandt
  • 1,438
  • 2
  • 15
  • 20
  • After the cherry-picking, I get: $ git status On branch master Your branch and 'origin/master' have diverged, and have 3 and 28 different commits each, respectively. (use "git pull" to merge the remote branch into yours) – Kevin Buchs Mar 30 '18 at 18:55
  • That's unrelated to cherry-pick. In the remote repo 28 commits were made (by others) you didn't pulled up to now. In your local are 3 commits (2 previous and the cherry-pick commit) which are not pushed to remote origin. Fine, that's a standard situation. You had had it already in advance of the cherry-pick - you only didn't knew. Before the cherry-pick git status would have told you 2 and 28 different commits. – milbrandt Mar 30 '18 at 19:03
  • as 2 commits are in the wrong branch, you need to do 2 cherry-picks - obvious. – milbrandt Mar 30 '18 at 19:20
  • So, if I want to get everything with origin up to date, then I need a `git push` after the cherry picking is complete and another after the reverting. Thanks. – Kevin Buchs Mar 30 '18 at 19:41
  • First you need to `git pull` which will merge your changes with origin/master. If there are merge conflicts you have to resolve them on your copy. Only if everything from origin/master is merged on your side it will be possible to push. – milbrandt Mar 30 '18 at 19:47
1

You can switch to master and git cherry-pick the two commits, abc and abc^ (the latter denoting the commit that precedes abc).

To correct your abc branch, check it out and annul your local commits using git reset --hard origin/abc.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
0

A quick way to fix this would be to merge abc into master with fast forward enabled is possible (which will keep a single line of history), then after that's done to reset HARD branch abc. As long as you merge into master first, nothing from abc will be lost since it will be in master.

AlexKven
  • 138
  • 1
  • 9