acman123
acman123

Reputation: 269

how to know which commit was last checked out?

I am using git. Consider that I have these in my history of commits:

commit 847a363f6676bf5d07c42d6ebec834fea1c9a363

commit 7b0720d5629ae0169fa2be17716ab736c0addeb0

My current commit (the current one with the latest changes) hash is 847a363f6676bf5d07c42d6ebec834fea1c9a363.

At some point in time I decide to remove my current files in my dir and restore the previous commit using this command:

$ git checkout 7b0720d5629ae0169fa2be17716ab736c0addeb0

But after some time I forgot which commit I last checked out. How can I know that?

Upvotes: 3

Views: 992

Answers (2)

Sajib Khan
Sajib Khan

Reputation: 24146

git reflog will show your whole working tree. It keeps a record of all commits that are or were referenced in your repo at any time.

$ git reflog   

Switch back whatever it was before the last checkout.

$ git checkout -            # back to last checkout   

Upvotes: 4

CodeWizard
CodeWizard

Reputation: 141956

There are few ways to find out which commit is currently checked out

1. git reflog

You can always use the reflog as well. git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

Every time the HEAD is modified there will be a new entry in the reflog

git reflog
git checkout HEAD@{...}
#This will get you back to your desired commit


2. Checkout prevoius branch

Another way: since you was in detached HEAD

git checkout 7b0720d5629ae0169fa2be17716ab736c0addeb0

You can always checkout the latest branch you worked on and get back to the latest commit.


3. git worktree

git worktree allow you to work on multiple branches simultaneously so you dont have to change and switch commits, you simply create an worktree and you will have a new 3-states in which you can switch and checkout any branch you like.

Upvotes: 2

Related Questions