Git Basics

Installation

sudo apt-get update \
&& sudo apt-get install -y git
git config --global user.name "Hyeonki Hong"
git config --global user.email "hhk7734@gmail.com"
git config --global core.eol lf \
&& git config --global core.editor vim \
&& git config --global credential.helper 'cache --timeout=86400'

macOS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์•„์ด๋”” ์•”ํ˜ธ๋ฅผ ๊ธฐ์–ตํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

git config --global credential.helper osxkeychain

clone

git clone <repository>
git clone <repository> -b <tag or branch> --single-branch
git clone <repository> -b <tag or branch> --depth <# of commit>

add, rm, mv, commit

add

git add ๋ช…๋ น์–ด๋Š” Untracked file, Modified file ์„ Staging area์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

git add [file]
git add .

rm

git rm ๋ช…๋ น์–ด๋Š” Tracked file์„ Untracked file๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  Staging area์— ์‚ญ์ œ๋œ ์ƒํƒœ๋ผ๋Š” ๊ฒƒ์„ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

git rm [file]

ํŒŒ์ผ์ด ๋ณ€๊ฒฝ๋˜์–ด Staging area์— ์žˆ๋Š” ๊ฒฝ์šฐ -f ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ๊ฐ•์ œ ์‚ญ์ œ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

git rm -f [file]

mv

git mv ๋ช…๋ น์–ด๋Š” ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝ์„ ํ•œ ํ›„ Staging area์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

git mv <file> <new name or directory>

commit

git commit ๋ช…๋ น์–ด๋Š” Staging area์—์„œ ํŒŒ์ผ์„ ์ œ์™ธ์‹œํ‚ค๊ณ , Unmodified file๋กœ ๋ณ€๊ฒฝํ•œ ํ›„ commit๋œ ํ•˜๋‚˜์˜ ์ž‘์—…์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

git commit
git commit -m "<message>"

log

log with commit message

git log [-<# of commit>] [directory or file]

log with commit message + diff

git log -p [-<# of commit>] [directory or file]

log with commit message + statistic

git log --stat [-# of commit] [directory or file]

log with oneline commit message

git log --oneline [--graph] [-# of commit] [directory or file]
git log --pretty=format:"[format]" [--graph] [-# of commit] [directory or file]

reflog

git reflog ๋ช…๋ น์–ด๋กœ git ๋ณ€๊ฒฝ ์ด๋ ฅ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

git reflog

commit hash && HEAD

commit hash๋Š” ํŠน์ • ์ปค๋ฐ‹์„ ๊ฐ€๋ฆฌํ‚ค๋Š” 7f0e7030d57d469d150c17eb4746872e415fb379์™€ ๊ฐ™์€ ํ•ด์‹œ๊ฐ’์ž…๋‹ˆ๋‹ค.

์ปค๋ฐ‹์„ ๊ฐ€๋ฆฌํ‚ฌ๋•Œ ํ•ด์‹œ๊ฐ’์˜ ์•ž์—์„œ๋ถ€ํ„ฐ 7์ž๋ฆฌ ์ •๋„๋งŒ ์‚ฌ์šฉํ•ด๋„ ๊ตฌ๋ณ„์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’ ์ „์ฒด๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

HEAD๋ผ๋Š” ํ‚ค์›Œ๋“œ ์กฐํ•ฉ์œผ๋กœ ํ˜„์žฌ ์ปค๋ฐ‹ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€์  ์œ„์น˜์˜ ์ปค๋ฐ‹์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ๊ทœ์น™์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

reflog์— ๋”ฐ๋ฅธ ์ƒ๋Œ€์ ์ธ ์ปค๋ฐ‹ ์œ„์น˜๋Š” HEAD@{#}์œผ๋กœ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋Œ€์‹  ์ˆซ์ž๋ฅผ ์ ์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

reset

git reset --[soft|mixed|hard] <commit hash>
  • soft: index๋Š” ์œ ์ง€ํ•˜๊ณ , HEAD๋งŒ ํ•ด๋‹น ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค.
  • mixed: index, HEAD๋ฅผ ํ•ด๋‹น ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ˆ˜์ • ๋‚ด์šฉ์€ ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค.
  • hard: ํ•ด๋‹น ์ปค๋ฐ‹ ์‹œ์ ์œผ๋กœ ์™„์ „ํžˆ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค.
Last updated on