6 ๋ถ„ ์†Œ์š”


undoing

  • ์–ด๋–ค ํ–‰์œ„๋ฅผ ์ทจ์†Œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด

  • ์‚ฌ์ „ ์ค€๋น„

# git ์ดˆ๊ธฐํ™” & a.txt README.md ํŒŒ์ผ ์ƒ์„ฑ

image



1. ํŒŒ์ผ ์ƒํƒœ๋ฅผ Unstage๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

Staging Area(INDEX)์™€ Working Directory(WA)๋ฅผ ๋„˜๋‚˜๋“œ๋Š” ๋ฐฉ๋ฒ•



์ฒซ ๋ฒˆ์งธ - rm --cached

  • ๋”ฐ๋กœ ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์‹ค์ˆ˜๋กœ ๋ชจ๋‘ $ git add . ๋ฅผ ํ•œ ์ƒํ™ฉ (์ฒ˜์Œ์œผ๋กœ add๋ฅผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ๊ฐ€์ •)
$ git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track) 

$ git add .

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage) # rm --cached๋ฅผ ์จ ... ๋ฌด๋Œ€์—์„œ ๋‚ด๋ฆฌ๊ณ  ์‹ถ์œผ๋ฉด..!
        new file:   README.md
        new file:   a.txt    



a.txt๋ฅผ addํ•˜๊ธฐ ์ „์œผ๋กœ ๋Œ๋ฆฐ๋‹ค.

$ git rm --cached a.txt
rm 'a.txt'

$ git status
On branch master

No commits yet

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        a.txt # ๋ฌด๋Œ€์—์„œ ๋‚ด๋ ค์˜ด!!

image-20210706143516204



commit ๋‚จ๊ฒจ๋ณด์ž!

$ git add .
$ git commit -m'first commit'
[master (root-commit) a46391e] first commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
 create mode 100644 a.txt



๋‘ ๋ฒˆ์งธ - restore

  • ๋‘ ๊ฐœ์˜ ํŒŒ์ผ์„ ๋ชจ๋‘ ์ˆ˜์ •ํ•˜๊ณ  ๋”ฐ๋กœ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, ์‹ค์ˆ˜๋กœ $ git add . ๋ผ๊ณ  ํ•ด๋ฒ„๋ฆฐ ์ƒํ™ฉ

  • a.txt, README.md ํŒŒ์ผ์— ๊ฐ๊ฐ ๋ฉ”์‹œ์ง€๋ฅผ ๋‚จ๊ฒจ๋ณด์ž

image

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory) # WD์— ์žˆ์Œ + commit์ด ํ•œ๋ฒˆ์ด๋ผ๊ณ  ๋ฐœ์ƒ ํ–ˆ๋˜ ์นœ๊ตฌ? ๋„ต! -> modified!       
        modified:   README.md
        modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git add .

# status 
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md
        modified:   a.txt
$ git restore --staged a.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)       
        modified:   a.txt

image-20210706143940012



์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ๋ญ๊ฐ€ ๋‹ค๋ฅผ๊นŒ?

bash๋กœ ๋ณผ ๋•Œ

$ touch b.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md  # SA + commit์ด ํ•œ๋ฒˆ์ด๋ผ๋„์žˆ์—ˆ๋˜ -> restore --staged

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in 
working directory)
        modified:   a.txt # WD + commit์ด ํ•œ๋ฒˆ์ด๋ผ๋„ ์žˆ์—ˆ๋˜ 

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        b.txt # WD + commit์ด ํ•œ๋ฒˆ๋„ ์•ˆ๋œ ์นœ๊ตฌ



๋‹ค์‹œ ์ •๋ฆฌํ•˜๋ฉด

  1. git rm --cached <file>
    • ๊ธฐ์กด์— ์ปค๋ฐ‹์ด ์—†๋Š” ๊ฒฝ์šฐ SA -> WD๋กœ ๋‚ด๋ฆด ๋•Œ ์‚ฌ์šฉ
  2. git restore --staged <file>
    • ๊ธฐ์กด์— ์ปค๋ฐ‹์ด ์žˆ๋Š” ๊ฒฝ์šฐ SA -> WD๋กœ ๋‚ด๋ฆด ๋•Œ ์‚ฌ์šฉ



2. Modified๋œ ํŒŒ์ผ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•

  • add๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€(WD์— ์žˆ๋Š”) + ์ˆ˜์ •๋œ(modified) a.txt๋ฅผ ๋‹ค์‹œ ๋Œ๋ ค๋ณด์ž
  • ์ผ๋‹จ commit์€ ์ ์–ด๋„ ํ•œ๋ฒˆ ์žˆ์—ˆ๊ณ  ์ˆ˜์ •๋˜์—ˆ์Œ
  • ํ•˜์ง€๋งŒ SA์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์€ ์ƒํƒœ

์ฃผ์˜!!!!

  • ์›๋ž˜ ํŒŒ์ผ๋กœ ๋Œ์•„๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— โ€˜์ ˆ๋Œ€๋กœโ€™ ๋‹ค์‹œ ๋˜๋Œ๋ฆด ์ˆ˜ ์—†์Œ
  • ์ˆ˜์ •ํ•œ ๋‚ด์šฉ์ด ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์„ ๋•Œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•จ(์ •๋ง ๋งˆ์Œ์— ์•ˆ๋“ค๋•Œ๋งŒ ์จ์•ผํ•จ)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in 
working directory)
        modified:   a.txt # ์ด๋…€์„์„ ์ˆ˜์ • ์ „ ์ƒํƒœ๋กœ ๋Œ๋ฆด ์˜ˆ์ •

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        b.txt
# ๊ธฐ์กด์— a.txt์— ์ž‘์„ฑ๋œ ๋‚ด์šฉ์ด ๋ชจ๋‘ ์‚ฌ๋ผ์ง
$ git restore a.txt

# status -> ์• ์ดˆ์— commit์œผ๋กœ ๋‚จ๊ธฐ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ๋ฆด ์ˆ˜ ์—†์Œ
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

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



3. ์™„๋ฃŒ๋œ ์ปค๋ฐ‹ ์ˆ˜์ •

$ git commit --amend
  1. ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ์ž˜๋ชป ์ ์€ ๊ฒฝ์šฐ ์ˆ˜์ •!!
    • ๊ฐ€์žฅ ์ตœ์‹ ์˜ commit๋งŒ ์ˆ˜์ • ๊ฐ€๋Šฅํ•จ..!
    • ์—ญ์‚ฌ๋ฅผ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ๋จ..!
  2. ๋„ˆ๋ฌด ์ผ์ฐ ์ปค๋ฐ‹์„ ํ•œ ๊ฒฝ์šฐ(๋ฌด์–ธ๊ฐ€ ๋นผ๋จน๊ณ  commit์„ ์ง„ํ–‰ํ•œ ๊ฒฝ์šฐ)

[์ฃผ์˜ ์‚ฌํ•ญ] :์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ”๊พธ๋ฉด ์ปค๋ฐ‹ ํ•ด์‹œ๊ฐ’์ด ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›๊ฒฉ ์ €์žฅ์†Œ์— ์—…๋กœ๋“œํ•œ ๊ฒฝ์šฐ ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋Š” ์ ˆ๋Œ€๋กœ ์ˆ˜์ •ํ•˜์ง€ ๋ง ๊ฒƒ!
amend ๋Š” ๋กœ์ปฌ์—์„œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.



3.1 ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€ ์ˆ˜์ •

  • ์ˆ˜์ •์„ ์ง„ํ–‰ํ•˜๊ณ  ์ฐฝ์„ ๋‹ซ์•„์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
$ git add .
$ git commit -m'amend text file' # ์˜คํƒ€๊ฐ€ ๋‚ฌ๋‹ค.
[master 2cc67ed] amend text file 
 2 files changed, 3 insertions(+)
 create mode 100644 b.txt   
 
# ์ˆ˜์ • ์ง„ํ–‰
# amend text file -> ์ด ๋ถ€๋ถ„ ์ˆ˜์ •

$ git commit --amend -m 'text file'
[master d984105] text file
 Date: Sun Nov 21 22:11:31 2021 +0900
 2 files changed, 3 insertions(+)    
 create mode 100644 b.txt

image



3-2. ์–ด๋– ํ•œ ํŒŒ์ผ์„ ๋นผ๋จน๊ณ  commit์„ ํ•œ ๊ฒฝ์šฐ

๋‹ค์‹œ ์ปค๋ฐ‹์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ˆ˜์ • ์ž‘์—…์„ ํ•˜๊ณ  SA์— ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ --amend ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปค๋ฐ‹ ์žฌ์ž‘์„ฑ

$ touch foo.txt bar.txt
$ git add foo.txt
# ์ƒํƒœ ํ™•์ธ
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   foo.txt # SA + new file -> commit์ด ํ•œ๋ฒˆ๋„ ์—†์—ˆ๋˜ ์ƒํƒœ

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        bar.txt # WD
# ์‹ค์ˆ˜๋กœ bar.txt๋ฅผ ๋นผ๋จน๊ณ  ์ปค๋ฐ‹์„ ์ง„ํ–‰ํ•จ
$ git commit -m'foo & bar'
[master 48f0541] foo & bar
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo.txt

# log
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        bar.txt # bar๋Š” WD์— ๋‚จ์•„์žˆ์Œ!

nothing added to commit but untracked files present (use "git add" to track)



ํ•ด๊ฒฐํ•˜๊ธฐ

$ git add bar.txt 
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   bar.txt
$ git add bar.txt

$ git commit --amend -m'foo & bar'
[master d6175dd] foo & bar
 Date: Sun Nov 21 22:22:41 2021 +0900
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar.txt
 create mode 100644 foo.txt
# ์ƒํƒœ ํ™•์ธ
$ git status
On branch master
nothing to commit, working tree clean

# log ํ™•์ธ
$ git log --oneline
d6175dd (HEAD -> master) foo & bar # ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์ด ์ƒ๊ธด๊ฒŒ ์•„๋‹ˆ๋ผ ๊ธฐ์กด ์ปค๋ฐ‹์— bar.txt์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ๋งŒ ์ถ”๊ฐ€๋จ
d984105 text file
a46391e first commit

reset vs revert

reset

  • https://git-scm.com/docs/git-reset
  • โ€œ์‹œ๊ณ„๋ฅผ ๋งˆ์น˜ ๊ณผ๊ฑฐ๋กœ ๋Œ๋ฆฌ๋Š” ๋“ฏํ•œ ํ–‰์œ„โ€
  • ํŠน์ • ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๋ฉฐ ๋˜๋Œ์•„๊ฐ„ ํŠน์ • ์ปค๋ฐ‹ ์ดํ›„์˜ ์ปค๋ฐ‹๋“ค์€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง€๋ฉฐ, ํŒŒ์ผ ์ƒํƒœ๋Š” ์˜ต์…˜์„ ํ†ตํ•ด ๊ฒฐ์ •



3๊ฐ€์ง€ ์˜ต์…˜

1. --soft

  • resetํ•˜๊ธฐ ์ „๊นŒ์ง€ ํ–ˆ๋˜ SA, WD ์ž‘์—…์€ ๋‚จ๊ฒจ๋‘ 
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค์„ staging area๋กœ ๋Œ๋ ค๋†“์Œ (commit ํ•˜๊ธฐ ์ „ ์ƒํƒœ)
  • ์ฆ‰, ๋ฐ”๋กœ ๋‹ค์‹œ ์ปค๋ฐ‹ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋จ

2. --mixed

  • (๊ธฐ๋ณธ) SA reset, WD์ž‘์—…์€ ๋‚จ๊ฒจ๋‘ 
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค์„ working directory๋กœ ๋Œ๋ ค๋†“์Œ (add ํ•˜๊ธฐ ์ „ ์ƒํƒœ)
  • ์ฆ‰, unstaged ๋œ ์ƒํƒœ๋กœ ๋‚จ์•„์žˆ์Œ
  • ๊ธฐ๋ณธ๊ฐ’

3. --hard

  • resetํ•˜๊ธฐ ์ „ SA, WD ๋ชจ๋“  ์ž‘์—… ๋ฆฌ์…‹
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค(tracked ํŒŒ์ผ๋“ค)์€ ๋ชจ๋‘ working directory์—์„œ ์‚ญ์ œ
  • ๋‹จ, Untracked ํŒŒ์ผ์€ Untracked๋กœ ๋‚จ์Œ

08-2

# undoing ํด๋”์—์„œ ํ–ˆ๋˜ ๋‚ด์šฉ ์ด์–ด์„œ ์ง„ํ–‰

# --hard ์˜ˆ์‹œ
$ git log --oneline
d6175dd (HEAD -> master) foo & bar
d984105 text file
a46391e first commit
$ git reset --hard d984105
HEAD is now at d984105 text file
$ git log --oneline
d984105 (HEAD -> master) text file
a46391e first commit

$ git status
On branch master
nothing to commit, working tree clean



reset ํŠน์ง•๋“ค

  • reset์€ ๊ณผ๊ฑฐ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋˜๋ฉด ๋Œ์•„๊ฐ„ ์ปค๋ฐ‹ ์ดํ›„์˜ ์ปค๋ฐ‹์€ ๋ชจ๋‘ ํžˆ์Šคํ† ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์‚ฌ์šฉ ์‹œ ์ถฉ๋Œ์ด ๋ฐœ์ƒ
  • ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์ด์ „ ์ปค๋ฐ‹์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” git revert ์‚ฌ์šฉ



revert

  • https://git-scm.com/docs/git-revert

  • โ€œํŠน์ • ์‚ฌ๊ฑด์„ ์—†์—ˆ๋˜ ์ผ๋กœ ๋งŒ๋“œ๋Š” ํ–‰์œ„โ€
  • ์ด์ „ ์ปค๋ฐ‹ ๋‚ด์—ญ์„ ๊ทธ๋Œ€๋กœ ๋‚จ๊ฒจ๋‘” ์ฑ„ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹(==์—†์—ˆ๋˜ ์ผ์ž…๋‹ˆ๋‹ค!๋ฅผ ์˜๋ฏธํ•˜๋Š” commit)์„ ์ƒ์„ฑ
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ ๋ณ€๊ฒฝ ์—†์ด ํ•ด๋‹น ์ปค๋ฐ‹ ๋‚ด์šฉ๋งŒ์„ ์‚ญ์ œํ•œ ์ƒํƒœ์˜ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ์ƒ์„ฑ

08-1



์ถ”๊ฐ€ commit 2๊ฐœ๋งŒ ๋” ๋‚จ๊ธฐ์ž

# undoing์—์„œ ์ด์–ด์„œ ์ง„ํ–‰
$ touch c.txt d.txt
$ git add c.txt
$ git commit -m "Add c.txt"
[master d9c38f7] Add c.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt

$ git add d.txt
$ git commit -m "Add d.txt"
[master aaf2db9] Add d.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d.txt
$ git log --oneline
aaf2db9 (HEAD -> master) Add d.txt
d9c38f7 Add c.txt
d984105 text file
a46391e first commit



revert commit ํŽธ์ง‘๊ธฐ ์‹คํ–‰

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์ด์ „ ์ปค๋ฐ‹์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
$ git revert d984105
Removing b.txt
hint: Waiting for your editor to close the file... error: There was a problem 
with the editor 'vi'.
                     Please supply the message using either -m or -F option.

$ git log --oneline
# ๊ธฐ์กด commit ์ด๋ ฅ์ด ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ํ•˜๋Š” ์‹œ์ ์œผ๋กœ ์–ธ์ œ๋“  
aaf2db9 (HEAD -> master) Add d.txt
d9c38f7 Add c.txt
d984105 text file
a46391e first commit



์ •๋ฆฌ

08-3

image



๊ทธ์™ธ ๋ฐฉ๋ฒ•

$ git reflog
aaf2db9 (HEAD -> master) HEAD@{0}: reset: moving to aaf2db9
d47f656 HEAD@{1}: reset: moving to d47f656
d47f656 HEAD@{2}: commit: text file amend
aaf2db9 (HEAD -> master) HEAD@{3}: commit: Add d.txt
d9c38f7 HEAD@{4}: commit: Add c.txt
d984105 HEAD@{5}: reset: moving to d984105
d6175dd HEAD@{6}: commit (amend): foo & bar
48f0541 HEAD@{7}: commit: foo & bar
d984105 HEAD@{8}: commit (amend): text file
2cc67ed HEAD@{9}: commit: amend text file
a46391e HEAD@{10}: commit (initial): first commit
  • reflog๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ด์ „๊นŒ์ง€ํ–ˆ๋˜ ์ž‘์—…๋“ค reflog๋ฅผ ํ™•์ธํ•ด ๋ช‡๋ฒˆ์งธ HEAD๋กœ ์ด๋™ํ• ์ง€ ํ™•์ธํ•œ๋‹ค.

  • ๋งŒ์•ฝ HEAD@{7}๋กœ ์ด๋™ํ• ๊บผ๋ผ๋ฉด

$ git reset --hard HEAD@{7}



References

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ