This guide provides an overview of the most commonly used Git commands for version control. It's a great reference for beginners and intermediate developers.
- Setting Up Git
- Creating and Cloning Repositories
- Committing Changes
- Working with Branches
- Pushing and Pulling Changes
- Viewing Changes
- Git Remotes
- Advanced Git Commands
- Git Best Practices
- Troubleshooting
Before using Git, configure your username and email:
# Set your username
git config --global user.name "Your Name"
# Set your email
git config --global user.email "[email protected]"
# Check your configuration
git config --list
# Set default branch name
git config --global init.defaultBranch main
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/username/repository.git
# Clone a specific branch
git clone -b branch-name https://github.com/username/repository.git
# Clone to a specific directory
git clone https://github.com/username/repository.git directory-name
# Check status of working directory
git status
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
# Commit changes
git commit -m "Your commit message"
# Add and commit in one command
git commit -am "Your commit message"
# Amend last commit
git commit --amend -m "New commit message"
# List all branches
git branch # Local branches
git branch -a # All branches including remote
git branch -r # Remote branches only
# Create new branch
git branch branch-name
# Switch to branch
git checkout branch-name
git switch branch-name # New Git syntax
# Create and switch to new branch
git checkout -b branch-name
git switch -c branch-name # New Git syntax
# Delete branch
git branch -d branch-name # Safe delete
git branch -D branch-name # Force delete
# Merge branch
git merge branch-name
# Push changes
git push origin branch-name
git push -u origin branch-name # Set upstream branch
# Pull changes
git pull origin branch-name
# Fetch changes
git fetch origin
git fetch --all
# Update forked repository
git remote add upstream https://github.com/original/repository.git
git fetch upstream
git merge upstream/main
# View commit history
git log
git log --oneline
git log --graph --oneline --decorate
# View changes in working directory
git diff
git diff --staged # View staged changes
# View specific commit
git show commit-hash
# View file history
git blame filename
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/username/repository.git
# Change remote URL
git remote set-url origin https://github.com/username/new-repository.git
# Remove remote
git remote remove origin
# Stash changes
git stash
git stash pop
git stash list
git stash drop
# Reset commits
git reset --soft HEAD~1 # Undo last commit, keep changes
git reset --hard HEAD~1 # Undo last commit, discard changes
# Rebase
git rebase main
git rebase -i HEAD~3 # Interactive rebase
# Cherry-pick
git cherry-pick commit-hash
-
Commit Messages
- Use present tense ("Add feature" not "Added feature")
- Be descriptive but concise
- Start with a capital letter
- Keep under 50 characters for the subject line
-
Branching Strategy
main # Production-ready code ├── develop # Development branch ├── feature/# Feature branches ├── hotfix/ # Urgent fixes └── release/# Release preparation
-
Regular Updates
git fetch origin git rebase origin/main
-
Merge Conflicts
# Abort merge git merge --abort # Resolve conflicts manually, then git add . git commit -m "Resolve merge conflicts"
-
Wrong Branch
# Save changes git stash # Switch branch git checkout correct-branch # Apply changes git stash pop
-
Undo Last Commit
# Keep changes git reset --soft HEAD~1 # Discard changes git reset --hard HEAD~1
# Dependencies
node_modules/
vendor/
# Environment files
.env
.env.local
# Build output
dist/
build/
# IDE files
.vscode/
.idea/
# System files
.DS_Store
Thumbs.db
# Logs
*.log
Remember to always verify your current branch and status before making important changes!