Git is a distributed version control system used to track changes in source code.
Installation
# Debian / Ubuntu
sudo apt install git
# RHEL / CentOS / Fedora
sudo dnf install git
# Arch Linux
sudo pacman -S git
# macOS
brew install git
# Verify installation
git --versionSetup and Configuration
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "nvim" # Set default editor
git config --global init.defaultBranch main # Set default branch name
git config --list # List all configurationsBasic Workflow
git init # Initialize a new Git repository
git status # Check status of modified files
git add . # Stage all changes in current directory
git add file.txt # Stage a specific file
git commit -m "Commit message" # Commit staged changes
git commit -am "Commit message" # Stage tracked files and commit in one stepBranching
git branch # List all local branches
git branch -a # List all branches (local + remote)
git branch new-feature # Create a new branch
git switch new-feature # Switch to a branch (modern)
git switch -c new-feature # Create and switch in one step (modern)
git checkout new-feature # Switch to a branch (legacy)
git checkout -b new-feature # Create and switch in one step (legacy)
git merge new-feature # Merge 'new-feature' into current branch
git branch -d new-feature # Delete a branch (only if merged)
git branch -D new-feature # Force delete a branch (even if unmerged)Remote Repositories
git clone https://github.com/user/repo.git # Clone a repository
git remote -v # List remote repositories and their URLs
git remote add origin <url> # Add a remote named 'origin'
git fetch # Download objects and refs from remote (no merge)
git pull # Fetch and merge from remote
git push origin main # Push local 'main' branch to remote
git push -u origin main # Push and set upstream (first push)
git push --force-with-lease # Force push safely (fails if remote has new commits)History and Logging
git log # View full commit history
git log --oneline # Compact one-line-per-commit view
git log --graph --oneline --all # Visual graph of all branches
git log -n 5 # Show only the last 5 commits
git diff # Show unstaged changes
git diff --staged # Show staged changes (about to be committed)
git show <commit-hash> # Show changes from a specific commit
git blame file.txt # Show who last modified each lineStash
Temporarily save uncommitted changes without committing them.
git stash # Stash all uncommitted changes
git stash -m "work in progress" # Stash with a descriptive message
git stash list # List all stashes
git stash pop # Apply the latest stash and remove it
git stash apply # Apply the latest stash (keep it in the list)
git stash apply stash@{2} # Apply a specific stash
git stash drop stash@{0} # Delete a specific stash
git stash clear # Delete all stashesRebase
Reapply commits on top of another base branch (alternative to merge).
# Rebase current branch onto main
git rebase main
# Interactive rebase (edit, squash, reorder last 3 commits)
git rebase -i HEAD~3
# Abort a rebase in progress
git rebase --abort
# Continue after resolving conflicts
git rebase --continueCherry-pick
Apply a specific commit from another branch to the current branch.
git cherry-pick <commit-hash> # Apply a specific commit
git cherry-pick --no-commit <hash> # Apply changes without creating a commitTags
git tag # List all tags
git tag v1.0.0 # Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0" # Create an annotated tag with a message
git push origin v1.0.0 # Push a specific tag to remote
git push origin --tags # Push all tags to remote
git tag -d v1.0.0 # Delete a local tag
git push origin --delete v1.0.0 # Delete a remote tagUndoing Changes
git restore file.txt # Discard local changes in a file
git restore --staged file.txt # Unstage a file (keep changes)
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --mixed HEAD~1 # Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1 # Undo last commit and DELETE all changes
git revert <commit-hash> # Create a new commit that undoes a specific commitSubmodules
git submodule add <url> path/to/dir # Add a submodule
git submodule update --init --recursive # Initialize and fetch submodule content
git submodule update --remote # Update submodules to latest remote commit