Reputation: 1
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?
Upvotes: 0
Views: 623
Reputation: 3608
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
.
Upvotes: 0
Reputation: 535925
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.
Upvotes: 1