0

I am a new Git user, I have used SVN for years and have just made the switch.

I'll try to summarise using the proper 'Git language': Can i safely merge un-pushed (i.e. only committed) changes from one local branch to another, and then push those commits on the new branch, in order to move the commits from one local branch to another?

Case:

There are 2 development branches, call them source_branch and dest_branch. source_branch is the main development branch being worked on by multiple engineers. dest_branch is a branch which has only my changes. Both were branched from master independently. I did some work on dest_branch, committed, and pushed back to the remote dest_branch. Then i switched to source_branch, made some changes, committed locally but did not push them back to the remote source_branch. It then became obvious that my change was more complicated than I thought; I no longer wanted to work on source_branch and i did not want to push anything up to the remote source_branch. I merged my local copy of source_branch onto dest_branch to try and 'move' the commits. Is this a bad idea? Should i have done something differently? When i try to delete my local copy of source_branch i get the following message:

warning: not deleting branch 'source_branch' that is not yet merged to '/origin/source_branch', even though it is merged to HEAD. error: The branch 'source_branch' is not fully merged.

I understand what it means - but I'm unsure if this come back to bite me later?

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 2
    Does this answer your question? [How do I move unpushed committed code to another branch?](https://stackoverflow.com/questions/45216144/how-do-i-move-unpushed-committed-code-to-another-branch) – SwissCodeMen Jul 08 '21 at 08:43

2 Answers2

1

I merged my local copy of source_branch onto dest_branch to try and 'move' the commits. Is this a bad idea?

Yes. If you make commits on branch A and realize that you wish they had been made on branch B, merging is totally the wrong thing to do. It does not move any commits and it changes the topology in undesirable ways.

Your first move should be to undo the merge. Get on branch B and reset hard to the commit before the merge, erasing the merge commit.

Now you can do it the right way. Still on branch B, cherry-pick the commits that should have been on branch B. That copies the commits. Now switch to branch A and reset hard to before the unwanted commits, erasing them from branch A.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

If you merged onto dest_branch or created another branch for this change, it will be recorded in your local Git history. When you push that branch to origin, it will also be accessible to others. In either case, there's no need for the change to be on source_branch as well. Assuming that other people will update source_branch at some point, it would be better to not have local commits for that lying around. Once your changes are safe, you can clean up using

git checkout source_branch
git reset --hard origin/source_branch

CAREFUL: the above is a destructive command so make sure you have your changes saved elsewhere!

Note that your merge will leave a merge commit in the history which may be regarded as unclean. To avoid that, you could either cherry-pick or rebase dest_branch.

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
  • This has worked and i can see in the history that my commit is now on dest_branch and that i have no outstanding commits on source_branch. Thanks. "Note that your merge will leave a merge commit in the history which may be regarded as unclean"..... what exactly do you mean by unclean here? – Tom Jackson Jul 08 '21 at 12:59
  • There are multiple ways of moving commits from one branch to another. Merging usually leaves a merge commit (with empty diff) to record this action although it may not be necessary and some people think it pollutes or clutters the history with unneeded information. If it bothers you, there's `git log --no-merges` to hide it. – Jan Wilamowski Jul 09 '21 at 01:22