TIL 25 | Git & Github

dabin *.โ—Ÿ(หŠแ—จห‹)โ—ž.*ยท2021๋…„ 8์›” 25์ผ
1

etc

๋ชฉ๋ก ๋ณด๊ธฐ
5/14
post-thumbnail
post-custom-banner

Git์ด๋ž€?

Git์˜ ๊ณต์‹ ๋ช…์นญ์€ ๋ถ„์‚ฐ ๋ฒ„์ „ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ๋กœ, ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ถ”์ ํ•˜๋Š” ์‹œ์Šคํ…œ์ด๋‹ค. ์ฝ”๋“œ ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ญ˜๊นŒ? ์ˆ˜์ •ํ•  ๋•Œ๋งˆ๋‹ค ํŒŒ์ผ์„ ์ƒˆ๋กœ ๋งŒ๋“ค๋ฉด ๊ด€๋ฆฌ๊ฐ€ ํž˜๋“ค์–ด์„œ, ์ด์ „ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€๊ธฐ ์œ„ํ•ด, ๋˜ ์—ฌ๋Ÿฌ๋ช…์˜ ๊ฐœ๋ฐœ์ž๋“ค์ด ํ˜‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ ๋ฒ„์ „ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

Github๋ž€?

Git์„ ์‚ฌ์šฉํ•ด ๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ ํ•œ ํ”„๋กœ์ ํŠธ์˜ ์ €์žฅ์†Œ์ด์ž, ๊ฐœ๋ฐœ์ž๋“ค์˜ social network๋‹ค.

Git ๊ธฐ๋ณธ ๋ช…๋ น์–ด

  • git init : git ์‹œ์ž‘(์‹œ์ž‘๋งŒ ํ•œ๋‹ค๊ณ  ๋ฒ„์ „ ๊ด€๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค)
  • git status : git ์ƒํƒœ ํ™•์ธ
  • git add : ํŒŒ์ผ ์ˆ˜์ • ์ด๋ ฅ ๊ธฐ๋ก ์ค€๋น„(commitํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ์•„๋†“์€ ๊ณต๊ฐ„/์ž„์‹œ์ €์žฅ์†Œ)
  • git add ./* : ๋ชจ๋“  ํŒŒ์ผ add
  • git commit : ํŒŒ์ผ ์ˆ˜์ • ์ด๋ ฅ ๊ธฐ๋ก(ํ˜„์žฌ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋ฅผ ์ปค๋ฐ‹์— ์ €์žฅ)
    [git commit -m "Commit message ex)Add: dabin.md ํŒŒ์ผ"]
    git commit๋งŒ ์ž…๋ ฅํ•˜๋ฉด edit์ฐฝ์ด ์—ด๋ฆฌ๊ณ  multi-line commit message ์ž…๋ ฅ ๊ฐ€๋Šฅ
  • git log : commit ๋‚ด์—ญ ๋ณด๊ธฐ(์ž‘์„ฑ์ž, hash ๊ฐ’, ๋‚ ์งœ์™€ ์‹œ๊ฐ„, ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€)
  • git checkout <commit-hash> : ํŠน์ • ์ปค๋ฐ‹ ์‹œ์ ์˜ ์ฝ”๋“œ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ, git log์—์„œ ๋ณด์ด๋Š” ์ปค๋ฐ‹์˜ ์‹ค์ œ hash ๊ฐ’ ์ž…๋ ฅ

    git์—์„œ ๊ด€๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ํŒŒ์ผ์€ .gitignore ํŒŒ์ผ์„ ํ”„๋กœ์ ํŠธ ํด๋”์— ์ƒ์„ฑํ•ด ๊ด€๋ฆฌํ•œ๋‹ค. ํŒŒ์ผ ์•ˆ์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ๋ช…๊ณผ ํด๋”๋ช…์„ ๊ฐ๊ฐ ์ƒˆ๋กœ์šด ์ค„์— ๋‚˜์—ดํ•œ๋‹ค.

Using git & github

  1. ๋กœ์ปฌ์—์„œ add/commitํ•œ ๋’ค
  2. github์—์„œ ์ƒˆ๋กœ์šด repository๋ฅผ ์ƒ์„ฑํ•˜๊ณ 
  3. ๋กœ์ปฌ repository์™€ Github repository๋ฅผ remote๋ฅผ ์ถ”๊ฐ€ํ•ด ์—ฐ๊ฒฐํ•˜๊ณ 
  4. ์ฝ”๋“œ๋ฅผ push ํ•˜๋ฉด ๋œ๋‹ค.

์ฃผ์˜

add๋Š” commit์„ ํ•˜๊ธฐ ์œ„ํ•œ ์ค€๋น„ ๋‹จ๊ณ„๋ผ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž. commit ํ•˜๊ธฐ ์ „์—๋Š” ๋ฐ˜๋“œ์‹œ add๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. add๋ฅผ ํ•œ ๋’ค ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ๋‹ค๋ฉด ๋‹ค์‹œ add๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. git status๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ํŒŒ์ผ์ด staging area์— add๋œ ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ  commitํ•œ๋‹ค.

github repository ์ƒ์„ฑ ํ›„


repository๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์นœ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” command line์„ ์•Œ๋ ค์ค€๋‹ค. ๊ธฐ์กด์— master branch๋กœ ์ž‘์—…์„ ํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— git branch -M main์œผ๋กœ branch๋ฅผ main์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  git remote add origin https://github.com/dabin219/wecode.git์œผ๋กœ ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ ๋’ค pushํ•˜๋ฉด github repository์—์„œ ์ซ˜๋ž€ ํ•˜๊ณ  ์ถ”๊ฐ€ํ•œ ํŒŒ์ผ์ด ๋ณด์ธ๋‹ค. ์ˆ˜์ •์‚ฌํ•ญ์ด ์ƒ๊ธด๋‹ค๋ฉด ํŒŒ์ผ์„ ์ €์žฅํ•˜๊ณ  add -> commit -> push ์ˆœ์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์•ผ Github repo์—๋„ ๋ฐ˜์˜์ด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ผญ ๊ธฐ์–ตํ•˜์ž!

git remote add origin https://github.com/dabin219/wecode.git : ๋กœ์ปฌ Git repository ์—๊ฒŒ ์ด๋ฆ„์ด origin ์ด๋ผ๋Š” ์–ด๋–ค URL์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์ด๋ฆ„์ด ๊ผญ origin ์ด์–ด์•ผ ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋ณดํ†ต remote ์ฃผ์†Œ๊ฐ€ ํ•œ๊ฐœ๋ผ๋ฉด origin ์ด๋ผ๊ณ  ์ง€์–ด์ฃผ๊ฒŒ ๋œ๋‹ค.

password token
์ฒ˜์Œ push๋ฅผ ํ–ˆ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.

์ด๋Š” https://github.com/settings/tokens ์—์„œ ํ† ํฐ์„ ๋งŒ๋“ค์–ด ๋น„๋ฐ€๋ฒˆํ˜ธ์— ํ† ํฐ์„ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.

github ์‹ค์ „

repository clone
repository์— ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ์ƒํƒœ๋กœ push๋ฅผ ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ์— repository์˜ ์ฝ”๋“œ๋ฅผ local๋กœ ๋จผ์ € ๊ฐ€์ ธ์˜จ ๋’ค ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค. ์ž‘์—…์ด ๋๋‚˜๋ฉด add commit push๋ฅผ ํ•ด์ค€๋‹ค. ์ž‘์—… ํ›„ repo ์ƒ์„ฑ์„ ํ•˜๋ฉด remote add๋กœ ์—ฐ๊ฒฐ์„ ํ•ด์•ผํ–ˆ์ง€๋งŒ, clone์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ทธ ๊ณผ์ •์ด ์ƒ๋žต๋œ๋‹ค.

git clone <github-repo-link>

Branching and merging
branch๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์–ด๋–ค ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค. ๊ฐ๊ฐ์˜ ๋ธŒ๋žœ์น˜๋Š” ๋‹ค๋ฅธ ๋ธŒ๋žœ์น˜์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์•„ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ๋™์‹œ์— ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • branch ๋งŒ๋“ค๊ธฐ
    git branch <branch name>
    git checkout <branch name>
    branch ๋งŒ๋“ค๊ณ  ๋‚˜์„œ 'checkout์„ ํ•ด์„œ ํ•ด๋‹น branch์—์„œ ์ž‘์—…ํ•ด์•ผํ•œ๋‹ค! ์ฃผ์˜!
  • vs code ์—์„œ ์ž‘์—… : Vs code์—์„œ ๋ช…๋ น ํŒ”๋ ˆํŠธ๋ฅผ ํ‚ค๊ณ  shell command : install 'code' command in PATH๋ฅผ ์„ค์น˜ํ•œ ๋’ค ํ„ฐ๋ฏธ๋„์—์„œ code . ํ•˜๋ฉด ํŒŒ์ผ์ด vs code์—์„œ ์—ด๋ฆฐ๋‹ค!
  • add commit push
    git add .
    git commit -m "message"
    git push origin <branch name>

    git merge <branch name> : ๋‹ค๋ฅธ ๋ธŒ๋žœ์น˜๋ฅผ ํ˜„์žฌ ๋ธŒ๋žœ์น˜์™€ ๋ณ‘ํ•ฉ, a๋ผ๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…ํ•œ ๋‚ด์šฉ์„ b๋ผ๋Š” ๋ธŒ๋žœ์น˜์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    git branch -d <branch name>: ๋ธŒ๋žœ์น˜ ์‚ญ์ œํ•˜๊ธฐ

Pull Request(PR)
ํ”„๋กœ์ ํŠธ ์˜ค๋„ˆ์—๊ฒŒ ๋‚ด๊ฐ€ ์ž‘์—…ํ•œ branch ๋‚ด์šฉ์„ master branch์— ๋ฐ˜์˜ํ•ด๋‹ฌ๋ผ๊ณ  ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค. Merge pull request -> create pull request ์ž‘์„ฑ

Merge
PR์ดํ›„ ๋ชจ๋“  ๋ฆฌ๋ทฐ ๋‚ด์šฉ์ด ๋ฐ˜์˜๋œ ๋’ค์— master branch์™€ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น PR์€ master branch๋กœ merge๋œ๋‹ค.

github๋กœ๋ถ€ํ„ฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ pull
git pull origin master
remote์˜ ์ตœ์‹ ํ™”๋œ ์ฝ”๋“œ๋ฅผ ๋‚ด ๋กœ์ปฌ repo์— ๋ฐ˜์˜ํ•œ๋‹ค. ๋ณ€๊ฒฝ์‚ฌํ•ญ๋งŒ ๋ฐ›์•„์˜จ๋‹ค. clone์€ ์ž‘์—…์„ ์‹œ์ž‘ํ•  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ ์ดํ›„์—๋Š” pull๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ›๋Š”๋‹ค.

์ฃผ์˜ : local master์—์„œ๋Š” pull๊ณผ ์ƒˆ๋กœ์šด branch๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋งŒ ํ•˜์ž. ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด github์—์„œ conflicts ๋•Œ๋ฌธ์— error ์ง€์˜ฅ์— ๊ฐ‡ํž์ˆ˜๋„,,,


์ด๋ฏธ์ง€ ์ถœ์ฒ˜

profile
๋ชจ๋ฅด๋Š”๊ฒƒํˆฌ์„ฑ์ด
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€