How do I start a git bisect?

Mastering Git: 10 Advanced Commands

15/03/2020

Rating: 4.68 (14536 votes)

Learning Git can feel like a steep climb, especially when you're first encountering the command line. Many developers stick to the basics: adding and committing files. However, Git offers a vast array of powerful functionalities that can significantly enhance your workflow and problem-solving capabilities. This article delves into 10 advanced Git commands that every developer should have in their toolkit, transforming you from a Git novice into a proficient user.

Before diving in, if you're new to Git fundamentals like adding, committing, and branching, it's recommended to familiarise yourself with those first. For those who prefer a visual approach, a video version of this guide is also available.

### 1. Add/Commit All Files Efficiently

A common task is adding and committing multiple files. While the standard git add . followed by git commit -m "Message" works, there's a more streamlined method. The git commit -a -m "Message" command stages all modified files and commits them in one go. However, this conveniently skips new or deleted files. To include these, use git add -A before committing. The -A flag stages all changes across the entire repository, unlike . which only stages changes in the current directory. For comprehensive staging, git add -A is the superior choice.

### 2. Harness the Power of Aliases

Repetitive commands, like staging and committing all files, can be simplified using Git aliases. Aliases allow you to create custom shorthand commands for complex Git operations. For instance, to create an alias ac for staging all changes and committing, you can use:

git config --global alias.ac '!git add -A && git commit -m'

This command configures your global Git settings, creating the ac alias. When you run git ac "Your Message", it executes git add -A followed by git commit -m "Your Message". The exclamation mark ! is crucial here; it tells Git to execute the command as a shell command, allowing for chained operations.

For simpler commands, like aliasing git commit -a -m "Message" to cam, you can omit the exclamation mark and the git prefix:

git config --global alias.cam "commit -a -m"

This allows you to simply type git cam "Your Message".

### 3. Revert Commits Safely

The git revert command is invaluable for undoing specific commits without rewriting history. Unlike git reset, revert creates a new commit that undoes the changes introduced by a previous commit.

git revert <commit-hash>

For example, git revert 486bdb2 will create a new commit that nullifies the changes made in commit 486bdb2. To revert the most recent commit, you can use:

git revert HEAD

This is a safe way to undo changes, preserving the project's history.

### 4. Navigate with Reflog

The git reflog command provides a history of your local repository's activity, including commits, resets, merges, and more. It's an excellent tool for tracking down mistakes or understanding recent changes.

git reflog

This command lists all the movements of your HEAD and branch pointers, offering a safety net if you accidentally make unwanted changes.

### 5. Enhance Your Commit Logs

Make your commit history more readable and insightful with git log combined with specific flags. The following command provides a concise, graph-based view of your commits, including decorations and one-line summaries:

git log --graph --decorate --oneline

This visual representation is particularly helpful for understanding branching and merging activities.

### 6. Search Your Commit History

Locate specific changes or code snippets within your commit history using git log with the -S flag. This searches for commits that introduced or removed a specific string.

git log -S "A promise in JavaScript is very similar"

This command would find the commit where the specified text was added or modified, proving incredibly useful for debugging or finding the origin of a particular piece of code.

### 7. Manage Work-in-Progress with Stash

When you need to switch contexts rapidly, perhaps to fix an urgent bug, but your current work isn't ready for a commit, git stash is your best friend. It temporarily shelves your uncommitted changes (both staged and unstaged) without creating a commit.

git stash

This command saves your modifications, allowing you to switch branches or pull updates. To reapply your stashed changes later, use:

git stash pop

This command applies the most recent stash and removes it from the stash list. It's an efficient way to handle interruptions without cluttering your commit history.

### 8. Clean Up Local Branches

Over time, your local repository can accumulate branches that have already been merged and deleted from the remote repository. These 'dead' branches can clutter your view. First, prune remote-tracking branches that no longer exist remotely:

git remote update --prune

Then, to remove local branches that are no longer tracked remotely (indicated by : gone]):

git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d

This complex command identifies and deletes these orphaned local branches. Consider creating an alias for this cleanup process for convenience.

### 9. Debug with Git Bisect

Identifying the commit that introduced a bug can be a tedious process, especially in large projects. git bisect automates this by performing a binary search through your commit history. To start:

git bisect start
git bisect bad
git bisect good <commit-hash>

Replace <commit-hash> with a commit hash you know was good (bug-free). Git then automatically checks out a commit roughly in the middle of the good and bad range. You test this commit. If the bug is present, you run git bisect bad; if it's absent, you run git bisect good. Git continues to narrow down the range until it pinpoints the exact commit that introduced the bug. This drastically reduces the time spent on debugging.

### 10. Discard Local Changes

Sometimes, you need to completely discard all local modifications and revert your working directory to a specific state, such as the latest commit on the main branch. The git reset --hard command achieves this:

git reset --hard origin/main

This command forcefully resets your current branch to match origin/main, discarding all uncommitted changes and any local commits not yet pushed. Use this command with caution, as it permanently deletes local changes.

### Conclusion

Mastering these 10 advanced Git commands can significantly boost your productivity and problem-solving skills. From managing your workflow with aliases and stashing to efficiently debugging with bisect and cleaning up your repository, these tools empower you to become a true Git power user. Experiment with these commands to integrate them seamlessly into your development process and elevate your Git proficiency.

How do I start a git bisect?
To start a bisect you need to run three commands. The first command starts the bisect. The second command tells Git which commit is the bad commit with the bug. If you leave this blank, as we have, Git will just use the latest commit. The final command tells Git which commit is known to not have this bug.

If you want to read more articles similar to Mastering Git: 10 Advanced Commands, you can visit the Automotive category.

Go up