๊น(GIT) (6)- undoing, amend
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
๋๊ธ๋จ๊ธฐ๊ธฐ