๐Ÿ““ GIT ๊นƒ

Geonil Jangยท2020๋…„ 11์›” 22์ผ
0
post-thumbnail
post-custom-banner

โš›๏ธ ๊นƒ์˜ ์ค‘์š”ํ•œ ์ปจ์…‰ ์ดํ•ดํ•˜๊ธฐ

  • ๊นƒ์€ ํŒŒ์ผ!!
  • working directory : ์ž‘์—…์ค‘ [untracked, tracked]
  • staging area : ์ปค๋ฐ‹์ค€๋น„ ์™„๋ฃŒ์ƒํƒœ
  • .git directory : ์ปค๋ฐ‹ํ›„ ํžˆ์Šคํ† ๋ฆฌ์— ์ €์žฅ -> checkout -> ๋Œ์•„๊ฐ€๊ธฐ ๊ฐ€๋Šฅ!
  • remote : gitgub(pull, push), bitbucket....
  • commit : hashcode [message, autore, data/time]

1. ๐ŸŒŸ ๊นƒ ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์‚ญ์ œ ํ•˜๊ธฐ

  1. git init
  2. ํด๋”์—์„œ .git ํŒŒ์ผ ์‚ญ์ œ!
//์ดˆ๊ธฐํ™” ํ•˜๊ธฐ
git init
ls -al// .git ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
//์‚ญ์ œ ํ•˜๊ธฐ
rm -rf .git

alias ์„ค์ •ํ•˜๊ธฐ
git config --global alias.st status // git st๋กœ -> git status ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”
git config --global alias.aa "add --all"
git config --global alias.cm "commit -m"

2. ๊นƒ ๋กœ์ปฌ ํŒŒ์ผ๋“ค ์ถ”๊ฐ€ํ•˜๊ธฐ add

  1. git add [., , .txt]
echo hello world1 > a.txt
echo hello world2 > b.txt
echo hello world3 > c.txt

git add *.txt // -> staging area => ready to commit
echo welcome >> a.txt //update => modified
git add a.txt

git rm --cached // from stage to untracked

.gitignore ๋งŒ๋“ค์–ด์„œ tracking์•ˆ๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

3. ๊นƒ ํ˜„์žฌ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ status

  1. git status // --long ๋””ํดํŠธ
git status -s // --short 

4. ๐Ÿ“ ๊นƒ ํŒŒ์ผ ๋น„๊ตํ•˜๊ธฐ diff

  • ์–ด๋–ค ํŒŒ์ผ์ด ์ˆ˜์ •๋˜์—ˆ๋‚˜??
  1. git diff // working directory์— ์žˆ๋Š” ์ƒํƒœ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์–ด์š” ์ด์ „ ์ปค๋ฐ‹ or staging area์™€ ๋น„๊ต
git diff
์ด์ „   ์ง€๊ธˆ
@@ -1 +1,2@@

git diff --stage // staging area์™€ ์ด์ „ ์ปค๋ฐ‹๊ณผ์˜ ๋น„๊ต

git config --global -e

5. ๐Ÿ—„ ๊นƒ ๋ฒ„์ „ ๋“ฑ๋กํ•˜๊ธฐ commit

  1. git commit
    • title & message
git add .
git commit -m "message1"

git commit -am "message all" // working directory ๋ชจ๋‘! ์ปค๋ฐ‹ํ•˜์ž

์–ด๋–ค ๊ทœ๋ชจ์˜ ์ปค๋ฐ‹? ๊นƒ์€ ํžˆ์Šคํ† ๋ฆฌ์˜ ์ฐฝ๊ณ !!
์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋Šฅ๋ณ„ ์ž‘์—…๋ณ„๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”!
๐Ÿ’ฅ ํ˜„์žฌํ˜• ๋™์‚ฌ๋กœ ๋งŒ๋“ ๋‹ค.
1. init
2. Add login
3. Add user
4. Add welcome

๋ฐ”๋กœ staging area๋กœ ๊ฐ€์ž!! ๐Ÿ›’
git rm
git mv

6. ๐Ÿ—‚ ๋ฒ„์ „๋“ค ๋ชฉ๋ก ๋ณด๊ธฐ log

  1. git log
git log --p //์ˆ˜์ • ๋‚ด์šฉํ™•์ธ ๊ฐ€๋Šฅ
git log --oneline //๊ฐ„๋‹จํ•œ ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€ 
git log --oneline --reverse //๊ฑฐ๊พธ๋กœ ๋ณด๊ธฐ
git log --pretty=oneline
git log --pretty=format:"%h %an"
[๊นƒ ๋กœ๊ทธ ์„ค์ •](https://git-scm.com/docs/git-log)

git log --oneline --graph --all

git log --oneline -3 // ์ตœ๊ทผ ์ปค๋ฐ‹ 3๊ฐœ ๊นŒ์ง€๋งŒ 
git log --author="gil"
git log --before="2020-12-25"
git log --grep="project" //title์—์„œ ์žˆ๋‹ค๋ฉด
git log -S "๋ฌธ์ž์—ด" //์ปค๋ฐ‹ ๋‚ด์šฉ์•ˆ์— ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ์ปค๋ฐ‹ 
git log -S "๋ฌธ์ž์—ด" -p // ์ฝ”๋“œ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

HEAD๋Š” ๋‚ด๊ฐ€ ์ง€๊ธˆ ๋ณด๊ณ  ์žˆ๋Š” ์œ„์น˜!!
head~1 // ์ด์ „
head~2 // ์ด์ „ ์ด์ „

git config --global alias.ll "log --graph --all --pretty=format:'%C(yellow)[%ad]%C(reset) %C(green)[%h]%C(reset) | %C(white)%s %C(bold red){{%an}}%C(reset) %C(blue)%d%C(reset)' --date=short"

6. ๐Ÿ”– ๊นƒ ํƒœ๊ทธ tag

  • ํŠน์ • ์ปค๋ฐ‹์„ ๋ถ๋งˆํฌํ•ด์„œ ๋น ๋ฅด๊ฒŒ! ๋ฆด๋ฆฌ์ฆˆ ๋ฒ„์ „v(major.minor.fix)์„ ํƒœ๊ทธ๋ณ„๋กœ ๊ด€๋ฆฌํ•˜์ž
  1. git tag "ํƒœ๊ทธ ์ด๋ฆ„" hashcode
  2. git tag "v2.1.1" hashcode -a[anotate] -m "message"
    • git tag "v2.*"
    • git tag -d ํƒœ๊ทธ์ด๋ฆ„
  3. git show ํƒœ๊ทธ์ด๋ฆ„
  4. git checkout ํƒœ๊ทธ์ด๋ฆ„

Branch ๐Ÿ’ฅ ๋ธŒ๋žœ์น˜ ๐Ÿ’ฅ

  • ํ˜‘์—…์˜ ํ•„์ˆ˜! link - point
    - master nono๐Ÿ”ด
    • feature yes๐ŸŸข
  • merge only commit ๐ŸŽ‡nice
  • delete branch

โ–ถ๏ธ ๋ธŒ๋žœ์น˜ ๊ธฐ๋ณธ์‚ฌ์šฉ

1. git branch

  1. local์˜ branch ๋ชฉ๋กํ™•์ธ ๊ฐ€๋Šฅ
  2. git branch --all ์„œ๋ฒ„์˜ ๋ชจ๋“  ๋ธŒ๋žœ์น˜ ํ™•์ธ ๊ฐ€๋Šฅ
  3. git checkout new-branch //๋™์ผํ•œ ์ปค๋ฐ‹์„ ๊ฐ€๋ฅดํ‚ค๋Š” ์ƒˆ๋กœ์šด ๋ธŒ๋žœ์น˜ ์ƒ์„ฑ
  4. git switch new-branch vs git checkout new-branch //์ƒˆ๋กœ์šด ๋ธŒ๋žœ์น˜๋กœ ์ด๋™
    • git switch -C new-branch
    • git checkout -b new-branch // ๋ธŒ๋žœ์น˜ ๋งŒ๋“ค๋ฉด์„œ ๋ฐ”๋กœ ์ด๋™
  5. git branch -d new-branch //์‚ญ์ œํ•˜๊ธฐ
    • git push origin --delete new-branch
    • git push origin :new-branch
  6. git branch --move fix fix-welcome
    • git push --set-upstream origin --fix-welcome

2. git merge

  • fast-forward merges
    - ๋งˆ์Šคํ„ฐ ๋ธŒ๋žœ์น˜์—์„œ ์ƒˆ๋กœ์šด ๋ธŒ๋žœ์น˜๊ฐ€ ์ƒ๊ธด ์ดํ›„์— ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค๋ฉด ๊ฐ€๋Šฅ!
    • ๋‹จ์ˆœ ํฌ์ธํ„ฐ๋ฅผ ๋ณ€๊ฒฝ! โœจ ์งœ๋ž€ โœจ
  1. git merge branch-name
git merge branch-name
git merge --no-ff branch-name //fast-forward ํ•˜๊ณ ์‹ถ์ง€ ์•Š์œผ๋ฉด! ๋จธ์ง€ ์ปค๋ฐ‹ ์ƒ๊น€
  • three-way mergets
    - fast-forward๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์—์„œ ์ด๋ฃจ์–ด ์ง€๋Š” ์ปค๋ฐ‹
    • ์ƒˆ๋กœ์šด ๋จธ์ง€ ์ปค๋ฐ‹์ด ์ƒ๊ธด๋‹ค.

์ถฉ๋Œ(conflict)
๋™์ผํ•œ ํŒŒ์ผ, ๋™์ผํ•œ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค๋ฉด ๋ฐœ์ƒ.

  • ์ถฉ๋Œ conflict
git merge branch //์ถฉ๋Œ ๋ฐœ์ƒ.
git mergetool //vscode๋กœ ์…‹ํŒ…ํ›„ [git config --global -e]
git add . //์ˆ˜์ • ์™„๋ฃŒํ›„
git merget --continue //๋จธ์ง€ ๊ณ„์†์ง„ํ–‰

git config --global -e

TIP: ์˜ค๋ฆฌ์ง€๋„ ํŒŒ์ผ nono
git config --global mergetool.keepBackup false

2. ๐Ÿ git rebase

  • ํ˜„ ๋ธŒ๋žœ์น˜์˜ ๋ฒ ์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด? fast-forward๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๐Ÿ”ด ์ฃผ์˜!! ๋ฆฌ๋ฒ ์ด์Šค์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๋งŒ๋“ค์–ด์„œ ๋ฒ ์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์œผ๋กœ! ๋‚˜ ํ˜ผ์ž๋งŒ ์ž‘์—…ํ•˜๋Š” ๋ธŒ๋žœ์น˜๋งŒ!!!!!!
  1. git rebase
  2. git rebase --onto master develop feature
    • develop์—์„œ ํŒŒ์ƒ๋œ feature๋ธŒ๋žœ์น˜๋ฅผ master๋กœ ๋ฆฌ๋ฒ ์ด์Šค ํ•˜๊ฒ ๋‹ค!
//master new-branch
git rebase master // new-branch๊ฐ€ master์˜ ์ตœ์‹ ์˜ ์ปค๋ฐ‹์œผ๋กœ ๋ณ€๊ฒฝ

2. ๐Ÿ’ git cherry-pick

  • ํŠน์ • ์ปค๋ฐ‹๋งŒ ๋”ฑ! ๊ฐ€์ ธ์™€
  • ์ž‘์—…๊ธฐ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์—์„œ ํŠน์ • ๊ธฐ๋Šฅ(์ปค๋ฐ‹)์„ ๋นผ์˜ฌ๋•Œ ์œ ์šฉ!
//master branch
git cherry-pick hash

Stash ๐Ÿ’ฅ ์Šคํƒœ์‹œ stack ๐Ÿ’ฅ

  • working directory์— ์žˆ๋Š” ๋‚˜์˜ ์ž‘์—…๋“ค!!.. ์•— ์•„์ง ์ปค๋ฐ‹ ๋‹จ๊ณ„๋Š”์•„๋‹ˆ์•ผโ—๏ธโ“
  • ์ž ์‹œ ์ €์žฅ!! ์–โš ๏ธ
  1. git stash
  2. git stash list //stash stack ๋ชฉ๋ก ๋ณด๊ธฐ
  3. git stash apply(๊ทธ๋ƒฅ ์ ์šฉ) id or pop(๋นผ๋ฉด์„œ ์ ์šฉ)
  4. git stash drop [id]//๊ทธ๋ƒฅ ์ œ๊ฑฐ
  5. git stash clear //์ „์ฒด ์‚ญ์ œ
git stash [push] -m "message"
git stash list

//์ง€๊ธˆ ์Šคํ…Œ์ด์ง•์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  stash stack์— push as well
git stash push -m "message" --keep-index 

//If you want to push untracking code into stash stack as well, Use -u option.
**ํ…์ŠคํŠธ**
git stash -u

Reset ๐Ÿ› ํ—›... fix ํ•˜์ž

1. git restore >2.23.0

  • ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ œ๊ฑฐํ•˜๊ธฐ
  1. git restore .
    • ์ „์ฒด์ ์ธ ๋‚ด์šฉ ์ดˆ๊ธฐํ™”
  • staging area์— ์žˆ๋Š” ๋‚ด์šฉ์„ working directory ๊ฐ€์ ธ์˜ค๊ธฐ
  1. git resotre --staged .
// ํ•ด๋‹น ๋‚ด์šฉ๊นŒ์ง€ ์‚ญ์ œ!
git restore --source=hash์ฝ”๋“œ
or
git restore --source=HEAD~1

2. ์ปค๋ฐ‹ ์ˆ˜์ •ํ•˜๊ธฐ amend

  1. git commit --amend // ์ž˜ ๋ชป๋œ ์ปค๋ฐ‹ ์ˆ˜์ •ํ•˜๊ธฐ.
    • โ€ผ๏ธ์„œ๋ฒ„์— ์—…๋กœ๋“œ ํ•˜์ง€ ์•Š์•˜์„๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ!!!
//message
git commit --amend -m "new message"

//๋‚ด์šฉ ์ถ”๊ฐ€ํ•˜๊ธฐ
//๋ฐฉ๊ธˆ ๋ฐ˜๋“  ์ปค๋ฐ‹์—์„œ ๋‹ค์‹œ ์ˆ˜์ •ํ•˜๊ธฐ...
~~~~
git add .
git commit --amend

2. ์ปค๋ฐ‹์„ ์ดˆ๊ธฐํ™” ์‹œํ‚ค์ž reset

  1. git reset ์›ํ•˜๋Š” ์ปค๋ฐ‹ ์œ„์น˜!
    • git reset HEAD~2
    • git reset hash
git reset // --mixed  (๊ธฐ๋ณธ)์ปค๋ฐ‹์€ ์‚ญ์ œ ํ•˜์ง€๋งŒ ๋‚ด์šฉ์€ working directory ์˜ฎ๊ธด๋‹ค.
git reset --soft HEAD~1 // --soft : staging area๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
git reset --hard HEAD// ๊ทธ๋ƒฅ ์ปค๋ฐ‹ + ๋‚ด์ž‘์—… ๋‚ด์šฉ ๋‹ค!!! ์‚ญ์ œํ•ด์ค˜ ๐Ÿ—‘โ€ผ๏ธ
  1. ๐Ÿ’ git reflog => reset ๋˜์‚ด๋ฆฌ๊ธฐ!
    • ์ง€๊ธˆ๊นŒ์ง€ ์‹คํ–‰๋œ ๋‚ด์šฉ์„ ๋ชจ๋‘ ๋‹ด๊ณ  ์žˆ์–ด์š”!
    • git reset --hard hash ๐Ÿคฉ ๋‹ค์‹œ ๋ถ€ํ™œ!

๐Ÿ”ฑ vscode local hisotry!! extension good!!
โšœ๏ธ webstorm์€ ๊ธฐ๋ณธ์œผ๋กœ ์žˆ์–ด์š”!.....

git clean -fd
ํŠธ๋ž˜ํ‚น ๋˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ํŒŒ์ผ์€ restore๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— clean์„ ์‚ฌ์šฉ.

3. ์ปค๋ฐ‹์„ ์ดˆ๊ธฐํ™” ์‹œํ‚ค์ž reset

  1. git reset ์›ํ•˜๋Š” ์ปค๋ฐ‹ ์œ„์น˜!
    • git reset HEAD~2
    • git reset hash
git reset // --mixed  (๊ธฐ๋ณธ)์ปค๋ฐ‹์€ ์‚ญ์ œ ํ•˜์ง€๋งŒ ๋‚ด์šฉ์€ working directory ์˜ฎ๊ธด๋‹ค.
git reset --soft HEAD~1 // --soft : staging area๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
git reset --hard HEAD// ๊ทธ๋ƒฅ ์ปค๋ฐ‹ + ๋‚ด์ž‘์—… ๋‚ด์šฉ ๋‹ค!!! ์‚ญ์ œํ•ด์ค˜ ๐Ÿ—‘โ€ผ๏ธ

git clean -fd
ํŠธ๋ž˜ํ‚น ๋˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ํŒŒ์ผ์€ restore๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— clean์„ ์‚ฌ์šฉ.

4. ์ปค๋ฐ‹์„ ์ดˆ๊ธฐํ™” revert

  • ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋‹ค์‹œ ์—†์•  ์ฃผ๋Š”, ์‚ญ์ œํ•ด์ฃผ๋Š”, ์ฅ์†Œํ•ด์ฃผ๋Š” ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๋งŒ๋“ ๋‹ค.
    - ๋”ฑ! ๊ทธ ์ปค๋ฐ‹์—์„œ ์ˆ˜์ •ํ•œ ๋‚ด์šฉ์„ ์ œ๊ฑฐํ•œ ๋‹ค์Œ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  1. git revert hash
  2. git revert --no-commit hash
    • ๋ฐ”๋กœ ์ปค๋ฐ‹์„ ํ•˜์ง€ ์•Š๊ณ  ์ทจ์†Œ ๋˜๋Š” ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ staging area์— ์˜ฌ๋ ค์ค€๋‹ค.

[[revert]]๋Š” ์ด๋ฏธ ๋งˆ์Šคํ„ฐ ๋ธŒ๋žœ์น˜์— ์ปค๋ฐ‹๋œ ์•„์ด๋“ค์ด๋ผ๋ฉด, reset๋˜๋Š” rebase๋ณด๋‹ค revert๋ฅผ ํ•˜๋Š”๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค. [[revert]]๋Š” ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๋งŒ๋“ค์–ด์„œ ์ด๋ฏธ ์ถ”๊ฐ€๋œ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ
์ฆ‰โ—๏ธ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ˆ˜์ • ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์–ธ์ œ๋“ ์ง€ ์ž์œ ๋กญ๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

5. ์ด์ „ ์ปค๋ฐ‹ ์ˆ˜์ •ํ•˜๊ธฐ interactive rebasing

  • ๐Ÿ”ด rebasing์„ ํ•˜๋Š” ์ˆœ๊ฐ„ ํ˜„์žฌ rebasing์ดํ›„์˜ ์ปค๋ฐ‹์€ ์ƒˆ๋กœ์šด ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋œ๋‹ค!!
    - ์ „๋ถ€ ์ƒˆ๋กœ์šด ์•„์ดํ…œ์ด ๋œ๋‹ค!!!!
    - ๋‚ด๊ฐ€ ๊ฐ€๊ณ  ์‹ถ์€ ์ด์ „๋ณด๋‹ค ํ•˜๋‚˜๋” โ—€๏ธ ์•ž!!! ์œผ๋กœ rebasingํ•ด์•ผ ํ•œ๋‹ค.
  1. git rebase -i hash
//hash๊ฐ’ ์ดํ›„๋‚˜์˜จ ์ปค๋ฐ‹๋“ค์„ ํ•จ๊ป˜ rebaseํ•  ๊ฑฐ๋‹ค!
git rebase -i hash

pick: use commit
reword: use commit, but edit the commit message
edit: use commit, but stop for ameding // ์ปท๋ฐ‹์„ ์“ฐ์ง€๋งŒ ์•ˆ์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ”๊พธ๊ฒ ๋‹ค.
squash: use commit, but meld into previous commit //์ปค๋ฐ‹์„ ํ•˜๋‚˜๋กœ ๋ฌถ๋Š”๋‹ค.
fixup: squash๋ž‘ ๊ฐ™์ง€๋งŒ ๋ฉ”์„ธ์ง€ ์•ˆ๋‚จ๊น€.

๐Ÿ’ฅdrop์„ ํ•ด์„œ ์ถฉ๋Œ์ด๋‚˜๋ฉด
git add .
git rebase --continue๋ฅผ ํ•ด์ค˜์šฉ
profile
takeaways
post-custom-banner

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