Debugging
git bisect
Description
It is used to perform a binary search to find the commit that introduced a bug or issue in the codebase. By systematically checking out and testing commits, git bisect
helps to narrow down the exact commit where the problem started. This process significantly speeds up the debugging process, especially in large projects with many commits.
Usage
Good and Bad Commits: The process requires to identify one commit where the bug is present (bad
) and one where the bug is not present (good
). This helps narrow down the range of commits to search.
Automating Tests: We can automate the bisecting process by using scripts. git bisect run
allows to provide a script that will automatically test each commit and mark it as good or bad based on the script's exit status.
Skipping Commits: If we encounter a commit that cannot be tested (e.g., it doesn't compile or crashes), we can skip it. git bisect
will choose another commit to test.
Example Workflow
Example Output
During the bisecting process
git grep
Description
It is used to search through the contents of files in a Git repository. It is similar to the Unix grep
command but specifically optimized for searching within a Git repository. This command is useful for finding specific text patterns, code snippets, or occurrences of keywords across your codebase.
Usage
Options
-i
: Perform a case-insensitive search.
-n
: Show line numbers of matches.
-c
: Count the number of matches in each file.
-v
: Invert match to show lines that do not match the pattern.
-l
: Show only the names of files containing matches.
-L
: Show only the names of files not containing matches.
--and
: Combine multiple patterns with a logical AND
--or
: Combine multiple patterns with a logical OR.
--not
: Exclude matches for a pattern
--recurse-submodules
: Search in submodules recursively
Regular Expressions: git grep
supports basic regular expressions. You can use them to perform more complex searches.
Combining Options: You can combine multiple options to refine your search further.
Exclude Binary Files: git grep
automatically excludes binary files from the search, ensuring that the output remains readable.
What It Does
Searches for Patterns: Finds occurrences of a specified pattern in the files tracked by Git.
Filters by Path: Limits the search to specific directories or files if paths are provided.
Supports Various Options: Offers a range of options to customize the search, such as case-insensitivity, counting matches, and showing line numbers.
Common Use Cases
Example Workflow
Example Output
When running git grep "search_term"
git fsck
Description
It is used to perform an integrity check on a Git repository. It verifies the connectivity and validity of the objects in the database, ensuring that everything is intact and that there are no corrupted objects. This command is particularly useful for diagnosing issues within the repository, such as broken links between commits, missing objects, and other potential data integrity problems.
Usage
Options
--full
: Perform a full check, including looking for missing objects.
--unreachable
: Print objects that are unreachable from any of the reference nodes.
--lost-found
: Write dangling objects into .git/lost-found
--name-objects
: Show object names
--progress
: Show progress information during the check.
--strict
: Enable more strict checking
--dangling
: Show dangling objects
What It Does
Verifies Object Connectivity: Ensures that all objects referenced by commits, trees, and other objects are present and properly linked.
Checks Object Validity: Validates the contents of each object to make sure they are correct and uncorrupted.
Reports Issues: Identifies and reports any issues found during the integrity check, such as dangling commits, missing blobs, or corrupted objects.
Dangling Objects: These are objects (commits, blobs, trees) that are not reachable from any reference. They are not necessarily a problem but might indicate orphaned changes.
Missing Objects: These indicate corruption or incomplete transfer of the repository and need to be addressed to ensure the repository's integrity.
Lost and Found: Objects written to the .git/lost-found
directory are still in the repository but are not reachable by any branch or tag. You can inspect these objects to determine if they are needed.
Common Use Cases
Example Output
When running git fsck
Last updated