5

I am new to git and stumbled upon a strange problem

So I did some commits to my local repository and viewed them with git log and it worked fine. After some time I checked out an older commit and later I wanted to jump back to my latest commit to continue my work. So I did another git log. But now the latest 3-4 commits were not displayed. Typing 'git checkout master' brought me to the latest commit of the second log command but not to the commit of the first one I described. Is this a issue or do I have to type another command to get back to the latest commit?

I hope my problem is understandable.

All commands i typed in the right order:

Git add public/
Git commit -m “Fo“
(No errors here)
Git log --oneline.
Output: 8 commits.
Git checkout “id of older commit“
Git log --oneline.
Output: 4 commits

4
  • You're very sure you didn't make these changes to a seperate/development branch or perhaps stashed them? Afaik; when your git status displays changes you can't simply check out to another state without either stashing your changes or resetting/reverting them to HEAD. In case you did stash your changes, using "git pop" should return you to the state when you started stashing. Commented Jun 3, 2016 at 12:36
  • 1
    You should provide the exact sequence of commands you ran, and the associated output, in order to define exactly what you did. Then describes exactly what you want to have. Commented Jun 3, 2016 at 12:37
  • 1
    Sounds like a detached HEAD. Try SourceTree until you're familiar with the functions. Commented Jun 3, 2016 at 13:07
  • It might be clearer passing in more flags to log; try something like git log --oneline --all --graph --decorate. Also mentioned in an answer was git reflog Commented Jun 3, 2016 at 13:19

2 Answers 2

12

After you run git checkout sha1, you are at detached HEAD state. Use git reflog to find your previous HEAD and git checkout that HEAD or git reset --hard to that HEAD.

If you were on a named branch, you could simply git checkout <branch name> to go back. If you were on a detached HEAD, use git reflog instead.

Sign up to request clarification or add additional context in comments.

4 Comments

So when i am on detached HEAD i just see the commits until this point?
"Doing so will make your working copy a 'detached HEAD', which means you won't be on a branch anymore. If you want to commit after this you'll probably want to either checkout a branch again, or create a new branch."
@GuyWithCookkies you could consider a detached HEAD as a nameless branch. HEAD always refers to the last commit of the work tree. After you checkout a specific sha1, the commits after it are hidden since that sha1 now is the HEAD. Unless you know exactly what you are doing, always avoid staying at detached HEAD state because you may easily confuse yourself and get lost.
To add to this, use git status often (many Git users set up their shells so that the command line prompt itself includes key items out of git status). If it says detached at <hash> or detached from <hash>, you're in this "detached HEAD" mode, on the anonymous branch. This is normal during rebase! In most other cases it just means you need to git checkout <branchname> to get back on a branch, though.
1

I don't think anyone gets what you're exactly asking, but I think you might've stashed your changes.

Type:

git stash list 

What does it show there, is there any information?

If there is something like this

stash@{0}: WIP on master: 049d078 added the index file 

You can type:

 git stash apply // for the last stash to be applied git stash apply stash@{1} // for the second from last stash 

Also, you can try:

git stash pop // apply the stash and then immediately drop it from your stack 

The difference between these above is that pop discards stash from stack, but apply keeps it for possible future reuse.

If your commit is missing in log or stash, you probably had your HEAD reset.

From now on, do the next procedure for your work in order not to mess up anything.

  • Checkout your branch from updated master

     git checkout -b your_branch_name 
  • After you've done some work

     git add "name of your file" or git add * (to add all files) 
  • Git add command updates the index using the current content found in the working tree, to prepare the content staged for the next commit

    git commit -m "Your message here" 
  • To push it to remote origin

    git push origin your_branch_name 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.