Mastering Git: 100 Commands with Real-Time Examples

Mastering Git: 100 Commands with Real-Time Examples

Introduction

Git is a powerful version control tool widely used by developers. Below is a categorized and detailed list of 100+ Git commands.


1. Basic Setup

  1. git init

  2. git config --global user.name "Your Name"

  3. git config --global user.email "email@example.com"

  4. git config --list

  5. git config --global core.editor "code --wait"


2. Repository Management

  1. git clone <repo-url>

  2. git status

  3. git add <file>

  4. git add .

  5. git commit -m "Message"

  6. git commit --amend -m "Updated Message"


3. Branching

  1. git branch

  2. git branch <new-branch>

  3. git branch -d <branch-name>

  4. git checkout <branch-name>

  5. git switch <branch-name>

  6. git switch -c <new-branch>

  7. git merge <branch-name>

  8. git rebase <branch-name>


4. Undoing Changes

  1. git reset --soft HEAD~1

  2. git reset --hard HEAD~1

  3. git revert <commit-hash>

  4. git checkout -- <file>

  5. git clean -f

  6. git stash

  7. git stash pop


5. Collaboration (Remote Operations)

  1. git remote add origin <repo-url>

  2. git remote -v

  3. git fetch

  4. git pull origin <branch>

  5. git push origin <branch>

  6. git push --force

  7. git push --tags

  8. git remote remove origin


6. Tagging

  1. git tag <tag-name>

  2. git tag -a <tag-name> -m "Message"

  3. git show <tag-name>

  4. git push origin <tag-name>


7. Viewing Changes

  1. git diff

  2. git diff --staged

  3. git log

  4. git log --oneline

  5. git show <commit-hash>

  6. git blame <file>

  7. git reflog

  8. git shortlog


8. Advanced History Commands

  1. git cherry-pick <commit-hash>

  2. git cherry

  3. git bisect

  4. git log --graph

  5. git log -p

  6. git describe

  7. git log --author="<author-name>"

  8. git log --grep="keyword"


9. Interactive Features

  1. git rebase -i HEAD~3

  2. git commit --squash <commit-hash>

  3. git commit --fixup <commit-hash>

  4. git rebase --autosquash

  5. git checkout -p


10. Patching and Applying Changes

  1. git format-patch <range>

  2. git apply <patch-file>

  3. git am <patch-file>

  4. git diff > diff.patch


11. Aliases

  1. git config --global alias.st status

  2. git config --global alias.co checkout

  3. git config --global alias.br branch

  4. git config --global alias.cm commit


12. Cleanup

  1. git gc

  2. git prune

  3. git clean -fd


13. Submodules

  1. git submodule add <repo-url>

  2. git submodule update

  3. git submodule init

  4. git submodule deinit <path>


14. Hooks

  1. git hooks

  2. git commit-msg

  3. git pre-commit


15. Security

  1. git log --show-signature

  2. git tag -v <tag-name>


16. Worktrees

  1. git worktree add <path> <branch>

  2. git worktree remove <path>


17. Debugging

  1. git fsck

  2. git verify-commit

  3. git verify-tag


18. Ignoring Files

  1. .gitignore files for directories or patterns.

19. Backup and Restore

  1. git bundle create <file.bundle> <branch>

  2. git bundle verify <file.bundle>


20. Collaboration (Code Review)

  1. git diff --word-diff

  2. git diff <commit1>..<commit2>

  3. git diff --name-only


21. Miscellaneous Commands

  1. git archive

  2. git sparse-checkout

  3. git config --global init.defaultBranch main

  4. git diff-tree

  5. git ls-files

  6. git whatchanged


22. Git Help

  1. git help

  2. git help -a

  3. git help config