What is Git? - ParkEunwoo/seoul-smart-app GitHub Wiki

1. Git ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

  • โœ”๏ธ๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ์œ„ํ•œ ์†Œํ”„ํŠธ์›จ์–ด

    • ๋ฒ„์ „๊ด€๋ฆฌ? -์†Œ์Šค ํ•˜๋‚˜ ๋˜๋Š” ๋ฌถ์Œ์„ ํ•˜๋‚˜์˜ ๋ฒ„์ „์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ๊ด€๋ฆฌ -ํ”„๋กœ์ ํŠธ ์–ด๋–ค ๋ถ€๋ถ„๋„ ๊ฒน์ณ์“ฐ์ง€ ์•Š๊ฒŒ ํ”„๋กœ์ ํŠธ ๋ณ€๊ฒฝ์„ ๊ด€๋ฆฌ -ํŒŒ์ผ/ํด๋”๋ฅผ ์ถ”๊ฐ€/์ˆ˜์ •/์‚ญ์ œํ•˜์—ฌ ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋ฌถ์Œ์„ ๋ฒ„์ „์œผ๋กœ ๊ด€๋ฆฌ
    • -> ex. ์ตœ์ข….ai, ใ„นใ…‡์ตœ์ข….ai, ์ด๊ฑฐ์ง„์งœ์ตœ์ข….ai, ํ•˜์ด๊ฒŒ์ง„์งœ.ai โ€ฆ ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๋ฒ„์ „์„ ์ €์žฅํ•  ํ•„์š” ์—†์ด, ์ €์žฅํ•˜๋ฉด ์ด์ „ file ์œ„์— overwriteํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ version์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•จ
  • โœ”๏ธ๋ถ„์‚ฐ์ €์žฅ์†Œ

    • ๊ฐœ๋ฐœ ํ˜‘์—…์„ ์œ„ํ•ด ์‚ฌ์šฉ -๊ฐ™์€ ๋ชจ๋“ˆ์„ ๊ฐœ๋ฐœํ•˜๋”๋ผ๋„ ์†Œ์Šค๋ฅผ ์„œ๋กœ ๊ณต์œ ํ•˜๋ฉด์„œ ๊ฐœ๋ฐœ -์ „์ฒด ๊ฐœ๋ฐœ ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•˜๋ฉฐ ๊ฐœ๋ฐœํŒŒํŠธ๋ฅผ ๋‚˜๋ˆ„์–ด ๊ณต์œ 
  • โœ”๏ธGitHub๋Š”? : Git ์ €์žฅ์†Œ๋ฅผ ์ง์ ‘ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ , GitHub์„ ํ†ตํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ

    • GitHub-fork : ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ €์žฅ์†Œ๋ฅผ ๊ฐ€์ ธ์™€ ์ œ ์ €์žฅ์†Œ๋กœ ๋งŒ๋“ค์–ด๋†“๋Š” ๊ธฐ๋Šฅ
    • ์˜คํ”ˆ์†Œ์Šค๋ฅผ pull / commit ํ•˜์—ฌ ์‚ฌ์šฉ๊ฐ€๋Šฅ

2. ๊นƒ์˜ ๋™์ž‘ ์›๋ฆฌ

git์€ 4๊ฐ€์ง€ ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

: blob, tree, commit, tag

  1. blob : ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๋‹ด์Œ
  2. tree : ๋””๋ ‰ํ† ๋ฆฌ์˜ ํŒŒ์ผ๋ช…/๋‚ด์šฉ์— ํ•ด๋‹น๋˜๋Š” ๋ธ”๋ฝ์˜ ์ •๋ณด
  3. commit : commit ์ •๋ณด
  4. tag : branch์™€ ๋‹ฌ๋ฆฌ ํŠน์ •๋ฒ„์ „ ์œ„์น˜์— ๋Œ€ํ•ด ๋‚˜์ค‘์— ์ฐพ์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ด๋ฆ„์„ ์ง€์ •ํ•ด๋†“์€๊ฒƒ ๋ฟ!

3. Git branch

: ๋ฒ„์ „๋“ค์„ ๋ฌถ์–ด์„œ branch ๋ผ๊ณ  ํ‘œํ˜„ -> branch๋Š” ์™”๋‹ค๊ฐ”๋‹คํ•˜๋ฉฐ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋‚ด์šฉ์„ ๋ณ€๊ฒฝ, ๋ฒ„์ „๊ด€๋ฆฌ

  • default branch : master //git branch ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ branch ํ™•์ธ ๊ฐ€๋Šฅ
  • ๋‚ด ์ปดํ“จํ„ฐ ๋‚ด์— ์žˆ๋Š” branch : local branch ์™ธ๋ถ€ ์„œ๋ฒ„์— ์žˆ๋Š” branch: remote branch

branch ์ƒ์„ฑ

$ git branch pong
$ git branch
	pong
	* master
$ git checkout pong
Switched to branch 'pong'
$ git branch
	* pong 
	master

/////////////////////////////

//์œ„๋ฅผ ํ•œ๊บผ๋ฒˆ์— ํ•˜๋ ค๋ฉด,
$ git checkout -b pong

4. Git ์šฉ์–ด

  • ์ €์žฅ์†Œ(Repository) : ํ”„๋กœ์ ํŠธ๊ฐ€ live ํ•  ์ˆ˜ ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ/์ €์žฅ๊ณต๊ฐ„
  • ๋ฒ„์ „๊ด€๋ฆฌ(Version Control) : ๊นƒ์˜ ์ฃผ๋ชฉ์ . ํ”„๋กœ์ ํŠธ ํžˆ์Šคํ† ๋ฆฌ์˜ ๋ชจ๋“  ์‹œ์ ์˜ ^์Šค๋ƒ…์ƒท^์„ ์œ ์ง€ํ•˜๋ฏ€๋กœ, ๊ฒฐ์ฝ” ์žƒ์–ด๋ฒ„๋ฆฌ๊ฑฐ๋‚˜ ๊ฒน์ณ์“ฐ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ๊ฒน์ณ์“ฐ๋ฉด ์˜คํžˆ๋ ค ์—๋Ÿฌ
  • ์ปค๋ฐ‹(Commit) : commit์‹œ ๊ทธ ์‹œ์  repo์˜ ^์Šค๋ƒ…์ƒท^์„ ์ฐ์–ด, ํ”„๋กœ์ ํŠธ๋ฅผ ์ด์ „์˜ ์–ด๋– ํ•œ ์ƒํƒœ๋กœ๋“  ์žฌํ‰๊ฐ€/๋ณต์›๊ฐ€๋Šฅํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ธŒ๋žœ์น˜(Branch) : ๋ฉ”์ธ ํ”„๋กœ์ ํŠธ์˜ branch off -> ์ž์‹ ์ด ๋ณ€๊ฒฝํ•˜๊ณ ์‹ถ์€ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฒ„์ „์„ ์ƒ์„ฑ -> ํ”„๋กœ์ ํŠธ์˜ ๋ฉ”์ธ ๋””๋ ‰ํ† ๋ฆฌ์ธ master์— branch๋ฅผ ๋‹ค์‹œ merge.

5. ์ฃผ์š” ๋ช…๋ น์–ด

  • git init : ๊นƒ ์ €์žฅ์†Œ ์ดˆ๊ธฐํ™”. ์ดˆ๊ธฐ, ์ €์žฅ์†Œ๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์—์„œ ์ด ๋ช…๋ น์„ ์‹คํ–‰ํ•œ๋‹ค.
  • git config : configure Set up Git - GitHub Help
  • git help : git help init ๋“ฑ ๊นƒ์˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉ/์„ค์ •ํ•˜๋Š”ํ•ต์‹ฌ์ ์ธ ๋ช…๋ น์–ด๋“ค์„ ๋ณด์—ฌ์ค€๋‹ค.
  • git add
    • index(=stage area, tree๊ตฌ์กฐ)์— object์ด๋ฆ„๊ณผ ์‹ค์ œ ํŒŒ์ผ์ด ์ถ”๊ฐ€๋˜๊ณ (tracked) objects์— blobํƒ€์ž…์œผ๋กœ ํŒŒ์ผ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋จ
    • git add . ์‹œ ์ „์ฒด ํŒŒ์ผ์ด staging ์ƒํƒœ๋กœ ๋ณ€ํ•˜๋ฉฐ, tracked ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
    • ๋ ˆํฌ์— ์ƒˆ file์„ ์ถ”๊ฐ€ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๊นƒ์ด ์ƒˆ file๋“ค์„ ์ง€์ผœ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ! โ€œ์Šค๋ƒ…์ƒทโ€์— ํฌํ•จ๋œ๋‹ค.
  • git pull : local pc์—์„œ ์ž‘์—…ํ•  ๋•Œ, ์ž‘์—…ํ•˜๊ณ ์žˆ๋Š” ์ €์žฅ์†Œ์˜ ์ตœ์‹ ๋ฒ„์ „์„ ์›ํ•  ๋–„ ๊นƒํ—™์œผ๋กœ๋ถ€ํ„ฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. -> ํ˜‘์—…์‹œ git push ํ•˜๊ธฐ ์ „ git pull ์„ ํ•œ ํ›„ ๊ฐ€์ ธ์˜ค์ž.
  • git commit : ์ €์žฅ์†Œ์˜ โ€œ์Šค๋ƒ…์ƒทโ€์„ ์ฐ๊ธฐ ์œ„ํ•ด ์ž…๋ ฅํ•œ๋‹ค. git commit -m โ€œCommit Message"์˜ ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • git push : ๋กœ์ปฌ์—์„œ ์ž‘์—…ํ•˜๊ณ  ๋ณธ์ธ์˜ ์ปค๋ฐ‹์„ ๊นƒํ—™์—์„œ ์˜จ๋ผ์ธ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ์›ํ•˜๊ณ , -> ํ˜‘์—…์‹œ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์š”๊ตฌํ•˜๊ธฐ์œ„ํ•˜์—ฌ ํ•˜๋Š” ์ž‘์—…. ์ด๋•Œ push log๋ฅผ ์ž˜ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜์ž.
  • git status : ์ €์žฅ์†Œ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ช…๋ น์–ด. ํ˜„์žฌ ์–ด๋Š ๋ ˆํฌ์˜ ์–ด๋–ค ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…ํ•˜๊ณ ์žˆ๋Š”์ง€, staginge๋˜์–ด์žˆ๋Š” ํŒŒ์ผ๋“ค์ด edit๋˜์—ˆ๋Š”์ง€ ๋“ฑ์„ ์•Œ๋ ค์ฃผ๋Š” ํšจ์ž์Šค๋Ÿฌ์šด ์ €์žฅ์†Œ์ด๋‹ค. -> workspace, index(stage) ํŒŒ์ผ์˜ ๋‚ด์šฉ๊ณผ ์ตœ์‹  commit obj ์‚ฌ์ด์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ add, commit ํ•  ๊ฒŒ ์—†๋Š”์ง€๋ฅผ ํ™•์ธ์‹œ์ผœ์คŒ
  • git merge : ํ˜„์žฌ branch์—์„œ์˜ ์ž‘์—… ์™„๋ฃŒ ํ›„ master branch๋กœ ๋ณ‘ํ•ฉํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. : ์—ฌ๋Ÿฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฐ์ž ๊ฐœ๋ฐœํ•œ ๋ฒ„์ „์„ ํ•ฉ์น˜๋Š” ๊ฒฝ์šฐ +์„œ๋กœ ๋‹ค๋ฅธ branch ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๊ฒฝ์šฐ
  • git rebase : git merge์™€ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์œผ๋‚˜, merge๋Š” branch ๋ถ„๊ธฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘์ง€๋งŒ(๋‘์ค„), git rebase๋Š” ๋ถ„๊ธฐ๋œ ๊ฒƒ์ด ์ผ๋ ฌ๋กœ ํ•ฉ์ณ์ง„๋‹ค(ํ•œ์ค„) -> git rebase๋ฅผ ํ•˜๋ฉด ์กฐ์ƒ ์ปค๋ฐ‹๊ฐ์ฒด(base)๋ฅผ ์ƒ๋Œ€ ๋ธŒ๋žœ์น˜์˜ ์ตœ์‹  ์ปค๋ฐ‹๊ฐ์ฒด(rebase)๋กœ ๋ฐ”๊พผ๋‹ค.

config -> init -> add -> commit -> push

git config โ€”global user.name โ€œpongsoyunโ€
git config โ€”global user.email โ€œ[email protected]โ€ // ์ตœ์ดˆ 1ํšŒ ์„ค์ •!(global)

mkdir ~/MyProject   // ๋กœ์ปฌ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ ํ›„
cd ~/myproject      // ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™
git init            // ๊นƒ ์ €์žฅ์†Œ ์ดˆ๊ธฐํ™”
git status          // ํ˜„์žฌ ์ƒํƒœ ์ ๊ฒ€
git add example.cpp  // ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ example.cpp ํŒŒ์ผ ์ถ”๊ฐ€
git add .           // ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ชจ๋“  ํŒŒ์ผ ์ถ”๊ฐ€
git commit -m โ€œAdd Exampleโ€ // ์ปค๋ฐ‹ํ•ด์„œ "Add Example" ์„์„ค๋ช…์œผ๋กœํ•˜๋Š” ์Šค๋ƒ…์ƒท์„ ์ฐ๋Š”๋‹ค.

git remote add origin https://github.com/pongsoyun/myproject.git // ๋กœ์ปฌ๊ณผ ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.
git remote -v // ํ˜„์žฌ ํด๋”์˜ ๋กœ์ปฌ๊ณผ ์›๊ฒฉ ์ €์žฅ์†Œ ์—ฐ๊ฒฐ์ƒํƒœ๋ฅผ ํ™•์ธ(์ฃผ์†Œ)
git push origin master // ๊นƒํ—™์œผ๋กœ ํ‘ธ์‰ฌ

์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ ํ‘ธ์‰ฌ ์™„๋ฃŒ -> ํŒ€์›๋“ค์˜ feedback ํ›„ pull request -> merge -> pull ํ•˜์—ฌ ๋กœ์ปฌ์˜ ๋ ˆํฌ๋ฅผ ์ตœ์‹ ์˜ ๊ฒƒ์œผ๋กœ ์œ ์ง€

cf

Files Status

  • ํฌ์žฅ ์ „์ธ ํŒŒ์ผ๋“ค: Unstaged Files
  • ํฌ์žฅํ•˜๊ธฐ๋กœ ํ•œ ํŒŒ์ผ๋“ค : Staged Files
  • ํฌ์žฅ ์™„๋ฃŒ ํŒŒ์ผ ๋ฌถ์Œ: Commit -> ์ƒˆ๋กœ์šด ์ปค๋ฐ‹

.gitignore ์ด๋ž€?

  • .gitignore ํŒŒ์ผ ๋‚ด๋ถ€์— ์˜ฌ๋ฆฌ๊ณ ์‹ถ์ง€ ์•Š์€ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ๊ธฐ์žฌํ•œ๋‹ค. ๊นƒ์ด ์•Œ์•„์„œ ์ œ์™ธํ•œ๋‹ค. ์ฐฉํ•˜๋‹ค

์„  ๋จธ์ง€(Merge) ํ›„ ํ’€๋ฆฌํ€˜(Pull Request)

ํ˜‘์—… ์‹œ ๋งค์šฐ ์ค‘์š”ํ•œ ๋ถ€๋ถ„ Main ํŽ˜์ด์ง€๋ฅผ ๋‹ค ๋งŒ๋“ค๊ณ , dev branch์— feature/main branch๋ฅผ ํ•ฉ์น˜๋ ค ํ•  ๋•Œ -> ๋ฆฌ๋ทฐ๋ฅผ ๋จผ์ € ๋ฐ›๊ณ  ํ•ฉ์ณ์•ผ ํ•จ โ€œdev branch์— ์ฝ”๋“œ๋ฅผ ํ•ฉ์น˜๊ณ  ์‹ถ์–ด! โ€œ -> ๋กœ์ปฌ ์ž‘์—… push -> โ€œ๋ณ€๊ฒฝ์‚ฌํ•ญ ํ™•์ธ ํ”Œ๋ฆฌ์ฆˆ ใ…Žใ…Žโ€ -> โ€œPull Requestโ€ -> Files changed๋ฅผ ๋ณด๊ณ , ํ˜‘์—…์ž๋“ค์˜ feedback -> Merge pull request -> dev ๋ธŒ๋žœ์น˜์— ํ•ฉ์นจ