[GitHub] Git으로 버전 관리하기(2/2)
4️⃣ 버전 만드는 단계마다 파일 상태 알아보기
> git에서는 버전을 만드는 각 단계마다 파일 상태를 다르게 표시한다.
파일의 상태를 이해하면 이 파일이 버전 관리의 여러 단계 중 어디에 있는지, 그 상태에서 어떤 일을 할 수 있는지 알 수 있다.
👾tracked / untracked
git status 을 사용하면 파일 상태와 관련된 여러 메시지가 나타난다.
ex) hello.txt를 수정, hello2.txt 생성
$ git status
==============================================================================
On branch master
Changes not staged for commit: //변경된 파일이 아직 스테이징되지 않음
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt //hello.txt 파일 수정됨
Untracked files:
(use "git add <file>..." to include in what will be commited)
hello2.txt
no changes added to commit (use "git add" and/or "git commit -a")
==============================================================================
→ git은 한번이라도 commit 한 파일의 수정 여부를 계속 추적.
반면 hello2.txt는 untracked file이라고 되어있는데 이는 한 번도 git에서 버전 관리를 하지 않았기 때문에 수정 내역을 추적하지 않는다.
🍎 untracked file = 한번도 commit 하지 않은 파일
$ git add hello.txt //수정된 파일 스테이징
$ git add hello2.txt //생성된 파일 스테이징
$ git status
==============================================================================
On branch master
Changes not staged for commit: //변경된 파일이 아직 스테이징되지 않음
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
new file: hello2.txt
==============================================================================
여기서 commit하면 hello.txt, hello2.txt 모두 커밋된다. (앞에서 스테이지에 있는 모든 파일을 commit한다고 했음)
$ git commit -m "message3"
$ git log
===================================================
commit ////////////////////////\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message3
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message1
===================================================
여기서 각 commit에 어떤 파일들이 관련된 것인지 알 수 없다... 이럴 땐~
$ git log --stat
===================================================
commit ////////////////////////\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message3
hello.txt | 1 +
hello2.txt | 4 ++++
2 files changed, 5 insertion(+)
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message2
hello.txt | 1+
1 file changed, 1 insertion(+)
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message1
hello.txt | 1 +
1 file changed, 1 insertion(+)
===================================================
(Q를 누르면 로그 화면을 빠져 나와 다시 깃 명령 입력 가능)
👾unmodified / modified / staged 상태
tracked 상태 파일은 git 명령으로 파일 상태를 확인하면 현재 작업 트리에 있는지, 스테이지에 있는지 등 더 구체적인 상태를 알려준다.
git의 commit 과정 중에서 tracked 파일의 상태가 어떻게 바뀌는지 확인해보자.
$ git status
//현재 작업 트리에 있는 모든 파일이 수정되지 않았다면 unmodified
nothing to commit, working tree clean
//파일이 수정되었고 아직 스테이지에 올라가지 않았다면 modified
Changes not stage for commit:
$ git add hello2.txt
$ git status
//파일을 스테이징하면 커밋한 변경사항이 있는 것 staged
Changes to be commited:
🚨 방금 커밋한 메세지 수정하기
커밋 메세지를 즉시 수정할 수 있다.
$ git commit --amend
//vim 실행되고 맨 위에 원래 커밋 메세지가 나타난다. i를 눌러서 입력 모드로 바꾸고 메세지 수정 가능
5️⃣ 작업 되돌리기
> 스테이지에 올렸던 파일을 내리거나 커밋 취소하는 등 각 단계로 돌아가는 방법에 대해 알아보자!! 😆
👾작업 트리에서 수정한 파일 되돌리기 - git checkout
파일을 수정한 뒤 소스가 정상적으로 동작하지 않는 등의 이유로 수정한 내용을 취소하고 가장 최신 버전 상태로 되돌려야 할 때가 있다.
checkout 명령어를 사용하면 작업 트리에서 수정한 내용을 쉽게 취소할 수 있다. (되돌린 내용은 다시 복구 불가)
$ vim hello.txt //파일 수정
$ git status
==============================================================================
On branch master
Changes not staged for commit: //변경된 파일이 아직 스테이징되지 않음
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt //hello.txt 파일 수정됨
no changes added to commit (use "git add" and/or "git commit -a")
==============================================================================
→ 아직 staging 하지 않은 상태!!
$ git checkout -- hello.txt
👾스테이징 되돌리기 - git reset HEAD 파일 이름
checkout에서는 파일의 수정을 취소하고 원래대로 되돌렸다. (staging 전 단계에서!)
이번에는 스테이징된 수정 파일을 스테이징 취소해보자.
$ vim hello2.txt
$ git add hello2.txt
$ git status
==============================================================================
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello2.txt //hello.txt 파일 수정됨
==============================================================================
$ git reset HEAD hello2.txt
==============================
Unstaged changes after reset:
hello2.txt
==============================
👾최신 커밋 되돌리기 - git reset HEAD^
수정된 파일을 스테이징하고 커밋까지 했을 때, 가장 마지막에 한 커밋을 취소하는 방법
$ vim hello2.txt
$ git commit -am "message4"
$ git log
===================================================
commit ///////////////////////\\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message4
commit ////////////////////////\\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message3
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message1
===================================================
$ git reset HEAD^
Unstaged changes after reset:
hello2.txt
$ git log
===================================================
commit ////////////////////////\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message3
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
message1
===================================================
🚨 git reset 명령 옵션 정리
--soft HEAD^ //최근 커밋하기 전 상태로 작업트리 되돌림
--mixed HEAD^ //최근 커밋과 스테이징하기 전 상태로 작업트리 되돌림 (default)
--hard HEAD^ //최근 커밋과 스테이징, 파일 수정을 하기 전 상태로 되돌림 (복구 불가)
👾특정 커밋 되돌리기 - git reset 커밋 해시
$ vim rev.txt
$ git add rev.txt
$ git commit -m "R1"
$ vim rev.txt //수정
$ git commit -am "R2"
$ vim rev.txt //수정
$ git commit -am "R3"
$ vim rev.txt //수정
$ git commit -am "R4"
$ git log
===================================================
commit ////////////////////////\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R4
commit ////////////////////////\\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R3
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R1
===================================================
여기서 커밋 해시를 사용해 되돌릴 때, 주의할 것이 있다.
reset A는 A 커밋을 삭제하는 것이 아니라 최근 커밋을 A로 리셋하는 것이다. (그 뒤에 있는 커밋들은 삭제됨)
원하는 커밋 해시를 복사해서
$ git reset --hard /////////////////////////\ //R2
HEAD is now at ////////// R2
$ git log
===================================================
commit /////////////////////////\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R1
===================================================
👾커밋 삭제하지 않고 되돌리기 - git revert
커밋을 되돌리더라도 취소한 커밋을 남겨두어야 할 때가 있다. 그러면 git revert
위에서 "R4"까지 만든 코드에서 git revert를 해보자. (git reset 커밋 해시 전!)
가장 최근에 커밋한 "R4" 버전을 취소하고 직전 커밋 "R2"로 되돌아가자.
$ git revert ////////////////////////\\
//기본 편집기가 자동으로 나타나면서 커밋 메세지를 입력할 수 있다. (저장하기)
Revert R4
일시적으로 커밋 보류함.
This reverts commit ////////////////////////\\
$ git log
===================================================
commit ////////////////////////\\ (HEAD -> master)
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
Revert "R4"
This reverts commit ////////////////////////\\
commit ////////////////////////\\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R4
commit ////////////////////////\\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R3
commit /////////////////////////\
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R2
commit //////////////////////////
Author: nyeong <nyeong@naver.com>
Date: xxx xx xx xxx xxxx xxxx
R1
===================================================
→ R4를 revert한 새로운 커밋이 생겼다. 기존 "R4"가 사라지지 않는다. R4 버전을 지우는 대신 R4에서 변경했던 이력을 취소한 새 커밋을 만드는 것!
참고 서적
Do it! 지옥에서 온 문서관리자 깃&깃허브 입문
생활코딩 강의에서 52개를 엄선, 한 권 안에 자주 쓰는 기능은 모두 담았다. 문서 지옥, 깃&깃허브로 하루 안에 탈출한다! 대학생, 개발자, 일반 사무직까지! 문서 지옥에 빠진 모두를 위한 깃&깃
digital.kyobobook.co.kr