Essential Git commands for version control - branching, committing, merging, and collaboration.
Configure Git and set up your identity
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Check your configuration
git config --list
# Set default branch name
git config --global init.defaultBranch main# Set default editor
git config --global core.editor "code --wait"
# Enable color output
git config --global color.ui auto
# Set up credential helper (Windows)
git config --global credential.helper wincredInitialize or clone Git repositories
# Initialize a new repository
git init
# Initialize with specific branch name
git init -b main
# Clone an existing repository
git clone https://github.com/user/repo.git
# Clone into a specific directory
git clone https://github.com/user/repo.git my-folderCommon commands for daily Git usage
# Check repository status
git status
# Add files to staging area
git add file.txt
git add . # Add all files
git add *.js # Add all JavaScript files
# Commit changes
git commit -m "Commit message"
git commit -am "Message" # Add and commit tracked files
# View commit history
git log
git log --oneline # Compact view
git log --graph # Show branch graphCreate and manage branches
# List branches
git branch # Local branches
git branch -a # All branches (local + remote)
# Create a new branch
git branch feature-branch
# Switch to a branch
git checkout feature-branch
git switch feature-branch # Modern alternative
# Create and switch to new branch
git checkout -b feature-branch
git switch -c feature-branch# Merge a branch into current branch
git merge feature-branch
# Delete a branch
git branch -d feature-branch # Safe delete
git branch -D feature-branch # Force delete
# Rename a branch
git branch -m old-name new-nameWork with remote repositories
# List remote repositories
git remote -v
# Add a remote repository
git remote add origin https://github.com/user/repo.git
# Fetch changes from remote
git fetch origin
# Pull changes from remote
git pull origin main
# Push changes to remote
git push origin main
git push -u origin main # Set upstream and push# Remove a remote
git remote remove origin
# Rename a remote
git remote rename old-name new-name
# Change remote URL
git remote set-url origin https://github.com/user/new-repo.gitRevert or reset changes
# Discard changes in working directory
git restore file.txt
git checkout -- file.txt # Old way
# Unstage files
git restore --staged file.txt
git reset HEAD file.txt # Old way
# Amend last commit
git commit --amend -m "New message"
# Revert a commit (creates new commit)
git revert <commit-hash># Reset to previous commit (careful!)
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged
git reset --hard HEAD~1 # Discard all changes
# Reset to specific commit
git reset --hard <commit-hash>Temporarily save changes
# Stash current changes
git stash
git stash save "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply # Keep stash
git stash pop # Apply and remove stash
# Apply specific stash
git stash apply stash@{0}
# Delete stash
git stash drop stash@{0}
git stash clear # Delete all stashesInspect differences and history
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
git diff --cached
# Show changes between commits
git diff commit1 commit2
# Show changes in a specific file
git diff file.txt# Show commit details
git show <commit-hash>
# Show file at specific commit
git show <commit-hash>:file.txt
# Show who changed what in a file
git blame file.txtAdditional helpful Git commands
# Clean untracked files
git clean -n # Dry run
git clean -f # Remove files
git clean -fd # Remove files and directories
# Search in repository
git grep "search term"
# Show who made changes to a file
git log --follow file.txt
# Create alias
git config --global alias.st status
git config --global alias.co checkout# Ignore file changes
git update-index --assume-unchanged file.txt
# Track file changes again
git update-index --no-assume-unchanged file.txt
# Show tracked files
git ls-files