Version control
Git basics
The commands you use every day, grouped by what you want to do.
01Start
git initMake a repository here
git clone <url>Copy a repository
git config user.nameSet or read your name
git status -sbShort status with the branch
02Save work
git add -pPick the changes to stage, piece by piece
git add <file>Stage one file
git commit -m "msg"Save the staged changes
git commit --amendFix the last commit
TipOne commit, one idea. A short message in the present tense reads best.
03Look around
git diffWhat changed, not yet staged
git diff --stagedWhat you are about to commit
git log --oneline --graphHistory 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 fetchGet new commits, change nothing
git pull --rebaseFetch, then replay your work on top
git push -u origin <name>Publish a branch and track it
git remote -vList 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 stashPut changes aside for later
git stash popBring them back
07Reset, three ways
| Mode | Commit | Index | Files |
|---|---|---|---|
--soft | moves | kept | kept |
--mixed | moves | reset | kept |
--hard | moves | reset | reset |
Warning
--hard deletes edits you have not committed. They cannot come back.08Fix a mistake
git reflogEvery 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.