본문 바로가기

git & github

Git으로 버전 관리하기 2

 

5. 작업 되돌리기

작업트리에서 수정한 파일 되돌리기 : git checkout

$ vi hello.txt
1
2
three

$ 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)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git checkout -- hello.txt

$ cat hello.txt
1
2
3

 

스테이징 되돌리기 : git reset HEAD 파일명

$ vi hello2.txt
A
B
C
D

$ git add hello2.txt    #스테이지에 올리기
warning: LF will be replaced by CRLF in hello2.txt.
The file will have its original line endings in your working directory

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello2.txt

$ git reset HEAD hello2.txt    #스테이징 되돌리기
Unstaged changes after reset:
M       hello2.txt

$ git status
On branch master
Changes not staged for commit:    #스테이지 취소 확인(not staged)
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello2.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

최신 커밋 되돌리기 : git reset HEAD^

가장 마지막에 한 커밋을 취소하는 방법

커밋도 취소되고 스테이지에서도 내려짐

$ vi hello2.txt
A
B
C
D
E

$ git commit -am "message4"
warning: LF will be replaced by CRLF in hello2.txt.
The file will have its original line endings in your working directory
[master 4d4bdb0] message4
 1 file changed, 5 insertions(+), 4 deletions(-)

$ git log
commit 4d4bdb0b828d01fdc4875715cd075d1caa00890f (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:06:27 2020 +0900

    message4

commit 5652137b7fbd5be6422ac1972de3f481e3b0cf2b
Author: unknown <계정명@github.com>
Date:   Tue Apr 14 15:48:23 2020 +0900

    message3

$ git reset HEAD^
Unstaged changes after reset:
M       hello2.txt

$ git log
commit 5652137b7fbd5be6422ac1972de3f481e3b0cf2b (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Tue Apr 14 15:48:23 2020 +0900

    message3

 

git reset 옵션

reset명령은 사용하는 옵션에 따라 되돌릴 수 있는 단계가 다름

reset 옵션

명령 설명
—soft HEAD^ 최근 커밋하기 전 상태로 작업트리를 되돌림
—mixed HEAD^ 최근 커밋과 스테이징을 하기 전으로 되돌림. default 값
—hard HEAD^ 최근 커밋과 스테이징, 파일 수정을 하기 전으로 되돌림. 되돌린 내용은 복구불가

특정 커밋으로 되돌리기 : git reset 커밋해시

특정 버전으로 되돌린 다음 그 이후 버전 삭제 가능

주의점은 reset A를 하면

A 커밋을 reset하는게 아니라 최근 커밋을 A로 리셋

즉, A 커밋이후에 만들어진 커밋을 지우고 A 커밋으로 이동

$ vi rev.txt
a

$ git add rev.txt
warning: LF will be replaced by CRLF in rev.txt.
The file will have its original line endings in your working directory

$ git commit -m "R1"
[master 6c230b6] R1
 1 file changed, 1 insertion(+)
 create mode 100644 rev.txt

$ vi rev.txt
a
b

$ git commit -am "R2"
warning: LF will be replaced by CRLF in hello2.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in rev.txt.
The file will have its original line endings in your working directory
[master 2592f8a] R2
 2 files changed, 6 insertions(+), 4 deletions(-)

***생략***

d까지 입력해서 R4까지 커밋하기

$ git log
commit a3661fde4ef967fa71109392b576f964ba114e33 (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:59:13 2020 +0900

    R4

commit 49d19c46d17bad4015d62bab906d62e219cfc7d3
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:58:57 2020 +0900

    R3

commit 2592f8a1f94027ee02434dc416a5c2beb6ee39be
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:58:05 2020 +0900

    R2

commit 6c230b618ccca51d7736f38f9c2826cb5f35613c
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:57:05 2020 +0900

    R1

$ git reset --hard 2592f8a1f94027ee02434dc416a5c2beb6ee39be
HEAD is now at 2592f8a R2

R4, R3은 삭제되고 R2가 최신 커밋이 됨

$ git log
commit 2592f8a1f94027ee02434dc416a5c2beb6ee39be (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:58:05 2020 +0900

    R2

commit 6c230b618ccca51d7736f38f9c2826cb5f35613c
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:57:05 2020 +0900

    R1

$ cat rev.txt
a
b

 

커밋 삭제하지 않고 되돌리기 : git revert 취소하려는 커밋해시

revert는 취소하려는 커밋버전을 입력함

$ vi rev.txt
a
b
e    #추가

$ git commit -am "R5"
[master b59df92] R5
 1 file changed, 1 insertion(+)

$ git log 
commit b59df92fb11136ecf599c1676308d530f19b04c0 (HEAD -> master)
Author: unknown <계정명@github.com>   
Date:   Wed Apr 29 14:02:11 2020 +0900

    R5

commit 2592f8a1f94027ee02434dc416a5c2beb6ee39be
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:58:05 2020 +0900

    R2

commit 6c230b618ccca51d7736f38f9c2826cb5f35613c
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:57:05 2020 +0900

    R1


$ git revert b59df92fb11136ecf599c1676308d530f19b04c0

Revert "R5" 
for test  
This reverts commit b59df92fb11136ecf599c1676308d530f19b04c0.   
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       modified:   rev.txt
#


$ git log
commit ee5d13a01c79d60d0b2283ceadde8f109e3bef6a (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Wed Apr 29 14:34:30 2020 +0900

    Revert "R5"
    for test

    This reverts commit b59df92fb11136ecf599c1676308d530f19b04c0.

commit b59df92fb11136ecf599c1676308d530f19b04c0
Author: unknown <계정명@github.com>
Date:   Wed Apr 29 14:02:11 2020 +0900

    R5

commit 2592f8a1f94027ee02434dc416a5c2beb6ee39be
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:58:05 2020 +0900

    R2

commit 6c230b618ccca51d7736f38f9c2826cb5f35613c
Author: unknown <계정명@github.com>
Date:   Thu Apr 16 09:57:05 2020 +0900

    R1


$ cat rev.txt
a
b

 

'git & github' 카테고리의 다른 글

원격 저장소와 깃허브  (0) 2020.10.31
브랜치 관리  (0) 2020.10.31
브랜치 병합  (0) 2020.10.31
깃과 브랜치  (0) 2020.10.31
Git으로 버전 관리하기 1  (0) 2020.10.31