reset 옵션은 이전 돌아가고 싶은 커밋으로 가고 싶을 때 사용한다.
# git reset
git reset --soft "commit hash"
git reset --mixed "commit hash"
git reset --hard "commit hash"
git reset HEAD~10
git reset HEAD^
1. git reset --soft "commit hash"
soft -> commit 된 파일들을 staging area로 돌려 놓는다.
git log 확인을 해보고 2번째 커밋목록으로 이동을 한 상태이다.
git status 를 확인 해보면 add 까지 된 상태이다.
limjian@Jians-MacBook-Pro-13 test-repository % git log
commit 760466e1c4ce72955537f6ec76f6d307b8cb98a8
(HEAD -> main, origin/main, origin/HEAD)
Author: jian-Lim
Date: Wed Feb 23 15:13:18 2022 +0900
TEST git reset commit 8d23327065eef90a44dbe30f10c2d7d81e3aaf0d
Merge: bd0a5f1 9a203a7 Author: jian-Lim Date: Wed Feb 23 13:21:27 2022 +0900
Merge branch 'main' of https://github.com/LimJiAn/test-repository
limjian@Jians-MacBook-Pro-13 test-repository % git reset --soft 8d23327065eef90a44dbe30f10c2d7d81e3aaf0d
limjian@Jians-MacBook-Pro-13 test-repository % git status
On branch main Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
limjian@Jians-MacBook-Pro-13 test-repository %
2. git reset --mixed "commit hash"
(--mixed 옵션을 주지 않은 상태로 사용해도 같다 (default))
mixed -> commit된 파일들을 working directory로 돌려놓는다.
git status 를 확인 해보면 add 하기 전 상태이다.
limjian@Jians-MacBook-Pro-13 test-repository % git reset --mixed 8d23327065eef90a44dbe30f10c2d7d81e3aaf0d
Unstaged changes after reset: M README.md
limjian@Jians-MacBook-Pro-13 test-repository % git status
On branch main Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
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: README.md
3. git reset --hard "commit hash"
hard -> commit 된 파일들 중 tracked 파일들을 working directory에서 삭제한다.
(Untracked 파일은 여전히 Untracked로 남는다.)
아예 수정 내용이 없이 그 커밋으로 돌릴 때 사용하는 방법이다.
limjian@Jians-MacBook-Pro-13 test-repository % git reset --hard 8d23327065eef90a44dbe30f10c2d7d81e3aaf0d
HEAD is now at 8d23327 Merge branch 'main' of https://github.com/LimJiAn/test-repository
limjian@Jians-MacBook-Pro-13 test-repository % git status
On branch main Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
limjian@Jians-MacBook-Pro-13 test-repository %
3. git reset HEAD~취소할커밋수
-> 현재 커밋으로부터 원하는 만큼의 커밋이 취소된다. (mixed 방식)
git status 를 확인 해보면 add 하기 전 상태이다.
limjian@Jians-MacBook-Pro-13 test-repository % git reset HEAD~3
Unstaged changes after reset:
M README.md
limjian@Jians-MacBook-Pro-13 test-repository % git status
On branch main Your branch is behind 'origin/main' by 5 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
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: README.md
no changes added to commit
(use "git add" and/or "git commit -a")
limjian@Jians-MacBook-Pro-13 test-repository %
4. git reset HEAD^
-> 가장 최근의 커밋이 취소된다. (mixed 방식)
git status 를 확인 해보면 add 하기 전 상태이다.
limjian@Jians-MacBook-Pro-13 test-repository % git reset HEAD^
Unstaged changes after reset:
M README.md
limjian@Jians-MacBook-Pro-13 test-repository % git status
On branch main Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
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: README.md
no changes added to commit
(use "git add" and/or "git commit -a")
limjian@Jians-MacBook-Pro-13 test-repository %
'Server > Git' 카테고리의 다른 글
[Git] refusing to allow a Personal Access Token to create or update workflow (0) | 2023.08.08 |
---|---|
[Git] Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. (0) | 2022.07.25 |
[git] git commit --amend (커밋 메시지 수정) (0) | 2022.02.23 |
[git] main branch (0) | 2022.01.29 |
[Git] git flow (0) | 2022.01.29 |