Bird
Raised Fist0
Gitdevops~10 mins

SHA-1 hashing concept in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - SHA-1 hashing concept
Input Data
↓
Preprocessing
↓
Split into Blocks
↓
Initialize Hash Values
↓
Process Each Block
↓
Compression Function
↓
Update Hash
↓
Final SHA-1 Hash Output
SHA-1 takes input data, processes it in blocks through a compression function, and produces a fixed 40-character hash string.
Execution Sample
Git
echo -n "hello" | sha1sum
2aae6c35c94fcfb415dbe95f408b9ce91ee846ed  -
This command computes the SHA-1 hash of the string "hello" and outputs the hash.
Process Table
StepActionInput/StateOutput/State
1Input Data"hello""hello"
2Preprocessing"hello""hello" + padding + length info
3Split into BlocksPreprocessed dataOne 512-bit block
4Initialize Hash ValuesN/A5 initial 32-bit words (h0-h4)
5Process Block512-bit block + h0-h480 words prepared for rounds
6Compression Function80 words + h0-h4Updated h0-h4 values
7Update HashUpdated h0-h4Final 160-bit hash
8Output Hash160-bit hash"2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" (hex)
💡 All blocks processed; final 160-bit SHA-1 hash produced as hex string.
Status Tracker
VariableStartAfter Step 4After Step 6Final
Input Data"hello""hello""hello" + padding"hello" + padding
BlocksN/AN/AOne 512-bit blockOne 512-bit block
h0-h4N/AInitial constantsUpdated by compressionFinal hash words
Hash OutputN/AN/AN/A"2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
Key Moments - 3 Insights
Why does the input data get padded before processing?
Padding ensures the data length is a multiple of 512 bits, which is required for SHA-1 to process data in fixed-size blocks, as shown in step 2 and 3 of the execution table.
What are h0 to h4 in the SHA-1 process?
They are initial fixed 32-bit words used as starting hash values, updated during processing (steps 4 to 6), eventually forming the final hash output.
Why is the final output a 40-character string?
Because SHA-1 produces a 160-bit (20-byte) hash, which is represented as 40 hexadecimal characters (2 hex chars per byte), as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of h0-h4 after step 4?
AInitial constants ready for processing
BFinal hash values
CInput data padded
DBlocks split into 512-bit chunks
💡 Hint
Refer to step 4 in the execution table where hash values are initialized.
At which step does the input data get split into blocks?
AStep 5
BStep 2
CStep 3
DStep 7
💡 Hint
Check the 'Split into Blocks' action in the execution table.
If the input string changes, which part of the execution table will definitely change?
AInitialize Hash Values
BCompression Function output
CPadding step only
DFinal output hash remains same
💡 Hint
Look at how the compression function output depends on input data blocks and hash values.
Concept Snapshot
SHA-1 hashing:
- Input data is padded to multiple of 512 bits
- Data split into 512-bit blocks
- Initial hash values (h0-h4) set
- Each block processed with compression function
- Final output is 160-bit hash (40 hex chars)
- Used in git to identify objects uniquely
Full Transcript
SHA-1 hashing takes any input data and processes it to produce a fixed-length 40-character hexadecimal string. The process starts by padding the input so its length is a multiple of 512 bits. Then the data is split into 512-bit blocks. Five initial hash values (h0 to h4) are set as constants. Each block is processed through a compression function that updates these hash values. After all blocks are processed, the final 160-bit hash is output as a 40-character hex string. This hash uniquely represents the input data and is used in git to track files and commits.

Practice

(1/5)
1. What does the SHA-1 hash in Git uniquely identify?
easy
A. A specific commit or file content
B. The username of the committer
C. The branch name
D. The date and time of the commit

Solution

  1. Step 1: Understand SHA-1 purpose in Git

    SHA-1 generates a unique code based on the content of files or commits.
  2. Step 2: Identify what SHA-1 represents

    It uniquely identifies the exact content or commit, not metadata like username or branch.
  3. Final Answer:

    A specific commit or file content -> Option A
  4. Quick Check:

    SHA-1 = unique content ID [OK]
Hint: SHA-1 hashes content, not user or branch info [OK]
Common Mistakes:
  • Thinking SHA-1 identifies user info
  • Confusing SHA-1 with branch names
  • Assuming SHA-1 stores commit date
2. Which command correctly shows the SHA-1 hash of the latest commit in a Git repository?
easy
A. git log -1 --format=%H
B. git sha1sum HEAD
C. git show --hash
D. git commit --hash

Solution

  1. Step 1: Recall Git command to show commit hash

    The command 'git log -1 --format=%H' prints the full SHA-1 hash of the latest commit.
  2. Step 2: Check other options for correctness

    The other options are invalid or do not exist in Git syntax.
  3. Final Answer:

    git log -1 --format=%H -> Option A
  4. Quick Check:

    Correct Git syntax for latest commit SHA-1 is git log -1 --format=%H [OK]
Hint: Use git log with format %H for full commit hash [OK]
Common Mistakes:
  • Using non-existent git commands
  • Confusing commit message commands with hash commands
  • Trying to use sha1sum directly on HEAD
3. Given the command git hash-object README.md, what is the expected output?
medium
A. An error because README.md is not a commit
B. The size of README.md in bytes
C. The last commit hash that modified README.md
D. The SHA-1 hash of the README.md file content

Solution

  1. Step 1: Understand git hash-object command

    This command computes and outputs the SHA-1 hash of the file content given (README.md here).
  2. Step 2: Verify output meaning

    It does not show file size, commit hashes, or errors for valid files.
  3. Final Answer:

    The SHA-1 hash of the README.md file content -> Option D
  4. Quick Check:

    git hash-object outputs file content SHA-1 [OK]
Hint: git hash-object hashes file content, not commits [OK]
Common Mistakes:
  • Expecting commit hash instead of file hash
  • Thinking it outputs file size
  • Assuming it errors on normal files
4. You run git hash-object -w file.txt but get an error. What is the most likely cause?
medium
A. The -w option is not valid for git hash-object
B. The file.txt does not exist in the current directory
C. You need to commit before hashing
D. The file.txt is already tracked by Git

Solution

  1. Step 1: Understand git hash-object -w usage

    The -w option writes the object to Git's object database; it requires the file to exist.
  2. Step 2: Identify cause of error

    If file.txt does not exist, the command errors. Other options are incorrect because -w is valid, committing is not required, and tracking status doesn't cause this error.
  3. Final Answer:

    The file.txt does not exist in the current directory -> Option B
  4. Quick Check:

    Missing file causes git hash-object -w error [OK]
Hint: Check file existence before git hash-object -w [OK]
Common Mistakes:
  • Thinking -w is invalid option
  • Assuming commit is required before hashing
  • Believing tracked files cause this error
5. You want to verify if a file's content matches a specific Git object SHA-1 hash. Which sequence of commands correctly helps you do this?
hard
A. Run git show SHA1_HASH and compare with file.txt content manually
B. Run git diff SHA1_HASH file.txt to see differences
C. Run git hash-object file.txt and compare output with the SHA-1 hash
D. Run git cat-file -p file.txt to get the hash

Solution

  1. Step 1: Understand how to get file's SHA-1 hash

    Use 'git hash-object file.txt' to compute the SHA-1 hash of the file content.
  2. Step 2: Compare computed hash with given SHA-1

    If the hashes match, the file content matches the Git object.
  3. Final Answer:

    Run git hash-object file.txt and compare output with the SHA-1 hash -> Option C
  4. Quick Check:

    Compare file hash with SHA-1 using git hash-object [OK]
Hint: Use git hash-object to get file hash for comparison [OK]
Common Mistakes:
  • Using git show to compare file content directly
  • Trying git diff with SHA-1 hash and file
  • Using git cat-file incorrectly on files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard