Git cheatsheet
14 คำสั่งที่ใช้กันทุกอาทิตย์ main วิ่งอยู่ข้างล่าง feature อยู่ข้างบน ◀ คือที่ที่ HEAD อยู่ ชีตจะเล่นทีละคำสั่งวนไป กดตัวไหนก็ได้เพื่อดูซ้ำ
git add -Aย้ายไฟล์ที่แก้ไปพักไว้ (staged) รอ commitเก็บงาน
branch
ย้อนกลับ
ดู
เก็บงาน
git add -Aย้ายไฟล์ที่แก้ไปพักไว้ (staged) รอ commitgit commit -m "C"ของที่ staged กลายเป็น commit ใหม่ branch ขยับตามไปgit stashเก็บงานที่ยังไม่ commit ไว้ข้าง ๆ ไฟล์กลับเป็นเหมือน commit ล่าสุดgit stash popเอางานที่เก็บไว้กลับมา แล้วลบออกจาก stashbranch
git switch -c featureสร้าง branch ใหม่ตรงที่อยู่ แล้วย้าย HEAD ไปที่มันgit switch mainย้าย HEAD ไปอีก branch ไฟล์ในเครื่องเปลี่ยนตามgit merge featureรวมอีก branch เข้ามา ได้ merge commit ที่มีพ่อแม่สองตัว ประวัติเดิมอยู่ครบgit rebase mainยก commit ของ branch นี้ไปต่อท้าย main ทีละตัว ได้ commit ใหม่ (id ใหม่) เส้นตรงgit cherry-pick featureก๊อป commit เดียว (ตัวบนสุดของ feature) มาต่อที่ branch นี้ย้อนกลับ
git revert HEADสร้าง commit ใหม่ที่กลับค่าตัวล่าสุด ประวัติไม่หาย ใช้กับของที่ push แล้วgit reset --hard HEAD~1ถอย branch กลับหนึ่ง commit ทิ้งงานนั้นไปเลย ห้ามใช้กับของที่ push แล้วgit reset --soft HEAD~1ถอย branch กลับหนึ่ง commit แต่ของในนั้นยัง staged อยู่ แก้แล้ว commit ใหม่ได้ดู
git statusบอกว่าอยู่ branch ไหน อะไรแก้แล้ว อะไร staged ไม่เปลี่ยนอะไรgit log --oneline --graph --allวาดกราฟ commit ทุก branch ในเทอร์มินัล ไม่เปลี่ยนอะไรสามคู่ที่คนชอบสับสน
merge หรือ rebase
merge เก็บทั้งสองเส้นไว้แล้วเชื่อมด้วย commit ที่มีพ่อแม่สองตัว rebase ก๊อป commit ของคุณไปต่อบนอีก branch เป็นตัวใหม่ ตัวเก่าถูกทิ้งไว้ เส้นเลยตรง rebase เฉพาะ commit ที่ยังไม่มีใคร pull ไป
revert หรือ reset
revert เพิ่ม commit ที่กลับค่าอีกตัว ใช้กับของที่ push แล้วได้ปลอดภัย reset ถอย branch กลับ --hard ทิ้งงานไปเลย --soft เก็บงานไว้ใน staged reset เฉพาะของที่ยังไม่มีใครมี
commit หายไปแล้ว?
หลัง reset --hard หรือ rebase commit เก่าไม่อยู่บน branch ไหน แต่ยังไม่หายจริง git reflog บอกว่า HEAD เคยไปอยู่ไหนบ้าง แล้ว git switch -c rescue <id> ก็เอากลับมาได้
ตรวจแล้ว
#!/bin/sh
# Every command on the Git cheatsheet, run for real: each one in a fresh repository set up as its "before" picture,
# then one line saying what the repository looks like after (each branch's first-parent line, HEAD, the counts).
# The page's pictures are held to these lines (lib/cheat-git.test.ts). Run: sh git.sh (needs git)
set -eu
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
G() { git -c user.name=mindloop -c user.email=mindloop@example.invalid -c init.defaultBranch=main -c advice.detachedHead=false "$@"; }
commit() { echo "$1" >> notes.txt; G add -A; G commit -q -m "$1"; }
fresh() { rm -rf "$work/r"; mkdir "$work/r"; cd "$work/r"; G init -q; commit A; commit B; }
three() { fresh; commit C; }
diverged() { fresh; G switch -q -c feature; echo F1 > f1.txt; G add -A; G commit -q -m F1; G switch -q main; commit C; G switch -q "$1"; }
edit() { echo "half done" >> notes.txt; }
say() { # what the repository looks like now, in the words of lib/cheat-git.ts summary()
out=""
for b in main feature; do
if G rev-parse -q --verify "refs/heads/$b" >/dev/null; then
line=$(G log --first-parent --reverse --format='%s%x09%p' "$b" | awk -F'\t' '{ n = split($2, p, " "); s = $1; sub(/^Merge branch .*/, "M", s); sub(/^Revert .*/, "R", s); printf "%s%s ", s, (n > 1 ? "(2)" : "") }' | sed 's/ $//')
out="$out$b=$line "
fi
done
staged=$(G diff --cached --name-only | wc -l | tr -d ' ')
changed=$(G diff --name-only | wc -l | tr -d ' ')
stash=$(G stash list | wc -l | tr -d ' ')
echo "$1: ${out}HEAD=$(G branch --show-current) staged=$staged changed=$changed stash=$stash"
}
fresh; edit; G add -A; say add
fresh; edit; G add -A; G commit -q -m C; say commit
fresh; edit; G stash -q; say stash
fresh; edit; G stash -q; G stash pop -q; say pop
fresh; G switch -q -c feature; say switchc
diverged feature; G switch -q main; say switch
diverged main; G merge -q --no-edit feature; say merge
diverged feature; G rebase -q main; say rebase
diverged main; G cherry-pick feature >/dev/null; say pick
three; G revert --no-edit HEAD >/dev/null; say revert
three; G reset -q --hard HEAD~1; say hard
three; G reset -q --soft HEAD~1; say soft
fresh; edit; echo staged > s.txt; G add s.txt; G status --short >/dev/null; say status
diverged main; G log --oneline --graph --all >/dev/null; say log
sh git.sh (ต้องมี git รันจริงใน repo ชั่วคราว แล้วลบทิ้ง)ผลที่พิมพ์ออกมา:add: main=A B HEAD=main staged=1 changed=0 stash=0 commit: main=A B C HEAD=main staged=0 changed=0 stash=0 stash: main=A B HEAD=main staged=0 changed=0 stash=1 pop: main=A B HEAD=main staged=0 changed=1 stash=0 switchc: main=A B feature=A B HEAD=feature staged=0 changed=0 stash=0 switch: main=A B C feature=A B F1 HEAD=main staged=0 changed=0 stash=0 merge: main=A B C M(2) feature=A B F1 HEAD=main staged=0 changed=0 stash=0 rebase: main=A B C feature=A B C F1 HEAD=feature staged=0 changed=0 stash=0 pick: main=A B C F1 feature=A B F1 HEAD=main staged=0 changed=0 stash=0 revert: main=A B C R HEAD=main staged=0 changed=0 stash=0 hard: main=A B HEAD=main staged=0 changed=0 stash=0 soft: main=A B HEAD=main staged=1 changed=0 stash=0 status: main=A B HEAD=main staged=1 changed=1 stash=0 log: main=A B C feature=A B F1 HEAD=main staged=0 changed=0 stash=0
ทุกคำสั่งในชีตนี้รันใน repo ใหม่ที่ตั้งไว้เหมือนภาพ "ก่อน" ของมัน แต่ละบรรทัดคือหน้าตาของ repo หลังรัน แล้ว test ของเว็บเทียบภาพทุกภาพกับบรรทัดพวกนี้