0

If

git log

#o/p

#commit 1

#commit 0

Then

git checkout 0

Then

git log

#commit 0

Commit 1 was erased...

I don't think this is normal behavior from my (limited) experience with git. I am using it in dropBox, but it seems that that's ok. Would really appreciate some help.

Sam Weisenthal
  • 2,791
  • 9
  • 28
  • 66
  • 1
    They're not gone! The issue is that `git log` starts by looking at the current, i.e., `HEAD`, commit, and working *back* from there (unless you give it some *other* starting point(s)). When you use `git checkout` to look back at an older commit, you won't see newer commits by default. Simply "re-attach your HEAD" as in sajib khan's answer to make `HEAD` refer to a branch name, so that `git log` starts from the current tip of that branch. – torek Feb 28 '17 at 12:50
  • 1
    You have explicitly chosen to place your HEAD at commit 0 and hence this is the expected behavior. You HEAD is now pointing to commit 0. You could always execute 'git pull' and it HEAD will point to commit 1 (or latest) – Balaji Katika Feb 28 '17 at 12:51
  • 1
    All that said, you really should avoid keeping a repository under dropbox. Git and dropbox do not play well together. – torek Feb 28 '17 at 12:51
  • 1
    Also, to make git log show all commits, use `git log --all`. If you lost the commit because there are no references to this commit, like branches or tags, use `git reflog` to see your previous git actions and checkout directly to the hash of the lost commit. – Alex Ruiz Feb 28 '17 at 13:15
  • Possible duplicate of [How to get back to the latest commit after checking out a previous commit?](http://stackoverflow.com/questions/2427288/how-to-get-back-to-the-latest-commit-after-checking-out-a-previous-commit) – Sam Weisenthal Mar 02 '17 at 16:49

2 Answers2

1

When we checkout to a commit-hash we are no longer on a branch (detached HEAD).

Checkout to branch. You should be back to your branch HEAD (exits commit0, commit1)

$ git branch                 # copy your branch name
$ git checkout <branch-name> # back to branch HEAD

Or,
$ git checkout -             # switch to the last commit you were
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
1

Commit 1 wasn't erased in this case. You just switched to an older commit and now git log shows only commits that are ancestors to that [older] commit.

You can use git checkout - or git checkout $commit1_hash to return to a newer revision.

Also, it may be helpful to use git reflog to see history of switches between revisions.

mshrbkv
  • 309
  • 1
  • 5