A progressive guide to master Git, from your first commits to advanced techniques
git config - Git Configuration
Usage: Defines your identity and Git preferences
# Global configuration
git config --global user.name "DorianABDS"
git config --global user.email "[email protected]"
# Check configuration
git config --list
git config --global --list๐ก Tip: This step is crucial for identifying your contributions!
git init - Create a new repository
Usage: Initializes a Git repository in an existing folder
mkdir my-project
cd my-project
git initโ
Result: Creates a hidden .git folder containing the history
git clone - Copy an existing repository
Usage: Clones a remote repository to your machine
# Clone via HTTPS
git clone https://github.com/user/project.git
# Clone with custom name
git clone https://github.com/user/project.git my-project
# Clone a specific branch
git clone -b develop https://github.com/user/project.gitgit add - Prepare files
Usage: Adds modifications to the index (staging area)
# Add a specific file
git add index.html
# Add all modified files
git add .
# Add all files of a type
git add *.js
# Add interactively
git add -p๐ File states: Untracked โ Staged โ Committed
git commit - Record modifications
Usage: Creates a snapshot of your modifications with a descriptive message
# Commit with message
git commit -m "feat: add login page"
# Add + commit in one command (already tracked files)
git commit -am "fix: fix display bug"
# Modify the last commit
git commit --amend -m "New message"
git commit --amend --no-edit # Keep the message๐ฏ Convention: Use clear and conventional messages!
git status - Repository state
Usage: Shows the current state of your working directory
git status
# Short version
git status -sSymbol legend:
??= UntrackedA= Added (new file)M= ModifiedD= Deleted
git log - Commit history
Usage: Explores the modification history
# Complete history
git log
# One line per commit
git log --oneline
# With branch graph
git log --graph --oneline --all
# Last commits
git log -5
# By author
git log --author="DorianABDS"
# Between two dates
git log --since="2024-01-01" --until="2024-12-31"git diff - Compare modifications
Usage: Visualizes differences between versions
# Unstaged modifications
git diff
# Staged modifications
git diff --staged
# Compare with a commit
git diff HEAD~1
# Compare two branches
git diff main develop
# Compare a specific file
git diff HEAD -- index.htmlgit branch - List and manage branches
Usage: Creation and management of development branches
# List local branches
git branch
# List all branches (local + remote)
git branch -a
# Create a new branch
git branch feature/login
# Delete a branch
git branch -d feature/login # If merged
git branch -D feature/login # Force deletion
# Rename current branch
git branch -m new-name
# Rename any branch
git branch -m old-name new-namegit checkout / git switch - Navigation
Usage: Move between branches or commits
# Classic method (checkout)
git checkout develop
git checkout -b feature/contact # Create + switch
# Modern method (switch) - RECOMMENDED
git switch develop
git switch -c feature/contact # Create + switch
# Return to previous branch
git switch -
# Move to a specific commit
git switch --detach abc1234๐ก Advice: Prefer git switch for navigation, git checkout for restoring files
git merge - Merge branches
Usage: Integrates modifications from one branch into another
# Merge feature/login into current branch
git merge feature/login
# Merge with custom message
git merge feature/login -m "Integration of login system"
# See already merged branches
git branch --merged
git branch --no-merged๐จ In case of conflict:
# 1. Resolve conflicts in your editor
# 2. Mark as resolved
git add conflicted-file.js
# 3. Finalize the merge
git commit -m "resolve: conflict in login system"git remote - Manage remote repositories
Usage: Configures connections to external repositories
# List remotes
git remote -v
# Add a remote
git remote add origin https://github.com/user/repo.git
# Modify a remote URL
git remote set-url origin https://github.com/user/new-repo.git
# Remove a remote
git remote remove origingit fetch - Retrieve without merging
Usage: Updates information about remote branches
# Fetch from origin
git fetch origin
# Fetch all remotes
git fetch --all
# Clean obsolete references
git fetch --prune๐ฏ Difference: fetch retrieves, pull retrieves AND merges!
git pull - Retrieve and merge
Usage: Equivalent to git fetch + git merge
# Pull current branch
git pull
# Pull from a specific branch
git pull origin develop
# Pull with rebase
git pull --rebase origin maingit push - Send modifications
Usage: Publishes your commits to the remote repository
# Push current branch
git push
# Push with upstream definition
git push -u origin feature/login
# Push all branches
git push --all
# Push tags
git push --tagsgit reflog - Action history
Usage: Finds "lost" commits or past actions
# See complete action history
git reflog
# Reflog of a specific branch
git reflog show feature/login
# Recover a "lost" commit
git switch --detach HEAD@{3}
git branch recovery-branch HEAD@{3}๐ Emergency: Your last commit disappeared? git reflog is your friend!
git reset - Undo commits
Usage: Moves HEAD and modifies history
# Undo last commit (keep modifications)
git reset --soft HEAD~1
# Undo and remove modifications
git reset --hard HEAD~1
# Undo to a specific commit
git reset --hard abc1234
# Reset a specific file
git reset HEAD file.js--hard permanently deletes your modifications!
git restore - Restore files
Usage: Restores files to a previous state (replaces git checkout for files)
# Cancel file modifications
git restore index.html
# Restore from index (unstage)
git restore --staged index.html
# Restore from a specific commit
git restore --source=HEAD~1 index.htmlgit revert - Clean cancellation
Usage: Creates a new commit that undoes an existing commit
# Revert a specific commit
git revert abc1234
# Revert multiple commits
git revert HEAD~3..HEAD
# Revert without creating commit immediately
git revert --no-commit abc1234โ Advantage: Preserves history, ideal for shared branches!
git stash - Temporary save
Usage: Sets aside modifications to return to them later
# Save modifications
git stash
# With descriptive message
git stash push -m "WIP: header refactoring"
# Include untracked files
git stash -u
# List stashes
git stash list
# Apply last stash
git stash pop
# Apply a specific stash
git stash apply stash@{1}
# Delete all stashes
git stash clear๐ก Typical usage: Switch branches quickly without commit!
git cherry-pick - Apply selective commits
Usage: Copies a specific commit to the current branch
# Cherry-pick a commit
git cherry-pick abc1234
# Cherry-pick multiple commits
git cherry-pick abc1234 def5678
# Cherry-pick a range
git cherry-pick abc1234..def5678
# Cherry-pick without automatic commit
git cherry-pick --no-commit abc1234git rebase -i - History rewriting
Usage: Modifies, reorganizes, or merges commits interactively
# Interactive rebase of last 3 commits
git rebase -i HEAD~3
# Rebase from a specific commit
git rebase -i abc1234Options in editor:
pick: keep the commitsquash: merge with previousfixup: merge ignoring the messagereword: modify only the messageedit: modify the commitdrop: delete the commit
Edit example:
pick abc1234 feat: add login
squash def5678 fix: typo in login
reword ghi9012 feat: complete system
drop jkl3456 test: debug commit
Conflict management:
# In case of conflict
git status # See conflicts
# Resolve in editor
git add resolved-file.js
git rebase --continue
# Abandon rebase
git rebase --abort
# Once finished, push modifications
git push -f
# If co-worker on same branch, secure their commits
git push --force-with-leasegit blame - Identify author
Usage: Shows who modified each line of a file
# Complete blame of a file
git blame index.html
# With more context
git blame -C index.html
# Between specific lines
git blame -L 10,20 index.htmlgit bisect - Binary search for bugs
Usage: Automatically finds the commit that introduced a bug
# Start bisect
git bisect start
# Mark current version as bad
git bisect bad
# Mark an earlier commit as good
git bisect good abc1234
# Git suggests a commit to test
# After testing, mark as good or bad
git bisect good # or git bisect bad
# Repeat until finding the faulty commit
# End bisect
git bisect resetgit gc - Cleanup and optimization
Usage: Optimizes the repository by compressing objects
# Basic cleanup
git gc
# Aggressive cleanup
git gc --aggressive --prune=now
# Check repository size
git count-objects -vHgit tag - Versioning and releases
Usage: Marks important points in history
# Create a lightweight tag
git tag v1.0.0
# Create an annotated tag (recommended)
git tag -a v1.0.0 -m "Version 1.0.0 - First stable release"
# Tag on a specific commit
git tag -a v0.9.0 abc1234
# List tags
git tag
git tag -l "v1.*"
# See tag details
git show v1.0.0
# Delete a local tag
git tag -d v1.0.0
# Push tags
git push origin v1.0.0 # Specific tag
git push origin --tags # All tags
git push --follow-tags # Tags + commits
# Delete a remote tag
git push origin :refs/tags/v1.0.0git submodule - Nested repositories
Usage: Includes an external Git repository as a subfolder
# Add a submodule
git submodule add https://github.com/user/library.git libs/library
# Clone a project with submodules
git clone --recurse-submodules https://github.com/user/project.git
# Initialize submodules after clone
git submodule update --init --recursive
# Update a submodule
git submodule update --remote libs/library
# Update all submodules
git submodule foreach git pull origin maingit subtree - Alternative to submodules
Usage: Merges an external repository into a subfolder
# Add a subtree
git subtree add --prefix=libs/utils https://github.com/user/utils.git main --squash
# Update a subtree
git subtree pull --prefix=libs/utils https://github.com/user/utils.git main --squash
# Push modifications to the subtree
git subtree push --prefix=libs/utils https://github.com/user/utils.git mainSecure push with --force-with-lease
Usage: Force push that checks remote modifications
# Classic force push (DANGEROUS)
git push --force origin develop
# Secure force push (RECOMMENDED)
git push --force-with-lease origin develop
# With specific commit verification
git push --force-with-lease=develop:abc1234 origin developโ Advantage: Avoids overwriting other developers' modifications!
Branch deletion and cleanup
Usage: Maintains a clean and organized repository
# Delete merged local branches with confirmation
git branch --merged | grep -v "\*\|main\|develop" | xargs -p -n 1 git branch -d
# Delete references to deleted remote branches
git remote prune origin
# See deleted remote branches
git branch -r --merged | grep origin | grep -v master | grep -v develop
# Delete a remote branch
git push origin --delete feature/old-feature# Feature branch
git switch develop
git switch -c feature/new-feature
# ... development ...
git push -u origin feature/new-feature
# ... pull request / merge request ...
# Release branch
git switch -c release/1.2.0 develop
# ... release preparation ...
git switch main
git merge release/1.2.0
git tag -a v1.2.0 -m "Release 1.2.0"
# Hotfix
git switch -c hotfix/critical-bug main
# ... correction ...
git switch main
git merge hotfix/critical-bug
git switch develop
git merge hotfix/critical-bug# Recommended format: type(scope): description
git commit -m "feat(auth): add OAuth login system"
git commit -m "fix(ui): fix header alignment on mobile"
git commit -m "docs(readme): update installation instructions"
# Conventional types:
# feat: new feature
# fix: bug fix
# docs: documentation
# style: formatting, no code change
# refactor: refactoring
# test: adding/modifying tests
# chore: maintenance tasks# Useful aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# Configure merge/diff tools
git config --global merge.tool vimdiff
git config --global diff.tool vimdiff
# Auto-correction of commands
git config --global help.autocorrect 1
# Colors
git config --global color.ui auto# 1. See what happened
git reflog
# 2. Return to a previous state
git reset --hard HEAD@{2}
# 3. Or create a recovery branch
git branch recovery-branch HEAD@{2}# 1. Note the commit hash
git log --oneline -1
# 2. Undo the commit on current branch
git reset --hard HEAD~1
# 3. Go to the right branch
git switch right-branch
# 4. Cherry-pick the commit
git cherry-pick abc1234# WARNING: Rewrites all history!
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch secret-file.txt' \
--prune-empty --tag-name-filter cat -- --all
# Modern version with git-filter-repo (recommended)
git filter-repo --path secret-file.txt --invert-paths- ๐ Official Git Documentation
- ๐ฎ Learn Git Branching
- ๐ Conventional Commits
- ๐ง Git Hooks
"This guide accompanies you in your Git progression. From junior to expert, each command is a step towards mastery!"
โจ Created with passion by DorianABDS
๐ Thanks @DorianABDS for sharing.
๐ก I suggest adding the
-poption when usingxargswhen triggering a destructive command (like the one below that removes local branches merged into the current branch).With the
-poption,xargswill ask for confirmation before removing the branch.Section: Branch deletion and cleanup