Bird
Raised Fist0
Gitdevops~20 mins

SHA-1 hashing concept in Git - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SHA-1 Hashing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does SHA-1 hashing in Git uniquely identify?

In Git, SHA-1 hashes are used to identify objects. What exactly does a SHA-1 hash uniquely identify?

AThe content of a file or commit, ensuring integrity and uniqueness
BThe file name and its location in the repository
CThe date and time when the commit was made
DThe user who made the commit
Attempts:
2 left
💡 Hint

Think about what Git uses to detect changes and avoid duplicates.

💻 Command Output
intermediate
1:30remaining
Output of SHA-1 hash command on a file

What is the output of the following command if the file example.txt contains the text Hello?

git hash-object example.txt
Ada39a3ee5e6b4b0d3255bfef95601890afd80709
B2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
Caaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
De69de29bb2d1d6434b8b29ae775ad8c2e48c5391
Attempts:
2 left
💡 Hint

Check the SHA-1 hash of the string 'Hello' without newline.

❓ Troubleshoot
advanced
2:00remaining
Why does Git show different SHA-1 hashes for identical file content?

You have two files with exactly the same content, but git hash-object shows different SHA-1 hashes for each. What could cause this?

AThe files are in different folders
BThe files have different file names
CGit caches the hash and shows old values
DOne file has different line endings (CRLF vs LF)
Attempts:
2 left
💡 Hint

Think about invisible characters that affect file content.

🔀 Workflow
advanced
2:30remaining
Order of Git object creation and SHA-1 hashing

Arrange the steps Git follows when creating a commit object and calculating its SHA-1 hash.

A1,2,3,4
B2,1,3,4
C1,3,2,4
D3,1,2,4
Attempts:
2 left
💡 Hint

Think about how Git builds from directory snapshot to commit storage.

✅ Best Practice
expert
3:00remaining
Why is SHA-1 considered weak for Git security and what is Git's response?

SHA-1 hashing is known to have vulnerabilities. Why is this a concern for Git, and what approach has Git taken to address it?

ASHA-1 hashes are too long; Git shortened them to 8 characters to save space.
BSHA-1 collisions can allow fake commits; Git is transitioning to SHA-256 hashing for stronger security.
CSHA-1 is slow; Git replaced it with MD5 for faster hashing.
DSHA-1 is proprietary; Git switched to a custom hashing algorithm.
Attempts:
2 left
💡 Hint

Consider cryptographic weaknesses and Git's recent updates.

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