Basic

HELP

$ git ๋ช…๋ น์–ด --help

๋ช…๋ น์–ด์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ž์„ธํžˆ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ๋ณธ์„ค์ •

$ git config

git์˜ ๊ธฐ๋ณธ์„ค์ •์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 git config --global user.name "์ด๋ฆ„"
 git config --global user.email "์ด๋ฉ”์ผ์ฃผ์†Œ"

๊ฐ๊ฐ์˜ ๋ฒ„์ „์€ ๋ˆ„๊ฐ€ ๋งŒ๋“ ๊ฒƒ์ธ์ง€ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ ์œ„์™€ ๊ฐ™์ด username๊ณผ email์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. (railsinstaller์„ค์น˜ ํ›„ ์ฒ˜์Œ ์ž…๋ ฅํ•œ ์ •๋ณด!)

์ƒˆ๋กœ์šด ์ €์žฅ์†Œ ๋งŒ๋“ค๊ธฐ

๋ชจ๋“ ๊ฒƒ์€ init์œผ๋กœ ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ๋‹ค.

$ git init
Initialized empty Git repository in /Users/dh0023/gittest/.git/

$ ls -al
total 0
drwxr-xr-x   3 dh0023  staff   102  3 23 17:08 .
drwxr-xr-x+ 34 dh0023  staff  1156  3 23 17:08 ..
drwxr-xr-x  10 dh0023  staff   340  3 23 17:08 .git
  • git init์„ ํ•˜๋ฉด ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ๋ฒ„์ „๊ด€๋ฆฌ์˜ ์ €์žฅ์†Œ๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  • ls -al์„ ํ•˜๋ฉด .git directory(ํด๋”)๊ฐ€ ์ƒ๊ธด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค..git ํด๋” ์•ˆ์—์„œ ๋ฒ„์ „๊ด€๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

์ƒํƒœํ™•์ธํ•˜๊ธฐ

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test1.html
--------------------------

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   test1.html
  • untracked files:์€ ๋ฒ„์ „๊ด€๋ฆฌ๊ฐ€ ๋˜์ง€์•Š๊ณ  ์žˆ๋Š” ํŒŒ์ผ๋กœ์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ ์ฆ‰ ๋ณ€๊ฒฝ๋œ ํŒŒ์ผ์ด๋‹ค. ๋ฒ„์ „๊ด€๋ฆฌ๊ฐ€ ๋˜์ง€์•Š๊ณ  ์žˆ๋Š” ํŒŒ์ผ,ํด๋”๋Š” ๋ถ‰์€์ƒ‰์œผ๋กœ ํ‘œ์‹œ ๋œ๋‹ค.

  • new file:์€ ๋ฒ„์ „๊ด€๋ฆฌ์— ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ํŒŒ์ผ์ด๋ฉฐ, ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค.(git add๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€)

์ถ”๊ฐ€

$ git add ํŒŒ์ผ(ํด๋”)๋ช…
$ git add test1.html

$ git add .

๋ฒ„์ „๊ด€๋ฆฌ ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

  • git add .์€ ๋ณ€๊ฒฝ๋œ ๋ชจ๋“  ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ์˜๋ฏธ

  • add๋ช…๋ น์–ด ํ›„ git status๋ฅผ ํ•ด๋ณด๋ฉด changed to be Committed: ํŒŒ์ผ์ด ๋ฒ„์ „๊ด€๋ฆฌ ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ™•์ •

$ git commit -m "first commit"
[master (root-commit) dc423a2] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test1.html

์ด๋•Œ๊นŒ์ง€ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ""์•ˆ์—๋Š” ๋ฒ„์ „์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ฐ„๋‹จํžˆ ์ ์–ด์ค€๋‹ค.

$ git commit --amend
[master c6ce442] first commit
 Date: Thu Mar 23 17:28:55 2017 +0900
 1 file changed, 1 insertion(+)
 create mode 100644 test1.html

์ถ”๊ฐ€์ ์ธ ์„ค๋ช…์„ ์ž์„ธํ•˜๊ฒŒ ์ ์„ ์ˆ˜ ์žˆ๋‹ค.

์„œ๋ฒ„์— ์˜ฌ๋ฆฌ๊ธฐ

ํ˜„์žฌ์˜ ๋ณ€๊ฒฝ ๋‚ด์šฉ์€ ์•„์ง ๋กœ์ปฌ ์ €์žฅ์†Œ์˜ HEAD ์•ˆ์— ๋จธ๋ฌผ๊ณ  ์žˆ๋‹ค. push๋ช…๋ น์–ด ์ดํ›„์— ๋ณ€๊ฒฝ๋‚ด์šฉ์ด ์„œ๋ฒ„์— ์ ์šฉ๋œ๋‹ค.

$ git push [๋ฆฌ๋ชจํŠธ ์ €์žฅ์†Œ ์ด๋ฆ„] [๋ธŒ๋žœ์น˜ ์ด๋ฆ„]

master ๋ธŒ๋žœ์น˜๋ฅผ origin ์„œ๋ฒ„์— ์˜ฌ๋ฆฌ๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด๋œ๋‹ค.

$ git push origin master

์ €์žฅ์†Œ ๋ฐ›์•„์˜ค๊ธฐ

$ git clone /๋กœ์ปฌ/์ €์žฅ์†Œ/๊ฒฝ๋กœ

ํŠน์ • ๋ธŒ๋žœ์น˜๋กœ ์ €์žฅ์†Œ๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ต์…˜์„ ์ฃผ๋ฉด ๋œ๋‹ค.

$ git clone -b {branch_name} --single-branch {repository_url}

์ทจ์†Œ

$ git reset [๋Œ์•„๊ฐˆ ๋ฒ„์ „]
$ git reset HEAD~1
  • Reset์€ ์„ ํƒํ•œ ๋ฒ„์ „์˜ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ, ๋ฒ„์ „์„ ์ง€์›Œ๋ฒ„๋ฆผ

$ git revert [๋Œ์•„๊ฐˆ ๋ฒ„์ „]
$ git revert HEAD ~1
  • Revert๋Š” ์„ ํƒํ•œ ๋ฒ„์ „์„ ์ทจ์†Œํ•ด์„œ ๊ทธ ์ด์ „ ์ƒํƒœ๋กœ ๋Œ๋ฆฌ๋Š”๊ฒƒ

revert ์™€ reverse ๋ฅผ ํ• ๋•Œ๋Š” ํ•ญ์ƒ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

Branch ์ƒ์„ฑํ•˜๊ธฐ

  • branch๋Š” ์•ˆ์ „ํ•˜๊ฒŒ ๊ฒฉ๋ฆฌ๋œ ์ƒํƒœ์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ.

$ git branch
*master

์ €์žฅ์†Œ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๋ฉด master branch(default)๊ฐ€ ๊ธฐ๋ณธ์ด๋‹ค.

$ git branch <๋ธŒ๋žœ์น˜๋ช…>
$ git branch exp
$ git branch
exp
*master

์‹คํ—˜์ ์ธ ์ž‘์—…์„ ํ•ด์•ผํ•˜๋Š”๊ฒฝ์šฐ๋‚˜ ํ˜‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉ. exp branch๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. *๋ถ™์€ ๊ฒƒ์ด ํ˜„์žฌ branch

  • ์ด์ „ commit์œผ๋กœ branch๋ฅผ ์ƒ์„ฑ

$ git branch <๋ธŒ๋žœ์น˜๋ช…> [ํŠน์ • ๋ฒ„์ „]
$ git branch example 9da3066404c68a4e7f77a7ddb69beb4d2475e2dd

ํ•ด๋‹น commit์—์„œ ๋ธŒ๋žœ์น˜๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

git log์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ

ํ˜„์žฌ Branch๋ณ€๊ฒฝ

$ git checkout exp
*exp
master

ํ˜„์žฌ branch๊ฐ€ *exp๋กœ ๋ฐ”๋€๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

$ git checkout -t origin/exp

remote์— ์žˆ๋Š” branch๋ฅผ ๊ฐ€์ ธ์™€ ๋กœ์ปฌ์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ํ•ด๋‹น branch๋กœ checkoutํ•˜๋Š” ์˜ต์…˜์ด๋‹ค.

$ git branch
* exp
master

Branch ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ๋ณ€๊ฒฝํ•˜๊ธฐ

$ git checkout -b <๋ธŒ๋žœ์น˜๋ช…>
$ git checkout -b example1
$ git branch
*example1
master

๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ(fetch)

fetch๋Š” ๋‹จ์ˆœํžˆ ์›๊ฒฉ ์ €์žฅ์†Œ์˜ ๋‚ด์šฉ์„ ํ™•์ธ๋งŒ ํ•˜๊ณ  ๋กœ์ปฌ ๋ฐ์ดํ„ฐ์™€ ๋ณ‘ํ•ฉ์€ ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

$ git fetch [remote-name]

์ฆ‰, local์—๋Š” ์—†์ง€๋งŒ remote ์ €์žฅ์†Œ์— ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

Branch ๋ณ‘ํ•ฉ

$ git merge <branch๋ช…>

branch๋ฅผ ์ด์šฉํ•ด ๋ณ‘ํ•ฉ์„ ํ•˜๋ฉด git์ด ์ž๋™์œผ๋กœ commit์„ ํ•ด์ค€๋‹ค.

$ git branch
example1
* master

$ git merge example1

Merge --squash

๋ณ‘ํ•ฉํ•  branch์˜ ๋ชจ๋“  commit์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ณ  ์‹ถ์„๋•Œ --squash ์˜ต์…˜์„ ์ง€์ •ํ•˜๋ฉด๋œ๋‹ค.

$ git merge --squash <branch๋ช…>
$ git merge --squash example1

๊ฐฑ์‹ (pull)

$ git pull

์›๊ฒฉ ์ €์žฅ์†Œ์˜ ๋ณ€๊ฒฝ ๋‚ด์šฉ์ด ๋กœ์ปฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ์— ๋ฐ›์•„์ง€๊ณ (fetch), ๋ณ‘ํ•ฉ(merge)๋œ๋‹ค.

์ถฉ๋Œ(conflict)

๋งŒ์•ฝ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ branch๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฒƒ์„ ์ˆ˜์ •ํ•  ๊ฒฝ์šฐ์— ๊นƒ์€ ์ถฉ๋Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ‘œ์‹œํ•ด์ค€๋‹ค.

ํ˜‘์—…

์–ด๋– ํ•œ ์ž‘์—…์„ ํ•˜๊ธฐ์ „์— pull์„ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹๋‹ค! pull->working->commit->pull->push

stash

์•„์ง commitํ•˜์ง€ ์•Š์€ ๋ฒ„์ „์„ ์ž„์‹œ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ! stash๋ฅผ ํ•˜๋ฉด ์ž„์‹œ๋กœ ์ €์žฅ๋œ ํ›„ ๋งˆ์ง€๋ง‰ ๋ฒ„์ „์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์‚ญ์ œ๋œ๋‹ค.

tag(github์—์„  releases)

์„ค๋ช…ํ•ด์ฃผ๋Š” ๊ฒƒ! ๊ณผ๊ฑฐ์˜ ํŠน์ •ํ•œ ๋ฒ„์ „์— ๋Œ€ํ•ด์„œ๋„ ํƒœ๊ทธ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

ignore

.gitignoreํŒŒ์ผ์— ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋ฉด git์ด ์—†๋Š”๊ฑธ๋กœ ๊ฐ„์ฃผํ•  ๋Œ€์ƒ์ด ๋œ๋‹ค.(github์— ์˜ฌ๋ฆด๋•Œ๋„ ์•ˆ์˜ฌ๋ผ๊ฐ!) ignore์— ํฌํ•จ๋˜์–ด์•ผํ•  ๋ชฉ๋ก

์ด ๋•Œ ์ค‘์š”ํ•œ id, ๋น„๋ฐ€๋ฒˆํ˜ธ, key๊ฐ’์ด ์„ค์ •๋œ ํŒŒ์ผ์€ ๋”ฐ๋กœ ์ €์žฅํ•œ ํ›„ .gitignore์— ์ถ”๊ฐ€ํ•ด ์›๊ฒฉ์ €์žฅ์†Œ์— ์˜ฌ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.

์ฐธ๊ณ 

Last updated