7 Git Commands You'll Love

7 Git Commands You'll Love
April 24, 2023

As a developer, you know that Git can be a lifesaver when managing your projects. Yet, even if you've been using Git for years, there's always more to discover.

That's why we've compiled this list of 7 excellent Git commands you might not know about.

These hidden gems can help you save time, work more efficiently, and create a smoother Git workflow. So, whether you're a Git newbie or a seasoned pro, get ready to fall in love with these excellent commands.

Make Your Life Easier with Cherry Pick

Picture this: you're working on a project with multiple branches and need to apply a specific commit from one branch to another. Sure, you could merge the entire branch, but there are better solutions than that. Instead, you can use the git cherry-pick command to apply a single commit from one branch to another.

For example, let's say you have a bug fix in branch bugfix that you want to apply to the main branch. You can do this by running:

git checkout main
git cherry-pick [commit-hash]

Where [commit-hash] is the commit's hash you want to apply. Voilà! You've just cherry-picked a commit, saving time and tidying your branches.

Switch Gears with Ease Using Stash

Imagine you're working on a new feature branch when suddenly, you need to switch gears and fix a bug on another branch. You could commit to your half-finished work, but that could be better. Instead, use the git stash command to save your changes and clean your commit history.

To stash your changes, run:

git stash save "Your stash message."

Now, you can switch branches, fix that pesky bug, and then return to your feature branch by running:

git stash apply

With git stash, you can keep your branches organized and your commits neat, even when juggling multiple tasks.

Find Bugs Quickly with Bisect

Finding a bug can be like looking for a needle in a haystack when it comes to large codebases. But don't worry, git bisect is here to help! This powerful command allows you to perform a binary search through your commit history, pinpointing the exact commit that introduced the bug.

Start by marking a good commit (without the bug) and a bad commit (with the bug):

git bisect start
git bisect good [good-commit-hash]
git bisect bad [bad-commit-hash]

Git will now start bisecting the commits and checking out the middle one for you to test. Based on the outcome, mark the commit as good or bad:

git bisect good
# or
git bisect bad

Keep marking commits until Git narrows down the culprit. Once you've found the problematic commit, run git bisect reset to return to your original branch. You'll be squashing bugs in no time!

Clean Up Your Commit History with Rebase

Let's face it—sometimes, our commit history can get messy. Thankfully, git rebase -i (interactive rebase) is here to help you clean it up. This command lets you pick, edit, or squash your commits, creating a polished and organized commit history.

To use interactive rebase, run:

git rebase -i [commit-hash]^
# or
git rebase -i main

Where [commit-hash] is the commit's hash you want to start rebasing from. This will open up an editor where you can modify your commit history by picking, editing, or squashing commits.

For instance, to combine two commits into one, replace pick with squash (or s) for the second commit in the editor. Save your changes and close the editor. Git will then open another editor to create a new commit message for the squashed commits. Once you've crafted your message, save and close the editor. You've just made your commit history cleaner and more understandable!

Recover Lost Commits with Reflog

Accidentally deleted a branch or commit? Don't panic! The git reflog command has got your back. This command displays a log of all your actions on your repository, including branch and commit deletions.


git reflog
5d8932c (HEAD -> main) HEAD@{0}: commit: Update README.md
3a5b0de HEAD@{1}: commit: Add LICENSE file
a1b2c3d HEAD@{2}: checkout: moving from feature-branch to main
b7d654e HEAD@{3}: commit: Fix bug in login feature
e8f7d6c HEAD@{4}: commit: Add login feature
a1b2c3d HEAD@{5}: checkout: moving from main to feature-branch
a1b2c3d HEAD@{6}: commit: Initial commit

Look for the hash of the deleted branch or commit in the reflog output. Once you've found it, you can recover the lost branch or commit by running:

git checkout -b [new-branch-name] [commit-hash]

With git reflog, you can breathe a sigh of relief knowing you can recover from accidental deletions.

Discover the Culprit with Blame

Who made changes to a specific line of code? Look no further than the git blame command. This handy command shows you the commit hash, author, and date of the last change to each line of code. It's handy when you need to trace the origins of a bug or identify who made a particular change to your codebase.

To use git blame, run:

git blame [file]
^b71b6d8 (John Doe 2022-01-01 15:23:45 -0500 1) def greet(name):
^b71b6d8 (John Doe 2022-01-01 15:23:45 -0500 2)     print(f"Hello, {name}!")
^e8d21b1 (Jane Smith 2022-01-02 10:12:34 -0500 3)
^e8d21b1 (Jane Smith 2022-01-02 10:12:34 -0500 4) def farewell(name):
^e8d21b1 (Jane Smith 2022-01-02 10:12:34 -0500 5)     print(f"Goodbye, {name}!")

Where [file] is the file you want to inspect. You'll get a detailed breakdown of the changes made to each line, so you can quickly pinpoint the source of any issues.

Work on Multiple Branches at Once with Worktree

Working on multiple features or bug fixes at the same time? The git worktree command lets you work on multiple branches simultaneously without affecting your original working directory. It's perfect for multitaskers who want to stay organized.

To create a new working directory for a different branch, run the following:

git worktree add [path] [branch]

Where [path] is the location for the new working directory, and [branch] is the branch you want to work on. You can easily switch between working directories and branches, keeping your projects organized and your workflow smooth.

Armed with these 7 amazing Git commands, you're ready to easily tackle even the most complex projects. Remember, practice is the key to mastering Git, so don't be afraid to dive in and experiment with these powerful commands.