4 ๋ถ„ ์†Œ์š”



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



References

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

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