Version control

Git basics

The commands you use every day, grouped by what you want to do.

01Start

git init
Make a repository here
git clone <url>
Copy a repository
git config user.name
Set or read your name
git status -sb
Short status with the branch

02Save work

git add -p
Pick the changes to stage, piece by piece
git add <file>
Stage one file
git commit -m "msg"
Save the staged changes
git commit --amend
Fix the last commit
TipOne commit, one idea. A short message in the present tense reads best.

03Look around

git diff
What changed, not yet staged
git diff --staged
What you are about to commit
git log --oneline --graph
History as a small tree
git blame <file>
Who changed each line, and when
git show <commit>
One commit in full

04Branches

git switch -c <name>
Make a branch and move to it
git switch <name>
Move to a branch
git branch -d <name>
Delete a merged branch
git merge <name>
Bring a branch into this one
git rebase <base>
Replay your commits on a new base

05Share

git fetch
Get new commits, change nothing
git pull --rebase
Fetch, then replay your work on top
git push -u origin <name>
Publish a branch and track it
git remote -v
List the remotes
WarningNever force-push a shared branch. Use --force-with-lease on your own branch only.

06Undo

git restore <file>
Throw away edits to a file
git restore --staged <file>
Unstage a file
git revert <commit>
Undo a commit with a new commit
git stash
Put changes aside for later
git stash pop
Bring them back

07Reset, three ways

What git reset <commit> changes.
ModeCommitIndexFiles
--softmoveskeptkept
--mixedmovesresetkept
--hardmovesresetreset
Warning--hard deletes edits you have not committed. They cannot come back.

08Fix a mistake

git reflog
Every place HEAD has been, so you can go back
git switch -c rescue <hash>

Save a lost commit on a new branch.

git cherry-pick <commit>

Copy one commit onto this branch.