Kei
Kei

Reputation: 771

Git checkout without losing working tree

Let's suppose I have many commits in the master branch.

Sha1 1st commit

Sha2 2nd commit

Sha3 3rd commit

Sha4 4th commit HEAD

Suppose I want to move (just to look around and make some slight modifies) to 2nd commit by using git checkout sha2, I will now be able to see what's in 2nd commit, but I will lose track of Sha3 and Sha4, though they are still there. Infact if I issue git log only 1st and 2nd commit will be shown. Then if I issue git checkout master I will move once again to 4th commit. Is there a way to keep track of Sha3 and Sha4 (without noting them down) in deatached HEAD mode when HEAD is at 2nd commit ?

Upvotes: 0

Views: 113

Answers (6)

Vlad Nikitin
Vlad Nikitin

Reputation: 1951

i always run git log with next options , which gives me comprehensive information

git log --graph --all --decorate

Upvotes: 0

Magnus Bäck
Magnus Bäck

Reputation: 11571

Yet another way can be to use the reflog, which records which commits have been checked out.

$ git rev-parse HEAD
65ca752aa18d6cd24196b5ba0fd6f53af49f8c56
$ git checkout HEAD~2
Previous HEAD position was 65ca752... 4th commit
HEAD is now at a59b854... 2nd commit
$ git rev-parse HEAD
a59b8545cf1aa013cf87b9946e738e0add1f4536
$ git checkout HEAD@{1}
Previous HEAD position was a59b854... 2nd commit
HEAD is now at 65ca752... 4th commit
$ git rev-parse HEAD
65ca752aa18d6cd24196b5ba0fd6f53af49f8c56

Note: While this'll get you back to the same commit you had checked out before, you'll end up in a detached head rather than at the branch. Since it seems you're explicitly looking for a way to reference the commits rather than moving back to the tip of the branch (because then the solution would've been trivial; git checkout some-branch-name).

Upvotes: 0

Wolf
Wolf

Reputation: 4452

You already have the tip of master at Sha4, so you can still see Sha3 and Sha4 just by saying

    git log master

...even when you're in a detached HEAD state at Sha2.

Upvotes: 2

Magnus Reftel
Magnus Reftel

Reputation: 999

One way is to tag them via git tag temp sha3 etc. before checking out sha2. Another is to look at the log for master after checking out sha2, e.g. via git log master --oneline.

Upvotes: 1

Abizern
Abizern

Reputation: 150605

If you want to have a look at the code in a previous commit, just checkout to a branch

git checkout -b just_looking sha2

Now you can look around the code in this new branch, and the original branch has not been affected.

If you make any changes, you can merge them with the original branch.

Upvotes: 0

bundacia
bundacia

Reputation: 1036

You can use git log master to see all of the commits, even when you don't have master checked out.

% git log --oneline                          
ec3541a 4th commit
b3febbf 3rd commit
3bbabce 2nd commit
3467bdc 1st commit
% git checkout 3bbabce                       
Note: checking out '3bbabce'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 3bbabce... 2nd commit
% git log --oneline                          
3bbabce 2nd commit
3467bdc 1st commit
% git log --oneline master                   
ec3541a 4th commit
b3febbf 3rd commit
3bbabce 2nd commit
3467bdc 1st commit

Upvotes: 1

Related Questions