Pro git - 2.4
Updated:
Pro git - 되돌리기
1. 되돌리기
- 커밋한 내용을 다시 커밋하고 싶으면 파일 수정 작업을 하고 staging Area에 추가한 다음 –amend 옵션을 사용하여 커밋을 재작성할 수 있다.
$ git commit --amend
$ git commit --amend -m "여기에 커밋 수정 메시지"
-
커밋 후에 수정한 것이 없고 커밋 메시지만 수정하고 싶으면 사용하면 됨
-
커밋을 했는데 Stage에 추가하는 파일을 빼먹어서 다시 넣고 싶다면 아래와 같이 하면 된다.
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
- 이렇게 하면 커밋 한개로 기록된다. 커밋이 덮어 씌워진것이다.
- 따라서 정말로 사소하고 마지막 확인 작업에서 빼먹은 파일이 있다면 사용할 때 유용하다.
2. 파일 상태를 Unstage로 변경하기
-
Staging Area와 워킹 디렉토리 사이를 넘나드는 방법이다.
- git reset HEAD < file> 명령어는 Unstaged 상태로 파일을 변경한다.
git reset
명령은 매우 위험하다.--hard
옵션과 함께 사용하면 더욱 위험하다. 하지만 옵션 없이 사용하면 워킹 디렉토리의 파일은 건드리지 않는다.
예제
- 2020-05-21-progit-10.md을 생성
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
_posts/book/git/2020-05-21-progit-10.md
nothing added to commit but untracked files present (use "git add" to track)
ㅠ
- 2020-05-21-progit-10.md을 add로 staged 상태로 변경(2020-05-21-progit-09.md 변경되어 같이 추가했다고 가정하자.) 이때 2020-05-21-progit-10.md만 빼고 싶다.
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io (master)
$ git add .
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: _posts/book/git/2020-05-21-progit-09.md
new file: _posts/book/git/2020-05-21-progit-10.md
- 2020-05-21-progit-10.md을 git reset HEAD 명령어로 Unstaged로 변경
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io (master)
$ git reset HEAD _posts/book/git/2020-05-21-progit-10.md
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: _posts/book/git/2020-05-21-progit-09.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
_posts/book/git/2020-05-21-progit-10.md
3. Modified 파일 되돌리기
- checkout은 어떤 파일을 수정하고 나서(unstaged 상태일때) 다시 되돌릴 때 사용(최근 커밋된 버전으로 되돌리는 방법)
- add하기 전에 사용해야 함
- add하고 나서는 되돌리고 싶으면 reset하고 checkout해야함
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io/_posts/book/git (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: 2020-05-21-progit-10.md
no changes added to commit (use "git add" and/or "git commit -a")
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io/_posts/book/git (master)
$ git checkout -- 2020-05-21-progit-10.md
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io/_posts/book/git (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
- 파일 안의 내용도 마지막 commit 전으로 돌아감. 따라서 커밋하지 않고 수정되기 전으로 돌아가기 위해서 되돌리고 싶을 때 사용하는데 중요한 점은 수정했던 시점으로는 다시 돌아갈 수 없다.
- 보통 이거보다 stash와 branch를 사용하여 수정 전으로 돌아간다.