Commonly-used Git Commands
let current working directory to be called “cwd” for short
Basic
init
add
add <file>
add -p <file>
: choose which change to commit
commit
: take a snapshot of the added files
diff
diff <file>
: compare<file>
in cwd and file inHEAD
diff <branch/hash> <file>
: compare<file>
in cwd and file in<branch/hash>
diff <branch1/hash1> <branch2/hash2> <file>
: compare<file>
in<branch1>
and<branch2>
Branch & Merge
branch
branch
: look up all the branch infosbranch <branch>
: create a new branch<branch>
checkout
checkout <branch>
checkout <file>
: copy<file>
inHEAD
to cwd, one way to throw away changescheckout -b <branch>
: same asbranch <branch>; checkout <branch>
merge
merge <branch>
:- if
HEAD
is the ancestor of<branch>
, then promote theHEAD->branch
and merge with<branch>
- else, a merge conflict occurs, and remains to by solved by the user
- if
merge --continue
: tell git the merge has finished]merge --abort
: come back to the state before you call merge
Remote
remote
remote add <remote> <URL>
push
push <remote> <local branch>:<remote branch>
branch --set-upstream-to=<remote branch>
branch -vv
push
clone
clone <URL>
clone --shalow
: only clone the latest snapshot
fetch
fetch <remote>
pull
- = fetch + merge
- you can take it as updating the pointers in addition to the snapshots
Other
one of the key algorithm behind git is the Delta Compression Algorithm
blame
stash
stash pop
bisect
: for debug- the
.gitignore
file: filenames and patterns that you want git to ignore