Git is a distributed version control system used to track changes in source code.
Setup 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 --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 commitBranching
git branch # List all local branches
git branch new-feature # Create a new branch
git checkout new-feature # Switch to the branch
git checkout -b new-feature # Create and switch in one step (older way)
git switch new-feature # Switch to the branch (modern way)
git switch -c new-feature # Create and switch (modern way)
git merge new-feature # Merge 'new-feature' into current branch
git branch -d new-feature # Delete branchRemote Repositories
git clone https://github.com/user/repo.git # Clone a repository
git remote -v # List remote repositories
git remote add origin <url> # Add a remote named 'origin'
git fetch # Download objects and refs from another repo
git pull # Fetch and merge from remote
git push origin main # Push local 'main' branch to remoteHistory and Logging
git log # View commit history
git log --oneline # View compact history
git log --graph --oneline --all # View a visual graph of branches
git diff # Show unstaged differences
git show # Show changes from the last commitUndoing Changes
git restore file.txt # Discard local changes in a file
git reset HEAD file.txt # Unstage a file
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --hard HEAD~1 # Undo last commit, DELETE all changes