Git Tips and Tricks

Git์˜ ๋‹ฌ์ธ์ด ๋˜์ž

1. Git Basics

(1) Git์œผ๋กœ ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ์‹œ์ž‘ : git init

  • ์ž์‹ ์ด ์•ž์œผ๋กœ ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ๊ธฐ๋กœํ•œ TIL ํด๋”๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋•Œ ํ•ด๋‹น ํด๋”๋Š” ์ตœ์ƒ๋‹จ์— ์ƒ์„ฑํ•œ๋‹ค

  • git bash ์—์„œ TIL ํด๋”๋กœ ์ด๋™ํ•œ ์ดํ›„์— ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋กœ git ๊ด€๋ฆฌ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค

git init

(2) Commit์„ ์œ„ํ•œ Staging : git add

  • ํ˜„์žฌ ์ฝ”๋“œ ์ƒํƒœ์˜ ์Šค๋ƒ…์ƒท์„ ์ฐ๊ธฐ ์œ„ํ•œ ํŒŒ์ผ ์„ ํƒ (== Staging Area์— ํŒŒ์ผ ์ถ”๊ฐ€)

git add [ํŒŒ์ผ์ด๋ฆ„] # .์€ ๋ชจ๋“  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ staging area๋กœ ์˜ฌ๋ฆผ

(3) ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์Šค๋ƒ…์ƒท ์ €์žฅ : git commit

  • ํ˜„์žฌ ์ƒํƒœ์— ๋Œ€ํ•œ ์Šค๋ƒ…์ƒท์„ commit ํ•˜์—ฌ, ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

git commit -m "commit message"

(4) ์›๊ฒฉ ์ €์žฅ์†Œ ์ •๋ณด ์ถ”๊ฐ€ : git remote

  • Github ์›๊ฒฉ(remote) ์ €์žฅ์†Œ(repository)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  TIL ํด๋”์™€ ์—ฐ๊ฒฐํ•œ๋‹ค

  • ์ƒˆ๋กœ์šด ์›๊ฒฉ ์ €์žฅ์†Œ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ๋งŒ ์ž…๋ ฅํ•œ๋‹ค.

git remote add origin [github ์›๊ฒฉ ์ €์žฅ์†Œ ์ฃผ์†Œ]

(5) ๋กœ์ปฌ ์ €์žฅ์†Œ๋กœ ์ฝ”๋“œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ git pull

  • Github ์›๊ฒฉ ์ €์žฅ์†Œ์— ์žˆ์€ ์ฝ”๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๊ฐ branch์— ํ•ด๋‹น๋˜๋Š” commit๋“ค์„ ์ ์šฉํ•œ๋‹ค.

git pull

git pull [์›๊ฒฉ ์ €์žฅ์†Œ ์ด๋ฆ„] (specify a remote to pull from)

(5) ์›๊ฒฉ ์ €์žฅ์†Œ๋กœ ์ฝ”๋“œ git push

  • ์ตœ์ข…์ ์œผ๋กœ Github ์›๊ฒฉ ์ €์žฅ์†Œ์— push ํ•œ๋‹ค.

git push

git push origin master

Tip

: ๋งŒ์•ฝ ํ˜„์žฌ branch๊ฐ€ push ํ•˜๋ ค๋Š” repo์˜ branch ์ด๋ฉด $ git push ๋ช…๋ น์–ด๋งŒ ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค!

(6) ๊ทธ ์™ธ ๋ช…๋ น์–ด

  • ํ˜„์žฌ git์˜ ์ƒํƒœ๋ฅผ ์กฐํšŒ git status

git status
  • ๋ฒ„์ „๊ด€๋ฆฌ ์ด๋ ฅ์„ ์กฐํšŒ

git log
  • git ์„ค์ • (user.name & user.email) : ์ตœ์ดˆ 1ํšŒ ์„ค์ •

git config --global user.name "Chloe Kim"
git config --global user.email "juhyun.kim@lindsey.edu"
  • Undo git add before commit

git reset

2. README.md

์›๊ฒฉ(remote) ์ €์žฅ์†Œ(repository)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋งˆํฌ๋‹ค์šด ๋ฌธ์„œ. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• ๋“ฑ์„ ๊ธฐ์žฌํ•œ๋‹ค.

(1) README.md ํŒŒ์ผ ์ƒ์„ฑ

  • README.md ํŒŒ์ผ์„ TIL ํด๋” (์ตœ์ƒ๋‹จ)์— ์ƒ์„ฑํ•œ๋‹ค. ์ด๋ฆ„์€ ๋ฐ˜๋“œ์‹œ README.md๋กœ ์„ค์ •ํ•œ๋‹ค.

touch README.md

(2) (์ž์‹ ๋งŒ์˜) ์›์น™์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ๋‚ด์šฉ ์ถ”๊ฐ€

  • ๋งˆํฌ๋‹ค์šด ์ž‘์„ฑ๋ฒ• pdf์—์„œ ๋ฐฐ์šฐ๊ณ  ์‹ค์Šตํ•œ ๋‚ด์šฉ์„ ํ† ๋Œ€๋กœ README.md ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

  • ํ˜•์‹์€ ์ž์œ ๋กญ๊ฒŒ ์ž‘์„ฑํ•˜๋˜ ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•(์˜๋ฏธ๋ก ์ )์„ ์ง€์ผœ์„œ ์ž‘์„ฑํ•œ๋‹ค.

(3) ์ €์žฅ ํ›„ ๋ฒ„์ „๊ด€๋ฆฌ : add, commit, push

  • ์ž‘์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด commit ์ด๋ ฅ์„ ๋‚จ๊ธฐ๊ณ  ์›๊ฒฉ ์ €์žฅ์†Œ๋กœ push ํ•œ๋‹ค.

git add README.md
git commit -m "add README.md"
git push origin master

3. Shared Repository

Fast forward merge ์‹ค์Šต

# branch ํ™•์ธ
$ git branch

# ์ƒˆ branch ๋งŒ๋“ค๊ธฐ
$ git branch [branch ๋ช…]

# ๋‹ค๋ฅธ branch๋กœ ์ด๋™ํ•˜๊ธฐ
$ git switch [์ด๋™ํ•˜๊ณ ์ž ํ•˜๋Š” branch ๋ช…]

# ๋‹ค๋ฅธ branch ์ด๋™ ver2)
$ git checkout [์ด๋™ํ•˜๊ณ ์ž ํ•˜๋Š” branch ๋ช…]


# branch ์‚ญ์ œํ•˜๊ธฐ
$ git branch -d [์ง€์šฐ๊ณ ์ž ํ•˜๋Š” branch ๋ช…]

# FF(=Fast Forward) merge ํ•˜๊ธฐ
# : ์ฃผ์ฒด๊ฐ€ ๋˜๋Š” branch๋กœ ์ด๋™ ํ›„ ์ง„ํ–‰ (ex. master๋กœ ์ด๋™ ํ›„ chloe ๋งŒ๋“ค๊ธฐ)
$ git merge [๋ณ‘ํ•ฉํ•  branch ์ด๋ฆ„]

# ์ƒˆ branch ๋งŒ๋“ค๋ฉด์„œ ์ด๋™ํ•˜๊ธฐ
$ git switch -c [์ƒˆ๋กœ ๋งŒ๋“ค branch]

# ์ƒˆ branch ๋งŒ๋“ค๋ฉด์„œ ์ด๋™ ver2)
$ git checkout -b ashley  # -b๋Š” branch ๋œปํ•จ


# git log graph๋กœ ๋ณด๊ธฐ
$ git log --oneline --graph

4. ํŠน์ • ์‹œ์ ์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ

ํŠน์ • commit์„ ๊ธฐ์ค€์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ

git checkout f008d8f

test branch ๋งŒ๋“ค๋ฉด์„œ ์ด๋™ํ•˜๊ธฐ

$ git checkout -b test
$ git log --oneline
f008d8f (HEAD -> test) 05 | Article Index
c2be3bc 04 | Article Model
0d28d53 03 | startapp articles
5afc6a9 02 | settings
a046911 01 | startproject

reference log ํ™•์ธ

git reflog

์–ด๋””์— head ์žˆ๋Š”์ง€ ํ™•์ธ

$ git log --oneline
fc58709 (HEAD -> master, origin/master, origin/HEAD) README.md ์ถ”๊ฐ€
fd2e992 06 | Article C with ModelForm
f008d8f (test) 05 | Article Index
c2be3bc 04 | Article Model
0d28d53 03 | startapp articles
5afc6a9 02 | settings
a046911 01 | startproject

๋‹ค์‹œ test branch๋กœ ๋Œ์•„๊ฐ€๊ธฐ

$ git checkout test
Switched to branch 'test'

5. ์›๊ฒฉ ์ €์žฅ์†Œ์— ์˜ฌ๋ผ๊ฐ„ ์ปค๋ฐ‹ ๋˜๋Œ๋ฆฌ๊ธฐ

๋ฐฉ๋ฒ• 1) Local์—์„œ commit ๋˜๋Œ๋ฆฐ ํ›„ ๊ฐ•์ œ Push ํ•˜๊ธฐ

$ git reset ์œผ๋กœ commit ๋˜๋Œ๋ฆฌ๊ธฐ

git reset --hard HEAD       (going back to HEAD)

git reset --hard HEAD^      (going back to the commit before HEAD)

git reset --hard HEAD~2     (going back two commits before HEAD)
git reset --hard HEAD^^    (another syntax for going back two commits)

-f or --force ๋ช…๋ น์–ด๋กœ ๊ฐ•์ œ๋กœ Push ํ•˜๊ธฐ

git push -f origin master

git push --force origin master

๋ฐฉ๋ฒ• 2) git revert ์‚ฌ์šฉํ•˜๊ธฐ

  • ๋ฐฉ๋ฒ• 1์€ ์›๊ฒฉ ์ €์žฅ์†Œ commit history๋ฅผ ๊ฐ•์ œ๋กœ ์กฐ์ž‘ํ•œ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Œ

  • but, git revert๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด commit์„ ๋˜๋Œ๋ฆฌ๋Š” ์ž‘์—…๋„ ํ•˜๋‚˜์˜ commit์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ commit history์— ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋จ

ํŠน์ • commit์—์„œ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ œ๊ฑฐํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ commit ์ƒ์„ฑํ•˜๊ธฐ

git revert --no-commit [๋˜๋Œ๋ฆฌ๊ณ  ์‹ถ์€ commit์˜ hash]     

git revert --no-commmit [๋˜๋Œ๋ฆฌ๊ณ  ์‹ถ์€ ์ปค๋ฐ‹์˜ ๋ฒ”์œ„]

git revert --no-commit Head~3

+

  • master branch์— ์žˆ๋Š” ๋‚ด์šฉ์€ ์ž๋™์œผ๋กœ ๋ฐฐํฌ๊ฐ€ ๋จ

    • master๋Š” ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ

ํŠน์ • branch ๋งŒ clone ํ•˜๊ธฐ

git clone -b {branch_name} --single-branch {์ €์žฅ์†Œ URL}

์›๊ฒฉ ์ €์žฅ์†Œ์˜ ํŠน์ • branch ๊ฐ€์ ธ์˜ค๊ธฐ

์šฐ์„  ์›๊ฒฉ ์ €์žฅ์†Œ์— ์–ด๋–ค branch ์žˆ๋Š”์ง€ ํ™•์ธ

git branch -r

์›๊ฒฉ์ €์žฅ์†Œ + ๋กœ์ปฌ branch ํ™•์ธ

git branch -a

์›๊ฒฉ ์ €์žฅ์†Œ์˜ branch๋ฅผ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

git checkout -t [origin/๊ฐ€์ ธ์˜ฌ ์›๊ฒฉ ์ €์žฅ์†Œ branch ์ด๋ฆ„]

์›๊ฒฉ ์ €์žฅ์†Œ์˜ branch๋ฅผ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๊ธฐ

git checkout -b [์ƒ์„ฑํ•  branch ์ด๋ฆ„][๊ฐ€์ ธ์˜ฌ ์›๊ฒฉ ์ €์žฅ์†Œ branch ์ด๋ฆ„]

Pushํ•˜์ง€ ์•Š์€ Commit message ์ˆ˜์ •ํ•˜๊ธฐ

git commit --amend

git fetch

remote branch ๊ฐ€ ์–ด๋””๊นŒ์ง€ ๊ฐ€์žˆ๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ

  • git pull == git fetch && merge

git rebase

์žฌ ๋ฐฐ์น˜ํ•˜๊ธฐ

  • ๋’ค์— -i ๋ฅผ ๋ถ™์ด๋ฉด interactive ํ•˜๊ฒŒ (๋Œ€ํ™”ํ˜•์œผ๋กœ) ์‹คํ–‰ ๊ฐ€๋Šฅ

6. Submodule ์‚ฌ์šฉํ•˜๊ธฐ

Submodule ์ด๋ž€?

  • Git main repo ์•ˆ์— ๋‹ค๋ฅธ sub repo๊ฐ€ ์žˆ๋Š” ๊ฒƒ

    • main repo์—์„œ๋Š” sub repo๋ฅผ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค!

  • Submodule์„ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ์ ํŠธ ๊ฐ„์— ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” logic์„ ๊ณต์œ  ํ•  ์ˆ˜ ์žˆ๋‹ค!

Add Submodule

git submoduel add [repo ์ฃผ์†Œ]
  • ์ดํ›„์— ๋”ฐ๋กœ git add ์—†์ด commit ํ•  ์ˆ˜ ์žˆ์Œ

Update Submodule

Update main repo

git pull
  • main repo root์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด main repo ๊ฐ€ update ๋จ

Update sub repo

git submodule update --remote --merge
  • main repo์˜ root์—์„œ ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด update๋œ main repo์™€ sub repo์˜ ๊ด€๊ณ„์— ๋”ฐ๋ผ sub repo๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•œ๋‹ค

Last updated