Method 1:
$ git checkout v4.1.0-rc12 branch
(see if it succeeds, if so):
$ git add ... # if / as needed
$ git commit
This might not work, because the git checkout
step will refuse to switch to the branch, if (and only if) said switching will wipe out the changes you've already made.
The git checkout
step will succeed, and carry your changes along, if it can.
If the git checkout
step succeeds, this is by far the easiest way to deal with it all. If not, proceed to alternate methods.
Method 2 (avi's answer):
$ git checkout -b tempbranch
$ git add ... # as and if needed
$ git commit
This (the -b tempbranch
) creates a new, temporary branch pointing to the commit at which you detached your HEAD earlier. The git commit
then creates a new commit on this temporary branch:
o--o--o <-- v4.1.0-rc12 branch
/
...--o--o
\
o <-- tempbranch
Now you can git checkout
whatever you like, and the commit you made on tempbranch
is permanently stored in your repository (well, permanent unless/until you delete the name tempbranch
), which enables you to copy stuff from it whenever you want.
Method 3:
$ git stash
$ git checkout v4.1.0-rc12 branch
$ git stash apply # or even "git stash pop" but I prefer "apply"
... check everything out ...
... if it all looks right ...
$ git stash drop # "git stash pop" means "apply and then auto-drop"
What git stash
does is make a commit (well, actually two commits) that are not on any branch. The effect is the same as when you make the temporary branch. That leaves the problem of deleting the non-branch when you're done with it, so git stash drop
deletes the unnamed non-branch that holds the commits.
The git stash apply
step essentially (though it's more complicated than this) cherry-picks the commit that git stash
made, wherever you are now: so this is like cherry-picking the commit you made on tempbranch
.
Note that git stash pop
automatically invokes git stash drop
, even if the result of the git stash apply
is a mess, you've lost your temporary commits if you pop
but not if you apply
. If everything goes well there's no real difference and pop
is more convenient; this is just a case of leaving a better bread-crumb trail in case things go bad.
(My personal preference, incidentally, is for method 1 first if possible, then usually 2, then 3, but sometimes—when I am sure it's going to work—I'll prefer method 3 to method 2.)