Skip to content

suyeonii99/Git_study

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 

Repository files navigation

Git_study

Git ๊ณต๋ถ€์šฉ ๐Ÿ“

๐Ÿ“git ๊ธฐ์ดˆ (mac os ๋‚ด์žฅ terminal or vsc terminal) ! git ๋ช…๋ น์–ด -> ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰.

โœ… git ์‹œ์ž‘ํ•˜๊ธฐ.

  • git init: ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์„ git์œผ๋กœ ๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ๋ช…๋ น์–ด.

    ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— .git (์ง์ ‘ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์œผ๋ฏ€๋กœ ์ˆจ๊น€ ํŒŒ์ผ ์ฒ˜๋ฆฌ) ํด๋”๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋ฒ„์ „ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ฒƒ ์ž๋™ ์ €์žฅ.

  • git config user.name โ€œ์ด๋ฆ„โ€: git ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์„ค์ •.
  • git config user.email โ€œ์ด๋ฉ”์ผโ€: git ์‚ฌ์šฉ์ž ์ด๋ฉ”์ผ ์„ค์ •.

    ๊ฐ™์€ ์ปดํ“จํ„ฐ๋ผ๋„ ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ์ผ ๊ฒฝ์šฐ ์œ ์ € ์ด๋ฆ„, ์ด๋ฉ”์ผ ์žฌ์„ค์ • ๊ฐ€๋Šฅ.

โœ… ํŠน์ • ๋ฒ„์ „์„ ์ €์žฅ = ์ปค๋ฐ‹(commit)

  • working directory: ์‹ค์ œ๋กœ ๋‹ค๋ฃจ๋Š” ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ
  • staging area: ํŠน์ • ๋ฒ„์ „์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์€ ํŒŒ์ผ์„ ๋ชจ์•„๋‘๋Š” ์žฅ์†Œ (์ง€๊ธˆ ๋‹น์žฅ commit ์„ ๋‚จ๊ธฐ๊ณ  ์‹ถ์ง€ ์•Š์€ ํŒŒ์ผ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ.)
  • repository: ํŠน์ • ์‹œ์ ์˜ staging area ๋ชจ์Šต์„ ์ปค๋ฐ‹(commit)์œผ๋กœ ๋‚จ๊ธฐ๋ฉด ์ปค๋ฐ‹(commit)๋“ค๋กœ ์ €์žฅ๋˜๋Š” ์˜์—ญ

    commit ๊ณผ์ •: ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ working directory์—์„œ commit์„ ํ•˜๊ณ  ์‹ถ์€ ๋ชจ๋“  ํŒŒ์ผ์„ staging area ํŒŒ์ผ์— ์˜ฌ๋ฆฐ ํ›„ commit ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ์‹œ์ ์˜ staging area์— ์กด์žฌํ•˜๋˜ ํŒŒ์ผ์ด commit ๋‹จ์œ„๋กœ repository์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅ๋˜์–ด ๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.

โœ… commit ํ•˜๋Š” ์ด์œ ? ์ž‘์—… ์ด๋ ฅ์„ ๋‚จ๊ฒจ๋‘๊ณ  ์›ํ•˜๋Š” ์‹œ์ ์œผ๋กœ ์ž์œ ๋กญ๊ฒŒ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•จ.

โœ… git commitํ•˜๊ธฐ.

  • git add ํŒŒ์ผ๋ช…(ํ™•์žฅ์ž๊นŒ์ง€ ex) .js ๋“ฑ): ์ง€์ •ํ•œ ํŒŒ์ผ๋ช…์˜ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์„ staging area์— ์˜ฌ๋ฆฌ๋Š” ๋ช…๋ น์–ด.
  • git add . : ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค์–ด์ง€๊ฑฐ๋‚˜ ์ˆ˜์ •๋œ ํŒŒ์ผ์„ ๋ชจ๋‘ staging area์— ์˜ฌ๋ฆฌ๋Š” ๋ช…๋ น์–ด.
  • git commit -m โ€œ์ปค๋ฐ‹๋ฉ”์„ธ์ง€โ€: ํ˜„์žฌ staging area ์— ์˜ฌ๋ผ๊ฐ„ ํŒŒ์ผ๋“ค์„ ํ•˜๋‚˜์˜ commit์œผ๋กœ ๋งŒ๋“ค์–ด repository์— ์ €์žฅํ•˜๋Š” ๋ช…๋ น์–ด.
    • git diff {commit1_id} {commit2_id}: ์ปค๋ฐ‹ ์‚ฌ์ด์˜ ์ฐจ์ด์ ์„ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด (๋ณดํ†ต commit id ๋Š” ์•ž ๋„ค์ž๋ฆฌ๋งŒ ๋”ฐ์˜ด.)

      ์ˆ˜์ •๋œ ๋ถ€๋ถ„๋งŒ ๋ฐ˜์˜ํ•˜์—ฌ ๋ณด์—ฌ์คŒ, ์ˆ˜์ •๋˜์ง€ ์•Š์€ (staging area์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์€) ๋ถ€๋ถ„์€ ๋ฐ˜์˜ X.

    • git reset โ€”{option} {commit_id or HEAD@{์ˆซ์ž}(reflog์—์„œ ๊ธฐ๋ก๋œ ํ‘œ๊ธฐ๋ฒ•)}: Head ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” commit์„ ๋ฐ”๊พธ๋Š” ๋ช…๋ น์–ด. (๊ณตํ†ต)

      • option์ด hard ์ผ ๊ฒฝ์šฐ: working directory ๋ชจ์Šต์ด ๋ณ€๊ฒฝ๋จ(์ฝ”๋“œ ๋ณ€๊ฒฝ).

        ์ง์ „ commit ๋“ค์„ ๋‚ ๋ ค๋„ ๊ดœ์ฐฎ์„ ๋•Œ ์ฃผ์˜ํ•˜์—ฌ ์‚ฌ์šฉ

      • option์ด mixed ์ผ ๊ฒฝ์šฐ: working directory ๋ชจ์Šต์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ. / staging area ๋Š” ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๋Š” commit ์œผ๋กœ ๋ณ€๊ฒฝ๋จ.
      • option์ด soft ์ผ ๊ฒฝ์šฐ: working directory ๋ชจ์Šต์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ. / staging area๋„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์Œ.

โœ… ์ž‘์—… ์ƒํƒœ, ๊ธฐ๋ก ํ™•์ธ

  • git status: commit์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด.
  • git status: commit์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด.
  • git log / git history: commit์˜ ๊ธฐ๋ก์„ ์‹œ๊ฐ„ ์ˆœ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด.
  • git reflog: head ๊ฐ€ ํ•œ๋ฒˆ์ด๋ผ๋„ ๊ฐ€๋ฆฌ์ผฐ๋˜ commit ๊ธฐ๋ก์„ ๋ชจ๋‘ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด.

    head@{์ˆซ์ž}์˜ ์ˆซ์ž๊ฐ€ ์ž‘์„์ˆ˜๋ก ์ตœ๊ทผ commit์ž„.

โœ…์™ธ๋ถ€ ์ €์žฅ์†Œ(gitHub, gitlab ๋“ฑ) ์ €์žฅ์‹œ ์žฅ์ 

  1. ํ”„๋กœ์ ํŠธ ๋ณต๊ตฌ ๊ฐ€๋Šฅ
  2. ํ˜‘์—…๊ณผ ๋™์‹œ์— ๋ฒ„์ „ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ.

โœ… ์™ธ๋ถ€ ์ €์žฅ์†Œ์— ์ €์žฅํ•˜์—ฌ ์ž‘์—…ํ•˜๊ธฐ

  • git remote add origin {gitlab_url(gitlab(github) ํŽ˜์ด์ง€์˜ clone์—์„œ ๋งํฌ ๊ฐ€์ ธ์˜ค๊ธฐ)} : git remote๋Š” ๋‚ด ์ปดํ“จํ„ฐ์—์„œ ์™ธ๋ถ€ ์ €์žฅ์†Œ ์ž‘์—…์„ ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋กœ url์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์™ธ๋ถ€ ์„œ๋ฒ„ ํ”„๋กœ์ ํŠธ๋ฅผ ์›๊ฒฉ ์ €์žฅ์†Œ๋กœ ์ง€์ •ํ•˜๊ณ  ๊ทธ ์ด๋ฆ„์„ origin์œผ๋กœ ํ•จ. ์ฆ‰, ์›๊ฒฉ ์ €์žฅ์†Œ(gitlab_url)์„ ์ถ”ํ›„ ์ด์šฉํ•˜๊ธฐ ์‰ฝ๋„๋ก origin์œผ๋กœ ์ง€์ •.
  • git push -u origin master: ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ๋‚ด์šฉ์„ origin์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์›๊ฒฉ ์ €์žฅ์†Œ์— ์˜ฌ๋ฆฌ๋Š” ๋ช…๋ น์–ด.

    .git ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์—์„œ ๊ด€๋ฆฌ๋˜๋˜ repository ์˜์—ญ์„ ์—…๋กœ๋“œ ํ•œ ๊ฒƒ. ์ด์ „์— git push -u origin master ๋ช…๋ น์–ด๋ฅผ ์ž‘์„ฑํ•œ ๊ธฐ๋ก์ด ์žˆ์œผ๋ฉด ์ด๋ฏธ origin์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ์•Œ๊ธฐ ๋•Œ๋ฌธ์— git push ๋งŒ ์ž‘์„ฑํ•ด๋„ ok.

  • README.md ํŒŒ์ผ: ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋‹ด๊ณ ์žˆ๋Š” ํŒŒ์ผ.
  • git clone {ํ”„๋กœ์ ํŠธ_url}: url์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์›๊ฒฉ ์ €์žฅ์†Œ์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋””๋ ‰ํ† ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ด.

    ์ œ์ผ ์ฒ˜์Œ ์ƒ๋Œ€๊ฐ€ gitlab์— ์˜ฌ๋ฆฐ ํ”„๋กœ์ ํŠธ์˜ commit์„ ๋‚ด ์ปดํ“จํ„ฐ์— ๊ฐ€์ ธ์˜ค๋Š” ๋ช…๋ น์–ด. clone์„ ํ•œ ๊ฒฝ์šฐ์—๋Š” ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ๊ฐ€์ ธ ์˜จ ๊ฒƒ์ด๋ฏ€๋กœ remote ๋ช…๋ น์–ด๋กœ ์›๊ฒฉ ์ €์žฅ์†Œ ์ง€์ • ํ•„์š” ์—†์Œ.

  • git pull: gitlab ์„œ๋ฒ„ ์ƒ์˜ ์ตœ์‹  commit์ด ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ ๋‚ด ์ปดํ“จํ„ฐ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ช…๋ น์–ด.

    gitlab history ์ƒ์˜ commit ๋“ค์ด ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์ตœ์‹  commit์ด ๋ชจ๋‘ ๋ฐ˜์˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธ ํ›„ pull.

๐Ÿ“ git ์‹ฌํ™”

โœ… ์‹ค๋ฌด ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๋ฐฉ์‹ โ—๏ธ fork: ํ”„๋กœ์ ํŠธ์˜ ์™„์„ฑ๋„๋ฅผ ์œ„ํ•ด ์›๋ณธ ํ”„๋กœ์ ํŠธ ์•„๋ž˜์— ๊ฐœ์ธ ๊ฐœ๋ฐœ์ž์˜ ํ”„๋กœ์ ํŠธ copied(๋ณต์ œ)๋ณธ ์ƒ์„ฑ. > git clone ํ•˜์—ฌ ๋‚ด ์ปดํ“จํ„ฐ์— ๊นƒ๋žฉ ์„œ๋ฒ„๋ฅผ ๊ฐ€์ ธ์˜ด. > ์ž‘์—…(commit)ํ•˜์—ฌ git push ํ•˜๋ฉด ์›๋ณธ ํ”„๋กœ์ ํŠธ๊ฐ€ ์•„๋‹Œ copied ํ”„๋กœ์ ํŠธ์— ์˜ฌ๋ผ๊ฐ. > ์ดํ›„ ๊นƒ๋žฉ์—์„œ ๋‚˜์˜ ๋ณต์ œ๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ ์›๋ณธ ํ”„๋กœ์ ํŠธ๋กœ merge request๋ฅผ ๋ณด๋ƒ„ (์ƒˆ๋กœ์šด ์ž‘์—… ํ™•์ธ ํ›„ ๋ฐ˜์˜์„ ์œ„ํ•จ.) > merge request ์— ๋Œ€ํ•œ ๊ธ€์„ ๋‚จ๊ธฐ๊ณ  ์‹ถ๋‹ค๋ฉด discussion์„ ํ†ตํ•ด ๊ธ€ ๋‚จ๊ธฐ๊ธฐ. โ—๏ธ branch : ํŠน์ • ์ปค๋ฐ‹์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐโ€จ> ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐœ๋ฐœ ํ๋ฆ„์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ธŒ๋žœ์น˜ ์‚ฌ์šฉ.โ€จ> master: git ์‚ฌ์šฉ์‹œ ๊ธฐ๋ณธ ์„ค์ •๋˜๋Š” ๋ธŒ๋žœ์น˜, ์‹ค์ œ ์„œ๋น„์Šค ๋ฐฐํฌ๋ฅผ ๋ชฉ์ ์œผ๋กœ ์™„์„ฑ๋œ ์ปค๋ฐ‹๋งŒ ๋‘ .

HEAD -> master (ํ—ค๋“œ๋Š” ๋ธŒ๋žœ์น˜๋ฅผ ํ†ตํ•ด ์ปค๋ฐ‹์„ ๊ฐ€๋ฆฌํ‚ด.) origin/master(gitlab ์„œ๋ฒ„์˜ master ๋ธŒ๋žœ์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ด.)

  • git branch {๋ธŒ๋žœ์น˜๋ช…}: ๋ธŒ๋žœ์น˜ ์ƒ์„ฑ ๋ช…๋ น์–ด.
  • git checkout {๋ธŒ๋žœ์น˜๋ช…}: HEAD๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ ์ž ํ•˜๋Š” ๋ธŒ๋žœ์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด.
  • git log --all --graph: HEAD๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” branch ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋“  branch์— ๋Œ€ํ•œ commit ๊ธฐ๋ก์„ ๊ทธ๋ž˜ํ”„ ํ˜•์‹์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด.

โœ… ํ˜‘์—…์‹œ ์ค‘์š”ํ•œ ๋ช…๋ น์–ด: (merge)

  • Head ๊ฐ€ ๊ฐ€๋ฆฌํ‚ฌ ๋ธŒ๋žœ์น˜๋กœ ์ด๋™ (git checkout {๋ธŒ๋žœ์น˜๋ช…})

  • git merge {ํ•ฉ์น  ๋ธŒ๋žœ์น˜๋ช…}: ํ˜„์žฌ head๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ธŒ๋žœ์น˜์— ํ•ฉ์น  ๋ธŒ๋žœ์น˜์˜ ๋ชจ๋“  ์ž‘์—…์„ ํ•ฉํžˆ๋Š” ๋ช…๋ น์–ด.

    • ํ๋ฆ„์˜ ๋ถ„๊ธฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ: Fast-forward merge: ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋ธŒ๋žœ์น˜๊ฐ€ ๋‹น๊ฒจ์ ธ ์˜ฎ.
    • ํ๋ฆ„์˜ ๋ถ„๊ธฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ: ์ƒˆ๋กœ์šด merge commit์„ ๋งŒ๋“ฆ.
  • Git push -u origin master: master branch๋ฅผ origin์ด ์˜๋ฏธํ•˜๋Š” ๊นƒ๋žฉ ์„œ๋ฒ„์˜ ํ”„๋กœ์ ํŠธ์— ์˜ฌ๋ฆฌ๋Š” ๋ช…๋ น์–ด.โ€จ> -u ์˜ต์…˜ (= โ€”set-upstream): ๋‚ด ์ปดํ“จํ„ฐ์˜ master ๋ธŒ๋žœ์น˜๊ฐ€ ๊นƒ๋žฉ ์„œ๋ฒ„์˜ master ๋ธŒ๋žœ์น˜๋ฅผ ๋ฐ”๋ผ๋ณด๊ฒŒ ํ•˜๋Š” ๋ช…๋ น์–ด.โ€จ > ์ดํ›„์—๋Š” ์ž‘์„ฑํ•  ํ•„์š” X

  • Git push โ€”force: ์—…๋กœ๋“œ๋ฅผ ๊ฐ•์ œํ•จ.

About

Git ๊ณต๋ถ€์šฉ ๐Ÿ“

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published