Advanced Git Commands You Need to Master (With Examples)

Git isn’t just about clone, commit, and push. It has powerful features that can rewrite history, find bugs faster, and recover lost work.
In this guide, we’ll walk through essential advanced Git commands with clear explanations and real-world examples.


1. Git Rebase – Rewrite and Streamline Commit History

The git rebase command moves commits from one branch and applies them onto another. It’s often used to create a cleaner, linear commit history.

# Rebase feature branch onto main
git checkout feature-branch
git rebase main

Interactive Rebase lets you squash, edit, or reorder commits:

git rebase -i HEAD~4

In the editor, change pick to:

  • squash – merge commits into one
  • edit – change the commit message or content
  • reword – change only the commit message

💡 Use case: Cleaning up messy commits before merging into the main branch.


2. Git Reflog – Recover Lost Commits

git reflog records every change to HEAD, even those not in the current branch history.

# Show all changes to HEAD
git reflog

# Restore HEAD to a previous position
git reset --hard HEAD@{5}

💡 Use case: You accidentally ran git reset --hard and lost changes — reflog can bring them back.


3. Git Reset – Undo Changes in Multiple Ways

git reset moves HEAD and changes the staging area and working directory depending on the mode:

# Soft reset: Keep changes staged
git reset --soft HEAD~1

# Mixed reset: Keep changes unstaged (default)
git reset HEAD~1

# Hard reset: Discard all changes
git reset --hard HEAD~1

💡 Use case: Uncommit the last commit or fully roll back to an earlier version.


4. Git Revert – Safely Undo a Commit

Unlike reset, git revert doesn’t remove commits — it creates a new commit that reverses the changes.

git revert abc123d

💡 Use case: Undo a commit in a shared branch without rewriting history.


5. Git Stash – Save Work Without Committing

git stash stores changes temporarily so you can switch branches or pull updates without committing.

# Save your changes
git stash

# Apply the last stash and remove it from stash list
git stash pop

# Show all stashes
git stash list

💡 Use case: Pause work to fix an urgent bug on another branch.


6. Git Bisect – Find the Commit That Introduced a Bug

git bisect uses binary search to quickly find the commit that introduced a problem.

git bisect start
git bisect bad             # Mark current commit as bad
git bisect good abc123d    # Mark an earlier commit as good

Git checks out a middle commit for testing. You mark it good or bad until the culprit is found.

💡 Use case: Debugging when you know the bug didn’t exist in earlier versions.


7. Git Cherry-pick – Apply Specific Commits

git cherry-pick applies a commit from one branch to another without merging the whole branch.

git cherry-pick abc123d

💡 Use case: Backport a bug fix from main to a release branch.


8. Git Clean – Remove Untracked Files

git clean deletes files and directories not tracked by Git.

# Preview what will be deleted
git clean -n

# Delete untracked files
git clean -fd

💡 Use case: Remove build artifacts or temporary files before committing.


9. Git Archive – Create a Project Snapshot

git archive creates a tar or zip file of a specific branch, tag, or commit.

# Create a ZIP archive of the current branch
git archive --format=zip --output=project.zip HEAD

💡 Use case: Share a clean copy of your project without .git history.


10. Git Submodule – Manage External Repositories

Submodules let you include other Git repositories inside your project.

# Add a submodule
git submodule add https://github.com/example/library.git libs/library

💡 Use case: Embed third-party libraries that are managed in separate repositories.


11. Git Blame – Find Who Changed a Line

git blame shows who last edited each line in a file and in which commit.

git blame app.py

💡 Use case: Track down the source of a bug and ask the original author about the change.


12. Git Grep – Search Inside Tracked Files

git grep searches for a string in tracked files, much faster than grep in large repos.

git grep "def my_function"

💡 Use case: Quickly find function definitions or references in the codebase.


13. Git Tag – Mark Important Points in History

Tags are permanent labels for specific commits, often used for releases.

# Create a tag
git tag v1.0.0

# Push tags to remote
git push --tags

💡 Use case: Tag releases so you can easily refer to them later.


Final Thoughts

These commands go beyond the basics and give you full control over your Git repository.
By mastering them, you can:

  • Recover lost work
  • Debug faster
  • Keep your repo clean and organized
  • Manage complex development workflows

You Might Also Like

🛠️ Recommended Tools for Developers & Tech Pros

Save time, boost productivity, and work smarter with these AI-powered tools I personally use and recommend:

1️⃣ CopyOwl.ai – Research & Write Smarter
Write fully referenced reports, essays, or blogs in one click.
✅ 97% satisfaction • ✅ 10+ hrs saved/week • ✅ Academic citations

2️⃣ LoopCV.pro – Build a Job-Winning Resume
Create beautiful, ATS-friendly resumes in seconds — perfect for tech roles.
✅ One-click templates • ✅ PDF/DOCX export • ✅ Interview-boosting design

3️⃣ Speechify – Listen to Any Text
Turn articles, docs, or PDFs into natural-sounding audio — even while coding.
✅ 1,000+ voices • ✅ Works on all platforms • ✅ Used by 50M+ people