Temporarily checkout an old commit of a remote branch

Viewed 544

I have a local branch named Foo set to track its remote branch named Foo on the upstream named origin.

I'd like to temporarily checkout an old commit of Foo, look at a few things, and then after I have just looked at the old code, get back to my latest, most recent commit.

To amplify, I am not after getting back the HEAD set to an older commit. I do not want to reset the HEAD of my repo. I just want to look at the older commit, and then get back to where I am in the present.

I have done this in the past several times but I have only a vague memory and so I'd like to confirm before I do it again. It was, I think:

$ git checkout {branchName}
$ git checkout {commitId}

Is that right?

I know that I can also browse the files in the old commit via the GitHub Web-based interface but I'd still like to checkout the older commit because there are way too many changes and the usability of the Web interface won't be good enough for me to look at all those changes.

3 Answers

Yes. Go back to a specific commit:

$ git checkout {branchName}
$ git checkout {commitId}

Back to the latest commit of a branch.

$ git checkout {branchName}

Or, simple tricks to switch back to the last commit you were 
$ git checkout -
  1. Git checkout -b branch_name commit_hash

    explanation: Creates new branch named branch_named from commit_hash and switches to it. Helpful in case of hot fixes

  2. Git checkout -b new_branch_name tagname

    explanation: Creates new branch from tag named tagname and switched to it.

I suggest that you'll branch the commit:

git checkout -b {tempBranchName} {commitId}

Look at the code, and then go back to the branch

git checkout {branchName}

And you can delete the temporary branch:

git branch -D {tempBranchName}
Related