2

I have a total of 24 commits, and I had checkout to commit 15. How can I go back to my last commit?

Note: I have run a lot of commits. For instances,

git revert, git checkout, git reset.

I don't know my last commit name, id, or even the files. All I know that I have committed, but I am not there.

but I could not go back to my latest commits.

Thank you

Nimatullah Razmjo
  • 1,831
  • 4
  • 22
  • 39
  • 3
    You might want to try `git reflog`. This command shows you the last things you did, with the most recent one at the top. The hash on the left is the commit which was checked out after that action. – lucash Jul 17 '16 at 10:42
  • 1
    Using git bash, you can issue the following command to get a list of all branches - `git branch` After that, you can issue the following command to checkout the latest of any branch - `git checkout ` After checking out, if you want to see the history, you can issue the following command - `git log` – ArunGeorge Jul 17 '16 at 10:43
  • Thank you @lucash. after git reflog, I checkout to my latest commit id. – Nimatullah Razmjo Jul 17 '16 at 10:50
  • 1
    Possible duplicate of [How can I reconcile detached HEAD with master/origin?](http://stackoverflow.com/questions/5772192/how-can-i-reconcile-detached-head-with-master-origin) – Jim Jul 17 '16 at 11:04
  • Thank you @Jim, after checkout to recent commit I couldnt push to online repository. this link helped me to solve my next problem :) – Nimatullah Razmjo Jul 17 '16 at 11:23

3 Answers3

5

Finally, with help of friends in here I come up with solution. the resean I post this. It may help other as well.

First, I used to git reflog to show all my last commit.

Second, I used git checkout commit_id

Third, git log --graph --decorate --pretty=oneline --abbrev-commit master github/master temp.

Then, git branch -f master temp.

And, git branch -d temp.

Finally, git push --force github master

Nimatullah Razmjo
  • 1,831
  • 4
  • 22
  • 39
3

git checkout foo will check out the most recent commit on the foo branch. If you're working in master, you would run the command git checkout master.

Edit: In this case, you seem to have been committing to a detached head. You should follow the advice given for this question and avoid doing this in future.

Community
  • 1
  • 1
Jim
  • 72,985
  • 14
  • 101
  • 108
  • It goes to my checkout commit not latest commit. After checkout I have worked and commit,but it not show – Nimatullah Razmjo Jul 17 '16 at 10:37
  • What do you mean by "checkout commit not latest commit"? – Jim Jul 17 '16 at 10:38
  • I mean, I had use git checkout 6d15fe9179a22ebb97333cb2fc15c1757d2d29f2. after checkout I have add and committed a lot. Now I want my head should be the latest commit not the checkout commit – Nimatullah Razmjo Jul 17 '16 at 10:41
  • This should be the accepted answer if you are navigating through commits (using `checkout`s) and you want to go back to the latest commit you made – testing_22 Jul 10 '22 at 14:31
1

git log --all will show you all your commits
you can git checkout the hash you want