[Git] git reset

2022. 2. 23. 15:27· Server/Git

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
'Server/Git' 카테고리의 다른 글
  • [Git] refusing to allow a Personal Access Token to create or update workflow
  • [Git] Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
  • [git] git commit --amend (커밋 메시지 수정)
  • [git] main branch
임쟌
임쟌
임쟌
Jian's Blog
임쟌
전체
오늘
어제

공지사항

  • [자기소개]
  • 쟌's Blog (227)
    • Language (32)
      • Python (8)
      • Go (24)
      • Java (0)
    • Framework (10)
      • Django (9)
      • Gin (1)
      • Spring boot (0)
      • Fiber (0)
    • Database (10)
      • PostgreSQL (8)
      • MySQL (0)
      • Redis (2)
    • Server (51)
      • Linux (16)
      • Git (12)
      • Oracle Cloud Infrastructure (13)
      • Mac (4)
      • Docker (4)
      • RabbitMQ (0)
      • ETC (2)
    • Operating System (0)
      • OS (0)
    • Algorithm (22)
      • Go (22)
      • Python (0)
    • Exam Certification (4)
    • Daily Life (27)
      • Review (21)
      • Diary (6)
    • 이공계전문기술연수 (71)
      • Java (17)
      • Database (8)
      • HTML | CSS (13)
      • JavaScript | jQuery (6)
      • Servlet | JSP (16)
      • Spring Framework (11)

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
임쟌
[Git] git reset
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.