2

I am very new to git usage and finding it very confusing to understand the branch/commit management. Anyway, the project I am working on is managed by git, and I am using gitk for it. Using gitk I was making commmits, and checked out a previous commit. And now when I do Visualize HEAD's history, the commit that I checked out appears last and my other 5 or 6 commits after that have disappeared.

Please note that I was working on master branch - but for some reason I was not on master branch right Now. That is to say the gitk visualization was like this:

commit 14 . . . commit 9 . . master . . . . commit 1

I checked out commit 9 and I don't have any commits hash codes either.

Saad Rehman Shah
  • 926
  • 2
  • 10
  • 28
  • 2
    Helpful trick with gitk: `gitk --all` (shows all branches). That way, no matter which one you're on, or even if you're in that "detached HEAD" state, you can see everything. – torek Mar 20 '12 at 17:25

2 Answers2

3

Use git log to see all your commits, get the SHA1 hash of the lastest one, then do

git checkout -b "branchname" <SHA1 goes here>

This will retrieve any commits you made when not on a checked out branch and you should then have all your commits visible on a new branch.

Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
  • Git log is displaying commits up till the current commit that I have checked out. It's not showing the ones I did after it. – Saad Rehman Shah Mar 20 '12 at 11:00
  • Okay I just did that. Thankfully it shows all the commits and their hashes too. It shows All 53 commits and shows the latest one next to HEAD{0}. And that's a checkout, so I will just checkout the one below this one. But can you, for the love of God, tell me what just happened? And how can I get my master to point to this latest commit? – Saad Rehman Shah Mar 20 '12 at 11:07
  • 1
    I think you may have been in 'detached head state' (http://stackoverflow.com/questions/3965676/why-did-git-detach-my-head), but I'm not sure how. Basically, make sure you are always checked out onto a named branch before committing. Git will record your commits in a tree structure, but will have no label (branch name) for the tip of the branch you are creating, so you can't find it. – Matt Gibson Mar 20 '12 at 11:11
  • So what do I do know? If I want to merge this head-less branch back to master? – Saad Rehman Shah Mar 20 '12 at 11:20
  • If you just want to add those commits to master, use: 'git checkout master', followed by 'git merge ' – Matt Gibson Mar 20 '12 at 11:22
  • 1
    Actually I was in a detached Head state. Creating a new tempt branch, resetting master to that branch, and then checking out master helped me sort that out.http://stackoverflow.com/questions/5772192/git-how-can-i-reconcile-detached-head-with-master-origin Was of GREAT help. – Saad Rehman Shah Mar 20 '12 at 11:35
0

You are in a detached HEAD state, meaning your current HEAD points to a commit and not a branch. What you need to do is to checkout the master branch again, then you will see your commits in gitk. Type the following in a command prompt:

git checkout master
rtn
  • 127,556
  • 20
  • 111
  • 121