TL;DR
What: Track code changes and collaborate with others.
Why: Never lose work, undo mistakes, work with a team.
Quick Start
Install:
macOS:
brew install git
Windows: Download Git for Windows
Linux:
sudo apt install git # Debian/Ubuntu
Configure:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
First repo:
git init
git add .
git commit -m "Initial commit"
Cheatsheet
| Command | Description |
|---|---|
git init | Create a new repo |
git clone URL | Clone a repo |
git status | Check current state |
git add FILE | Stage changes |
git add . | Stage all changes |
git commit -m "msg" | Commit changes |
git push | Push to remote |
git pull | Pull from remote |
git log | View history |
git diff | View changes |
Gotchas
Forgot to add files before commit
git add forgotten-file
git commit --amend
Wrong commit message
git commit --amend -m "New message"
Undo last commit (keep changes)
git reset --soft HEAD~1
Merge conflicts
Edit the conflicting files, then:
git add .
git commit