๊น(GIT) ์ฌ์ฉ๋ฒ: undoing, amend, reset, revert
undoing
-
์ด๋ค ํ์๋ฅผ ์ทจ์ํ ๋ ์ฌ์ฉํ๋ ๋ช ๋ น์ด
-
์ฌ์ ์ค๋น
# git ์ด๊ธฐํ & a.txt README.md ํ์ผ ์์ฑ

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 # ๋ฌด๋์์ ๋ด๋ ค์ด!!

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 ํ์ผ์ ๊ฐ๊ฐ ๋ฉ์์ง๋ฅผ ๋จ๊ฒจ๋ณด์

$ 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

์ฒซ ๋ฒ์งธ์ ๋ ๋ฒ์งธ ๋ญ๊ฐ ๋ค๋ฅผ๊น?
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์ด ํ๋ฒ๋ ์๋ ์น๊ตฌ
๋ค์ ์ ๋ฆฌํ๋ฉด
git rm --cached <file>- ๊ธฐ์กด์ ์ปค๋ฐ์ด ์๋ ๊ฒฝ์ฐ SA -> WD๋ก ๋ด๋ฆด ๋ ์ฌ์ฉ
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
- ์ปค๋ฐ ๋ฉ์์ง๋ฅผ ์๋ชป ์ ์ ๊ฒฝ์ฐ ์์ !!
- ๊ฐ์ฅ ์ต์ ์ commit๋ง ์์ ๊ฐ๋ฅํจ..!
- ์ญ์ฌ๋ฅผ ๋ฐ๊พธ๋ ค๊ณ ํ๋ฉด ์๋จ..!
- ๋๋ฌด ์ผ์ฐ ์ปค๋ฐ์ ํ ๊ฒฝ์ฐ(๋ฌด์ธ๊ฐ ๋นผ๋จน๊ณ 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

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๋ก ๋จ์

# 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)์ ์์ฑ
- ์ปค๋ฐ ํ์คํ ๋ฆฌ ๋ณ๊ฒฝ ์์ด ํด๋น ์ปค๋ฐ ๋ด์ฉ๋ง์ ์ญ์ ํ ์ํ์ ์๋ก์ด ์ปค๋ฐ์ ์์ฑ

์ถ๊ฐ 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
์ ๋ฆฌ


๊ทธ์ธ ๋ฐฉ๋ฒ
$ 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}
๋๊ธ๋จ๊ธฐ๊ธฐ