Bird
Raised Fist0
Gitdevops~5 mins

SHA-1 hashing concept in Git - Commands & Configuration

Choose your learning style10 modes available
Introduction
SHA-1 hashing creates a unique code for data to identify it easily. It helps Git track changes and find files quickly without storing duplicates.
When you want to check the unique ID of a file or commit in Git.
When Git needs to verify if a file has changed or stayed the same.
When you want to see the commit history with unique commit IDs.
When you want to share a specific commit by its unique SHA-1 hash.
When Git stores objects like commits, trees, and blobs using SHA-1 hashes.
Commands
This command creates a new Git repository named 'my-repo' to start tracking files and changes.
Terminal
git init my-repo
Expected OutputExpected
Initialized empty Git repository in /home/user/my-repo/.git/
Change directory into the new repository to work inside it.
Terminal
cd my-repo
Expected OutputExpected
No output (command runs silently)
Create a new file named 'file.txt' with the text 'Hello Git' to add to the repository.
Terminal
echo "Hello Git" > file.txt
Expected OutputExpected
No output (command runs silently)
Stage the new file so Git knows to include it in the next commit.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Create a commit that saves the current state of the repository with a message describing the change.
Terminal
git commit -m "Add file.txt with greeting"
Expected OutputExpected
[main (root-commit) 1a2b3c4] Add file.txt with greeting 1 file changed, 1 insertion(+) create mode 100644 file.txt
→
-m - Adds a commit message inline without opening an editor
Show the commit history with short SHA-1 hashes and commit messages to identify commits quickly.
Terminal
git log --oneline
Expected OutputExpected
1a2b3c4 Add file.txt with greeting
→
--oneline - Displays each commit in a single line with short SHA-1 hash
Calculate and show the full SHA-1 hash of the file content to see its unique identifier in Git.
Terminal
git hash-object file.txt
Expected OutputExpected
3b18e7a2a1f4c9d5e6f7a8b9c0d1e2f3a4b5c6d7
Key Concept

If you remember nothing else from this pattern, remember: Git uses SHA-1 hashes to uniquely identify and track every file and commit.

Common Mistakes
Trying to use SHA-1 hashes without committing files first
No commits or objects exist yet, so Git cannot show hashes for them
Add and commit files before checking their SHA-1 hashes
Confusing full SHA-1 hashes with short hashes shown in logs
Short hashes are abbreviations and may not be unique in large repos
Use full SHA-1 hashes when exact identification is needed
Summary
Initialize a Git repository to start tracking files.
Add and commit files to create objects identified by SHA-1 hashes.
Use git log and git hash-object to view SHA-1 hashes for commits and files.

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