0

I had some branch checked out locally. I committed a fist full och changes to it. Just when I was about to push them to the remote repo, I realized, I was working on the wrong branch.

I need to sort of 'move' those commits to the other branch, both locally and remotely, that is:

I have:

  • local branch A plus changes (commited, not pushed),
  • local branch B no changes,
  • currently checked out local branch A
  • remote branche A no changes,
  • remote branch B no changes

And I need to end up with:

  • local branch A no changes,
  • local branch B plus changes (pushed),
  • remote branch A no changes,
  • remote branch B plus changes.

I am new to git and not a professional developer, so git-push(1) does not help me without further research.

I came across this post: git push command for pushing a local commit to a different remote branch

It suggests:

git push ssh://company.com:29418/platform/vendor/com-proprietary/ship/ftm 72bc75e409e50dcad29bd790b4b6478dc6668f12:jb_mr2

Obviously the jb_mr2-part I change to whatever my branch B is called, but where do I get the rest of the numbers, and by what means do I get rid of the changes on branch A?

  • A friend helped me, we figured it out: –  bersama Jul 28 '19 at 14:53
  • A friend helped me, we figured it out: ``` $(A) git log - 1236 more new files need to check - 1235 new files need to check - 1234 checked files ready for project $(A) git checkout B $(B) git cherry-pick 1235, 1236 $(B) git checkout A $(A) git reset --hard 1234 ``` Obviously the numbers are much longer and not in sequence since they are checksums. –  bersama Jul 28 '19 at 15:00
  • Possible duplicate of [Move the most recent commit(s) to a new branch with Git](https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git) – Yawar Jul 28 '19 at 17:36
  • Here's an good answer to this type of question ('how do I move commits from one branch to another'): https://stackoverflow.com/a/36463546/20371 – Yawar Jul 28 '19 at 17:37
  • 1
    Thanks, Yawar, I didn't come across that thread in my research, but that seems correct (only I already have that other branch). Anyway, I fixed my problem this time, but I'll bookmark that answer, because I'm pretty sure I WILL forget to switch branches some time soon again, possibly with more commits than the few I had now. –  bersama Jul 30 '19 at 20:58

1 Answers1

0

A friend helped me, we figured it out:

$(A) git log 
- 1236 more new files need to check 
- 1235 new files need to check 
- 1234 checked files ready for project 
$(A) git checkout B 
$(B) git cherry-pick 1235
$(B) git cherry-pick 1236 
$(B) git checkout A 
$(A) git reset --hard 1234 

Obviously the numbers are much longer and not in sequence since they are checksums.

This worked fine for me, as I had only a few commits to take care of. And they were the last commits in sequence.

If there were later commits that should not be moved or if there had been lots of commits, the answer that Yawar referred to above seems worth checking out.