📖 Scenario: You are working on a small project stored in a Git repository. You want to make sure the repository is healthy and has no broken or missing objects. This is important to keep your project safe and avoid data loss.
🎯 Goal: Learn how to use the git fsck command to check the integrity of a Git repository and understand its output.
📋 What You'll Learn
Have Git installed on your computer
Have a local Git repository initialized
Basic command line usage knowledge
💡 Why This Matters
🌍 Real World
Developers use <code>git fsck</code> to verify their Git repositories are not corrupted before pushing or deploying code.
💼 Career
Knowing how to check repository health is important for software engineers and DevOps professionals to maintain code integrity.
Progress0 / 4 steps
1
Initialize a Git repository
Open your terminal and create a new directory called myproject. Then, initialize a Git repository inside it by running git init.
Git
Hint
Use mkdir myproject to create the folder, then cd myproject to enter it, and finally git init to start the repository.
2
Create a file and commit it
Inside the myproject directory, create a file named README.md with the text "My project". Then add and commit this file to the Git repository using git add README.md and git commit -m "Initial commit".
Git
Hint
Use echo "My project" > README.md to create the file with content, then add and commit it.
3
Run git fsck to check repository integrity
Run the command git fsck inside the myproject directory to check the repository for any broken or missing objects.
Git
Hint
Simply type git fsck and press Enter to check the repository.
4
Interpret the git fsck output
Observe the output of git fsck. If the repository is healthy, it should show no errors. Print the message "Repository is healthy" if no errors are found.
Git
Hint
Check the exit status of git fsck with $?. If it is 0, print the message.
Practice
(1/5)
1. What is the primary purpose of the git fsck command?
easy
A. To check the integrity and validity of objects in a Git repository
B. To clone a remote Git repository
C. To merge two branches automatically
D. To delete untracked files from the working directory
Solution
Step 1: Understand the function of git fsck
The git fsck command scans the Git repository to verify the integrity of objects like commits, trees, and blobs.
Step 2: Compare with other Git commands
Commands like clone, merge, and clean serve different purposes unrelated to repository integrity checks.
Final Answer:
To check the integrity and validity of objects in a Git repository -> Option A
Quick Check:
Repository integrity check = D [OK]
Hint: Remember: fsck means 'file system check' for Git data [OK]
Common Mistakes:
Confusing git fsck with git clone
Thinking it modifies files instead of checking
Assuming it deletes files or merges branches
2. Which of the following is the correct syntax to run a basic integrity check on your Git repository?
easy
A. git fsck --check
B. git check fsck
C. git fsck
D. git fsck --repair
Solution
Step 1: Identify the correct command syntax
The basic command to check repository integrity is simply git fsck without extra flags.
Step 2: Understand invalid options
Options like --check or --repair do not exist for git fsck, and git check fsck is not a valid Git command.
Final Answer:
git fsck -> Option C
Quick Check:
Basic integrity check command = B [OK]
Hint: Use just 'git fsck' to check integrity, no extra flags needed [OK]
Common Mistakes:
Adding non-existent flags like --check or --repair
Reversing command order like 'git check fsck'
Confusing with other Git commands
3. You run git fsck on your repository and see the output:
dangling commit 3a1b2c4d5e6f7g8h9i0j
What does this output mean?
medium
A. The commit is currently checked out in your working directory
B. The repository has corrupted files that must be deleted
C. The repository is fully healthy with no issues
D. There is a commit object not reachable from any branch or tag
Solution
Step 1: Understand 'dangling commit' meaning
A dangling commit is a commit object that exists in the repository but is not referenced by any branch, tag, or other refs.
Step 2: Interpret the output context
This does not mean corruption or errors; it means the commit is orphaned but still present.
Final Answer:
There is a commit object not reachable from any branch or tag -> Option D
Quick Check:
Dangling commit = orphaned but intact commit = A [OK]
Hint: Dangling means unreferenced, not broken [OK]
Common Mistakes:
Assuming dangling means corrupted
Thinking the commit is currently active
Ignoring the meaning of unreachable objects
4. You ran git fsck and got this error:
error: object file .git/objects/ab/cdef1234... is corrupt
What should you do to fix this problem?
medium
A. Restore the corrupted object from a backup or fetch from remote
B. Run git fsck --full to repair the corruption automatically
C. Delete the entire repository and clone it again
D. Ignore the error; it will not affect your repository
Solution
Step 1: Understand the error meaning
The error indicates a corrupted object file in the Git repository, which can cause problems.
Step 2: Choose the correct fix approach
Since git fsck only checks and does not repair, you should restore the corrupted object from a backup or fetch it from a remote repository.
Final Answer:
Restore the corrupted object from a backup or fetch from remote -> Option A
Quick Check:
Corruption fix = restore or fetch = C [OK]
Hint: Restore corrupted objects from backup or remote, fsck only checks [OK]
Common Mistakes:
Expecting git fsck to repair corruption automatically
Ignoring corruption errors
Deleting repo unnecessarily without backup
5. You want to ensure your Git repository is fully healthy before a big release. Which sequence of commands correctly uses git fsck and related steps to check and fix repository integrity?
hard
A. Run git fsck --repair to fix all errors automatically, then commit changes
B. Run git fsck, then if errors appear, restore corrupted objects from backup or remote, then run git fsck again
C. Run git fsck and delete all dangling commits to clean the repo
D. Run git fsck and ignore any warnings about dangling objects
Solution
Step 1: Use git fsck to check repository integrity
Start by running git fsck to detect any corrupted or missing objects.
Step 2: Fix detected errors properly
If errors appear, restore corrupted objects from backup or fetch from remote repositories. Dangling commits usually do not need deletion.
Step 3: Verify fixes by rerunning git fsck
Run git fsck again to confirm the repository is clean and healthy before release.
Final Answer:
Run git fsck, then if errors appear, restore corrupted objects from backup or remote, then run git fsck again -> Option B
Quick Check:
Check, fix, recheck = A [OK]
Hint: Check first, fix from backup, then recheck with git fsck [OK]
Common Mistakes:
Expecting automatic repair with non-existent flags
Deleting dangling commits unnecessarily
Ignoring warnings that could cause issues later
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.