Why must we use the first seven characters of a SHA hash to identify a commit?

Question

We learn here that when using git reset with the commit’s SHA hash, we should use the first seven characters of the hash. Why is this?

Answer

The reason for this is to reduce the potential ambiguity of the commit you are selecting. The characters are hexadecimal digits (0-9, a-f) and so by using seven characters, there are over 250 million potential hashes. This range reduces the possibility of collision.

Note that we can use a smaller number of characters to identify a commit at the cost of a higher chance of collisions. If ever the hash characters you choose are ambiguous, you will receive a error message stating so.

2 Likes

Hi,

I am unable to write anything on terminal. After typing git log, received all the details of previous commit. But along with that “Add.gitignore is visible.”

2 Likes

click q to exit the log

8 Likes

Hey after I git reset the first seven letter of the SHA and git log like requested I get an error and I’m not able to proceed any help?

This is happening to me as well.

Me too. Same problem

Could you provide some details about the error you received (@mark0751608359 & @micro9576893349). What did you type in and what was the error that popped up?

I just went back to get a screengrab, but it worked this time. Thinking through what happened when I first tried, I might have copied the first 7 SHA of my current HEAD, which I think shouldn’t work with the reset command, right? That might I have been my issue, but can’t remember.

1 Like

What the problem was is that we should copy the sha for the commit made in November instead of the ones we made

2 Likes

Hi, I’m stuck in this exercise, and I don’t know why, after I printed the log of the commits with the git log, I see a list of commits with the commit numbers. on the second step, it asked me to enter the reset command, and I tried the first seven numbers of all the commit’s but still not working and it keeps giving me the following message:

fatal: Unable to create ‘/home/ccuser/workspace/hamlet-prince-of-denmark/.git/index.lock’: File exists.

If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.

I think the suggestion in that error to remove the file manually might be the simplest-
rm /home/ccuser/workspace/hamlet-prince-of-denmark/.git/index.lock
If that’s no good then perhaps resetting that lesson might be the second easiest option.

1 Like

yes, thank you so much, that fixed it.

I have an error when type git reset 054D8S7 it doesn’t check the box with the green mark so I can move to the next lesson

I think it’s looking for that step of “print the git commit log again” in order to consider that task completed.

1 Like

I solve it by resetting the last commit, what I was doing before resetting the first commit it’s just a conflict in my brain lol, anyway thanks for your reply

1 Like

Am facing the same issue and stiil cant understand how to resolve it.

Dear all, if you’re getting an error message, it is because we need to use the second latest SHA and not the latest. Please see below the one we need to use:

commit d640dc3b22b3ee1d0a6e87477a35fd5e75c0712e
Author: codecademy [email protected]
Date: Thu Nov 5 13:19:17 2015 -0500

Complete ghost line of dialogue

The answer is therefore:

$ git reset d640dc3

2 Likes

Solved the issue with your advice. It works. Thanks

Im having the same problem too

Even using the command you gave me i still get an error message???

reset the work page and then it worked.