Pergunta

I have a project in git. I checked out an earlier commit, now I'm on an un-named branch. I want to save the work here and go back to the latest branch but still be able to come back. How do I do that, is it stash & pop?

Here is what transpired. I realized that I needed to go back to the state of things several months ago. So I ran

$ git log

and located the commit number, E.G.

commit 1910e8fdf0b37cd5c7a693572bf89767a2615c7d
Author: user account <user@xxx.xxx.xxx>
Date:   Fri Jun 15 15:00:43 2012 -0700

--> WORKING VERSION <--

At which point I switched to that commit by typing:

$ git checkout 1910e8fdf0b37cd5c7a693572bf89767a2615c7d

I made a few changes. Now I need to jump back to the latest version but I want to preserve the changes that I made in the earlier version. I don't want to merge them; they should be a separate branch. But I'm not on a branch. When I type "git branch" I get this:

$ git branch
* (no branch)
  bias_turn_on_experiment
  hst_persist
  master

And "git status" shows me this:

$ git status
# Not currently on any branch.
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   sn102
#       modified:   timIRmisc.asm
#       modified:   timing.def
#

So how do I save the state of things, go back to master, and then come back to this current state, without effecting any of the other branches? Is it git stash? But then where do I pop it back again? Or maybe git stash branch? I'm not sure. Thanks.

Foi útil?

Solução

I would create a branch with git branch <NAME> and then stash the commit in it. Then you can later check out the branch again and pop the stash there to continue work.

EDIT user3392484 solution is fine if you have a clean working commit. For the sake of keeping a functional clean history I would prefer my approach.

Outras dicas

Create a branch to hold your changes:

git branch foo 1910e8f
git checkout foo
git add ...
git commit

If you don't want to create a branch (begging the question why not) then commit in the detached state and tag:

git add ...
git commit
git tag foo
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top