tags:

views:

1319

answers:

4

How can I do this in git:

My current branch is branch1 and I have made some local changes. However I now realize that I actually meant to be applying these changes to branch2. Is there a way to apply/merge these changes so that they become local changes on branch2 without committing them on branch1?

+1  A: 

If it were about committed changes, you should have a look at git-rebase, but as pointed out in comment by VonC, as you're talking about local changes, git-stash would certainly be the good way to do this.

claferri
I do not understand this solution: it would rewrite commit history of branch2 from branch1... why getting all committed changes from branch1 in branch2 when we only want to get local non-committed changes of branch1 in branch2 ?...
VonC
@VonC : agreed, in this case, the rebase gets all committed changes since last merge between branches into branch1. I didn't get the "non-committed" parameter of this question at first. rebase isn't the good answer.
claferri
@claferri: pfew... I was beginning to have an headache ;) I would have downvoted your answer, but since I had myself published one, there was a "clear conflict of interest". With your updated post, I do not have to downvote at all now. Thanks :)
VonC
@VonC : next time, feel free to down vote as long as my answer is as wrong as this one ;)
claferri
+1  A: 

There is a great Git Tutorial right here on SO. Its kind of a central for all git questions on stack overflow.

Decio Lira
Why the downvote exactly? Input would be nice, so I could update my answer accordingly.
Decio Lira
+12  A: 

Since your files are not yet committed in branch1:

git stash
git checkout branch2
git stash pop

or

git stash
git checkout branch2
git stash list       # to check the various stash made in different branch
git stash apply x    # to select the right one
VonC
Thanks - that's exactly what I was looking for
solsberg
You are welcome. More examples of stash usage at http://unethicalblogger.com/posts/2008/11/git_protip_stash_goods_yo_git_stash .
VonC
One small typo `got stash pop` should be `git stash pop`. Someone want to fix. And +1, just what I was looking for.
MitMaro
@MitMaro: thank you for your feedback. And the typo is now fixed ;)
VonC
+4  A: 

Stashing, temporary commits and rebasing may all be overkill. If you haven't added the changed files to the index, yet, then you may be able to just checkout the other branch.

git checkout branch2

This will work so long as no files that you are editing are different between branch1 and branch2. It will leave you on branch2 with you working changes preserved. If they are different then you can specify that you want to merge your local changes with the changes introduced by switching branches with the -m option to checkout.

git checkout -m branch2

If you've added changes to the index then you'll want to undo these changes with a reset first. (This will preserve your working copy, it will just remove the staged changes.)

git reset
Charles Bailey
I thought the stash "simpler" somehow to understand, but your approach is better at taking into account the working directory across different branches. +1
VonC
A plain traditional checkout seemed more appropriate to the problem in hand. checkout is lighter weight, it just updates the files that need to change. Perhaps it's easier to understand the stash approach, or it may just be that it's not obvious enough that checkout is 'safe' in this use case.
Charles Bailey