12 Advanced Git Commands Every Developer Should Know

Oct 14, 2024

|

12 min read

Git is a powerful tool that goes far beyond basic commands like git add, git commit, and git push. Once you're familiar with the basics, it's time to dive into more advanced commands that can optimize your workflow. In this post, we'll explore 12 advanced Git commands every developer should know.

Advanced Git Commands

Show Only My Commits with a Colorized Date

Use this command to display only your commits with a nicely colorized date:

git log --all --author="Your Name or Email" --pretty=format:"%C(yellow)%h %C(cyan)%ad%C(reset) | %s" --date=short

Show a Graph of Commits with Branches and Tags

Visualizing your commit history is easy with this command:

git log --graph --oneline --decorate --all

Find the Commit that Introduced a Bug with git bisect

This command helps you find the specific commit that introduced a bug:

git bisect start
git bisect bad # Mark the current commit as bad
git bisect good <commit_hash> # Mark an earlier commit as good

Interactive Rebase to Rewrite Commit History

Need to clean up your commit history? Try an interactive rebase:

git rebase -i HEAD~n

Stash Uncommitted Changes with a Message

When you need to switch branches but don't want to lose your work, stash it:

git stash push -m "Your stash message"

To retrieve it later, use:

git stash apply

Cherry-pick a Specific Commit from Another Branch

Pull a specific commit from a different branch with:

git cherry-pick <commit_hash>

Undo the Last Commit but Keep Changes

Undo your last commit without losing your changes using:

git reset --soft HEAD~1

Show What Files Have Changed Between Two Branches

Easily compare the changes between branches:

git diff branch1..branch2 --name-only

List All the Files That Changed in a Commit

View the files modified in a specific commit:

git diff-tree --no-commit-id --name-only -r <commit_hash>

Squash Multiple Commits into One

Simplify your commit history by squashing several commits into one:

git rebase -i HEAD~n

In the interactive editor, replace pick with squash for the commits you want to combine.

List Untracked Files Only

If you want to see only the files not tracked by Git:

git ls-files --others --exclude-standard

Clean Up Local Branches That Have Been Merged

Get rid of merged branches in your local repo with this command:

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Conclusion

These 12 advanced Git commands will enhance your Git workflow, helping you manage your projects more efficiently. From cleaning up branches to visualizing commit history, mastering these commands can save you significant time and effort.

By mastering advanced Git techniques, you'll improve your productivity and gain a deeper understanding of how to manage your codebase effectively.