Skip to content

Instantly share code, notes, and snippets.

@RajChowdhury240
Created January 14, 2026 15:43
Show Gist options
  • Select an option

  • Save RajChowdhury240/c5ead31228975bc3789fdf69fab7c498 to your computer and use it in GitHub Desktop.

Select an option

Save RajChowdhury240/c5ead31228975bc3789fdf69fab7c498 to your computer and use it in GitHub Desktop.

Advanced Git Commands Reference

Table of Contents


Rebase Operations

Interactive Rebase

# Rebase last 5 commits interactively
git rebase -i HEAD~5

# Rebase onto another branch
git rebase main

# Continue rebase after resolving conflicts
git rebase --continue

# Skip current commit during rebase
git rebase --skip

# Abort rebase and return to original state
git rebase --abort

# Rebase and preserve merge commits
git rebase -p main

# Rebase with autosquash (automatically squash fixup commits)
git rebase -i --autosquash main

# Rebase onto specific commit
git rebase --onto <new-base> <old-base> <branch>

Rebase vs Merge

# Standard merge (creates merge commit)
git merge feature-branch

# Rebase feature onto main (linear history)
git checkout feature-branch
git rebase main

# Pull with rebase instead of merge
git pull --rebase origin main

Branch Management

Creating and Switching Branches

# Create new branch
git branch feature-name

# Create and switch to new branch
git checkout -b feature-name

# Create and switch (modern syntax)
git switch -c feature-name

# Create branch from specific commit
git branch feature-name <commit-hash>

# Create orphan branch (no history)
git checkout --orphan new-branch

Switching Branches

# Switch to existing branch
git checkout branch-name

# Switch (modern syntax)
git switch branch-name

# Switch to previous branch
git checkout -

# Force switch (discard local changes)
git checkout -f branch-name

Branch Information

# List all branches
git branch -a

# List remote branches
git branch -r

# List branches with last commit
git branch -v

# List branches with more details
git branch -vv

# Show branches merged into current branch
git branch --merged

# Show branches not merged into current branch
git branch --no-merged

# Show branches that contain specific commit
git branch --contains <commit-hash>

Deleting Branches

# Delete merged branch
git branch -d branch-name

# Force delete branch (even if not merged)
git branch -D branch-name

# Delete remote branch
git push origin --delete branch-name

# Prune deleted remote branches from local
git fetch --prune
git remote prune origin

Merge & Conflict Resolution

Merge Strategies

# Standard merge
git merge feature-branch

# Merge without fast-forward (always create merge commit)
git merge --no-ff feature-branch

# Fast-forward only merge (fail if not possible)
git merge --ff-only feature-branch

# Squash merge (combine all commits into one)
git merge --squash feature-branch

# Use specific merge strategy
git merge -s recursive feature-branch
git merge -s ours feature-branch
git merge -s theirs feature-branch

Conflict Resolution

# Show files with conflicts
git status

# Show conflicts in diff format
git diff

# Show conflicts with more context
git diff --conflict=diff3

# Accept incoming changes for a file
git checkout --theirs <file>

# Accept current branch changes for a file
git checkout --ours <file>

# Use merge tool
git mergetool

# Mark conflict as resolved
git add <resolved-file>

# Continue merge after resolving
git merge --continue

# Abort merge
git merge --abort

Advanced Merge Options

# Merge with custom commit message
git merge -m "Custom message" feature-branch

# Merge and edit commit message
git merge --edit feature-branch

# Merge without committing
git merge --no-commit feature-branch

# Show what would be merged without merging
git merge --no-commit --no-ff feature-branch
git diff --cached
git merge --abort

HEAD Management

Understanding HEAD

# Show what HEAD points to
git symbolic-ref HEAD

# Show HEAD commit
git show HEAD

# Show previous HEAD positions
git reflog show HEAD

Detached HEAD

# Checkout specific commit (creates detached HEAD)
git checkout <commit-hash>

# Checkout tag (creates detached HEAD)
git checkout v1.0.0

# Create branch from detached HEAD
git checkout -b new-branch-from-detached

# Return to branch (attach HEAD)
git checkout main

Moving HEAD

# Move HEAD to previous commit
git reset HEAD~1

# Move HEAD without changing working directory
git reset --soft HEAD~1

# Move HEAD and update working directory
git reset --hard HEAD~1

# Move HEAD to specific commit
git reset --hard <commit-hash>

Relative References

# Parent of HEAD
git show HEAD^
git show HEAD~1

# Grandparent of HEAD
git show HEAD^^
git show HEAD~2

# Second parent of merge commit
git show HEAD^2

# Great-grandparent
git show HEAD~3

Logging & History

Basic Logs

# Show commit history
git log

# Show last n commits
git log -n 5

# One line per commit
git log --oneline

# Show with graph
git log --graph

# Show all branches
git log --all

# Pretty format
git log --pretty=format:"%h - %an, %ar : %s"

Advanced Log Formatting

# Detailed graph with decorations
git log --graph --oneline --decorate --all

# Show commits with file changes
git log --stat

# Show commits with patches
git log -p

# Show commits affecting specific file
git log -- path/to/file

# Show commits by author
git log --author="John Doe"

# Show commits in date range
git log --since="2 weeks ago"
git log --after="2024-01-01" --before="2024-12-31"

# Show commits with specific message
git log --grep="fix bug"

# Show commits that added or removed specific string
git log -S "function_name"

# Show commits that changed specific function
git log -L :function_name:file.js

Visualization Options

# Beautiful graph format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

# Show reflog (all reference updates)
git log -g

# Show only merge commits
git log --merges

# Show only non-merge commits
git log --no-merges

# Show commits with word diff
git log -p --word-diff

Searching History

# Find commits that introduced a string
git log -S "search_string" --source --all

# Find commits that changed number of occurrences
git log -G "regex_pattern"

# Show files changed in each commit
git log --name-only

# Show files with status
git log --name-status

# Show diff for specific commit
git show <commit-hash>

# Show specific file from specific commit
git show <commit-hash>:path/to/file

Stashing

Basic Stash Operations

# Stash current changes
git stash

# Stash with message
git stash save "work in progress on feature X"

# Stash including untracked files
git stash -u

# Stash including ignored files
git stash -a

# List all stashes
git stash list

# Show stash contents
git stash show
git stash show -p

# Apply most recent stash
git stash apply

# Apply and remove most recent stash
git stash pop

# Apply specific stash
git stash apply stash@{2}

Advanced Stash Operations

# Create branch from stash
git stash branch new-branch-name

# Stash only staged changes
git stash --keep-index

# Stash with patch mode (interactive)
git stash -p

# Drop specific stash
git stash drop stash@{1}

# Clear all stashes
git stash clear

# Show diff of stash
git diff stash@{0}

Reset & Revert

Reset Types

# Soft reset (keep changes staged)
git reset --soft HEAD~1

# Mixed reset (keep changes unstaged) - default
git reset HEAD~1
git reset --mixed HEAD~1

# Hard reset (discard all changes)
git reset --hard HEAD~1

# Reset specific file
git reset HEAD <file>

# Reset to specific commit
git reset --hard <commit-hash>

Revert Operations

# Revert specific commit (creates new commit)
git revert <commit-hash>

# Revert without committing
git revert -n <commit-hash>

# Revert merge commit
git revert -m 1 <merge-commit-hash>

# Revert range of commits
git revert <oldest-commit>..<newest-commit>

# Continue revert after resolving conflicts
git revert --continue

# Abort revert
git revert --abort

Clean Operations

# Show what would be removed
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files as well
git clean -fdx

# Interactive clean
git clean -i

Cherry-Pick

Basic Cherry-Pick

# Cherry-pick single commit
git cherry-pick <commit-hash>

# Cherry-pick multiple commits
git cherry-pick <commit1> <commit2>

# Cherry-pick range of commits
git cherry-pick <start-commit>..<end-commit>

# Cherry-pick without committing
git cherry-pick -n <commit-hash>

# Cherry-pick and edit commit message
git cherry-pick -e <commit-hash>

Cherry-Pick Conflict Resolution

# Continue after resolving conflicts
git cherry-pick --continue

# Skip current commit
git cherry-pick --skip

# Abort cherry-pick
git cherry-pick --abort

Reflog

Viewing Reflog

# Show reflog for HEAD
git reflog

# Show reflog with dates
git reflog --date=iso

# Show reflog for specific branch
git reflog show branch-name

# Show all reflogs
git reflog show --all

Recovering Lost Commits

# Find lost commit
git reflog

# Checkout lost commit
git checkout <commit-hash>

# Create branch from lost commit
git checkout -b recovered-branch <commit-hash>

# Reset to previous state
git reset --hard HEAD@{2}

Bisect

Binary Search for Bugs

# Start bisect
git bisect start

# Mark current commit as bad
git bisect bad

# Mark known good commit
git bisect good <commit-hash>

# Mark current commit as good
git bisect good

# Skip current commit
git bisect skip

# Automate bisect with script
git bisect run ./test-script.sh

# End bisect
git bisect reset

# Visualize bisect
git bisect visualize

Submodules

Adding Submodules

# Add submodule
git submodule add <repository-url> path/to/submodule

# Add submodule with specific branch
git submodule add -b branch-name <repository-url> path/to/submodule

Working with Submodules

# Initialize submodules
git submodule init

# Update submodules
git submodule update

# Clone repository with submodules
git clone --recursive <repository-url>

# Update all submodules to latest
git submodule update --remote

# Update specific submodule
git submodule update --remote path/to/submodule

# Execute command in all submodules
git submodule foreach 'git pull origin main'

# Show submodule status
git submodule status

Removing Submodules

# Remove submodule
git submodule deinit path/to/submodule
git rm path/to/submodule
rm -rf .git/modules/path/to/submodule

Worktrees

Managing Worktrees

# Create new worktree
git worktree add ../new-worktree branch-name

# Create worktree with new branch
git worktree add -b new-branch ../new-worktree

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../new-worktree

# Prune worktree information
git worktree prune

# Move worktree
git worktree move <worktree> <new-path>

Tags

Creating Tags

# Create lightweight tag
git tag v1.0.0

# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"

# Tag specific commit
git tag -a v1.0.0 <commit-hash> -m "Version 1.0.0"

# Create signed tag
git tag -s v1.0.0 -m "Signed version 1.0.0"

Managing Tags

# List all tags
git tag

# List tags matching pattern
git tag -l "v1.*"

# Show tag information
git show v1.0.0

# Push tag to remote
git push origin v1.0.0

# Push all tags
git push origin --tags

# Delete local tag
git tag -d v1.0.0

# Delete remote tag
git push origin --delete v1.0.0

# Checkout tag
git checkout v1.0.0

Remote Operations

Managing Remotes

# Add remote
git remote add origin <url>

# Show remotes
git remote -v

# Show remote details
git remote show origin

# Rename remote
git remote rename origin upstream

# Remove remote
git remote remove origin

# Change remote URL
git remote set-url origin <new-url>

Fetching and Pulling

# Fetch from remote
git fetch origin

# Fetch all remotes
git fetch --all

# Fetch and prune deleted branches
git fetch --prune

# Pull with rebase
git pull --rebase origin main

# Pull specific branch
git pull origin feature-branch

Pushing

# Push to remote
git push origin main

# Push and set upstream
git push -u origin main

# Force push (dangerous!)
git push --force origin main

# Force push with lease (safer)
git push --force-with-lease origin main

# Push all branches
git push origin --all

# Push tags
git push origin --tags

# Delete remote branch
git push origin --delete branch-name

Tracking Branches

# Set upstream branch
git branch --set-upstream-to=origin/main main

# Push and set upstream
git push -u origin feature-branch

# Show tracking branches
git branch -vv

# Checkout remote branch
git checkout -b local-branch origin/remote-branch

Advanced Configuration

Aliases

# Create alias
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'

# Use alias
git st
git co main

Configuration Scopes

# System-wide configuration
git config --system user.name "Name"

# Global (user) configuration
git config --global user.name "Name"

# Local (repository) configuration
git config --local user.name "Name"

# Show all configuration
git config --list

# Show configuration with origin
git config --list --show-origin

# Unset configuration
git config --global --unset user.name

Useful Configurations

# Set default editor
git config --global core.editor "vim"

# Enable color output
git config --global color.ui auto

# Set default branch name
git config --global init.defaultBranch main

# Configure merge tool
git config --global merge.tool vimdiff

# Configure diff tool
git config --global diff.tool vimdiff

# Automatically prune on fetch
git config --global fetch.prune true

# Rebase on pull by default
git config --global pull.rebase true

# Set credential helper
git config --global credential.helper cache

Advanced Git Commands

# Show file from specific commit
git show <commit>:<file>

# Compare file between branches
git diff branch1 branch2 -- file.txt

# Show who changed each line of file
git blame file.txt

# Show blame with commit details
git blame -L 10,20 file.txt

# Find which commit introduced a bug
git bisect start
git bisect bad HEAD
git bisect good v1.0

# Archive repository
git archive --format=zip --output=project.zip HEAD

# Create patch file
git format-patch -1 <commit>

# Apply patch
git apply patch-file.patch

# Show changes not yet committed
git diff

# Show staged changes
git diff --cached

# Show word-level diff
git diff --word-diff

# Assume file unchanged (ignore local changes)
git update-index --assume-unchanged file.txt

# Undo assume-unchanged
git update-index --no-assume-unchanged file.txt

# Show files assumed unchanged
git ls-files -v | grep '^h'

# Garbage collection
git gc

# Verify repository integrity
git fsck

# Optimize repository
git repack -ad

# Prune unreachable objects
git prune

# Show repository size
git count-objects -vH

Workflow Examples

Feature Branch Workflow

# Create feature branch
git checkout -b feature/new-feature

# Work on feature
git add .
git commit -m "Add new feature"

# Update with main branch
git fetch origin
git rebase origin/main

# Push feature branch
git push -u origin feature/new-feature

# Create pull request (on GitHub/GitLab)
# After approval, merge and delete branch
git checkout main
git pull origin main
git branch -d feature/new-feature
git push origin --delete feature/new-feature

Hotfix Workflow

# Create hotfix branch from main
git checkout main
git checkout -b hotfix/critical-bug

# Fix bug
git add .
git commit -m "Fix critical bug"

# Merge to main
git checkout main
git merge --no-ff hotfix/critical-bug

# Tag release
git tag -a v1.0.1 -m "Hotfix release 1.0.1"

# Merge to develop
git checkout develop
git merge --no-ff hotfix/critical-bug

# Delete hotfix branch
git branch -d hotfix/critical-bug

Undoing Mistakes

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo changes to file
git checkout -- file.txt

# Recover deleted branch
git reflog
git checkout -b recovered-branch <commit-hash>

# Fix last commit message
git commit --amend -m "New message"

# Add to last commit
git add forgotten-file.txt
git commit --amend --no-edit

Tips & Best Practices

  • Always pull before pushing to avoid conflicts
  • Use meaningful commit messages
  • Commit early and often
  • Use branches for features and fixes
  • Never force push to shared branches without team agreement
  • Review changes before committing with git diff
  • Use .gitignore to exclude unnecessary files
  • Keep commits atomic (one logical change per commit)
  • Use interactive rebase to clean up history before merging
  • Regularly prune old branches to keep repository clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment