Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible Ways to refer a commit

Tags:

git

Would like to find different ways that we can refer to a commit in git. Could anyone please let me know what are the different ways that we can refer to a commit in git?

like image 334
Krishna Avatar asked Jan 21 '26 18:01

Krishna


1 Answers

As a Git newbie it took me a while to parse Rishi's explanation, so let me tell you my interpretation:

  • The word HEAD refers to the most recent commit.

Example:

git show HEAD:content.js
  • Appending ~ and a number refers to the nth commit before that one. So HEAD~2 is the commit two steps before HEAD.

Example:

git show HEAD~2:content.js
  • You can use the hash of a commit to refer to it, or even the first part of the hash (so long as it is unique). You can find your commits' hashes by typing git log --reflog; the hash looks like d8c01a835cd2d345703914c3302632bbfb1f0c58. You can type that all out, or you can refer to it as d8c01, or as many characters from the beginning that work. (I got four to work; I don't know if that is a global minimum or not.)

Example:

git show d8c01a83:content.js
  • You can use the git tag command to give a tag to a commit, which can be referred to later. So git tag v1 gives the current commit the tag v1. You can also name previous commits, like git tag v1 d8c01. Example:
git tag v1 d8c01
git show v1:content.js
  • Lastly, refspecs are (I believe) things like origin/main which refer to the current commit on the remote server.

Example:

git show origin/master:content.js
like image 129
Lihtox Avatar answered Jan 23 '26 14:01

Lihtox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!