🔥 git checkout

731자
7분

git checkout 명령어를 사용하면 원하는 커밋이나 브랜치로 작업 트리를 전환할 수 있습니다. 이 명령어는 Git에서 매우 다재다능하게 활용되는데요, 자주 사용되는 몇 가지 시나리오를 살펴보겠습니다.

1. 특정 커밋으로 체크아웃하기

저장소의 히스토리에서 특정 커밋으로 이동하고 싶다면 아래와 같이 git checkout 명령어에 커밋 해시를 지정해 주면 됩니다.

$ git log
 
commit caf90dee1b4c6b3b00c0149039d34864346e90e1 (**HEAD ->** **main**)
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:07:32 2024 +0900
 
    Update README
 
 
commit 070ebaca5cd8eda7a17b5759dbe2b06c986e876c
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:07:24 2024 +0900
 
    Initial commit
 
 
commit 713d30794120139e6f55aba91c0e2830ada99ff0
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:00:54 2024 +0900
 
    Update hello.txt
 
 
commit 160ea51d9b3e1d3fdfcb0e22ba94f223646b9724
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 17:56:35 2024 +0900
 
    Add message
 
 
commit 9c5b645ff15259a4e56e078a53b836a0bd26d8eb
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 17:55:17 2024 +0900
 
    Add hello.txt
 
 
commit fe5b5e06fd88611c44d7630010e93bb01b4e48cb (**origin/main**, **origin/HEAD**)
Author: codingmax <codingmax@tv.local>
Date:   Fri Mar 29 17:37:50 2024 +0900
 
    update README.md
 
 
commit 103d6c84cc6d52f11993fdadd69c8d914e46917e (**origin/develop**)
Author: codingmax <codingmax@tv.local>
Date:   Fri Mar 29 17:28:10 2024 +0900
 
    Initial commit
 
shell
$ git checkout 160ea51d9b3e1d3fdfcb0e22ba94f223646b9724
shell

위 명령어를 실행하면 160ea51d9b3e1d3fdfcb0e22ba94f223646b9724 커밋 시점으로 작업 트리가 전환됩니다.

Previous HEAD position was caf90de Update README
HEAD is now at 160ea51 Add message
shell

이때 Git은 'detached HEAD' 상태로 들어가는데요, 이는 현재 브랜치에서 벗어나 특정 커밋을 가리키고 있음을 나타냅니다.

$ git branch
 
* (HEAD detached at 160ea51)
  feature/login
  main
shell

2. 브랜치 전환하기

개발을 하다 보면 여러 브랜치를 오가며 작업해야 할 때가 많습니다. git checkout 명령어로 브랜치를 전환할 수 있어요.

$ git checkout feature/login
shell
Previous HEAD position was 160ea51 Add message
Switched to branch 'feature/login
shell

위 명령어는 feature/login 브랜치로 전환합니다. 브랜치를 전환하면 해당 브랜치가 가리키는 커밋으로 작업 트리가 변경되고, HEAD는 그 브랜치를 가리키게 됩니다.

$ git branch
* feature/login
  main
shell

3. 브랜치를 새로 생성하면서 전환하기

git checkout 명령어에 -b 옵션을 주면 브랜치를 생성함과 동시에 그 브랜치로 전환할 수 있습니다. 아래 명령어를 확인해 보세요.

$ git checkout -b feature/signup
Switched to a new branch 'feature/signup'
 
shell

위 명령어는 feature/signup이라는 새 브랜치를 생성하고, 바로 그 브랜치로 전환합니다.

$ git branch
  feature/login
* feature/signup
  main
 
shell

이 옵션을 사용하면 브랜치를 만드는 것과 전환하는 것을 한 번에 할 수 있어 편리하죠.

4. 개별 파일 되돌리기

만약 작업 트리의 일부 파일만 이전 상태로 되돌리고 싶다면, git checkout 명령어에 파일 경로를 지정해 주면 됩니다.

# 특정 파일을 이전 커밋 상태로 되돌리기
git checkout HEAD~2 -- path/to/file.txt
 
# 여러 파일을 한꺼번에 되돌리기
git checkout HEAD -- path/to/file1.txt path/to/file2.txt
 
shell

위 예시처럼 -- 뒤에 되돌리고 싶은 파일 경로를 지정해 주면, 해당 파일들만 선택적으로 이전 상태로 돌아갑니다. 이 기능은 실수로 파일을 삭제했거나 변경 사항을 취소하고 싶을 때 유용하게 쓰입니다. 예로 README.md 파일을 feature/signup 브랜치에서 삭제하고 되돌려 볼게요.

$ git branch
  feature/login
* feature/signup
  main
shell
$ rm -rf README.md
$ git add .
$ git commit -m "Delete README.md"
[feature/signup 2fa353f] Delete README.md
 1 file changed, 1 deletion(-)
 delete mode 100644 README.md
shell
$ git log
[feature/signup 2fa353f] Delete README.md
 
 1 file changed, 1 deletion(-)
 
 delete mode 100644 README.md
 
codingmax@tv git-commands-cookbook % git log
commit 2fa353fefce1095d375f6541f24af17091fec802 (**HEAD ->** **feature/signup**)
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:36:21 2024 +0900
 
    Delete README.md
 
 
commit caf90dee1b4c6b3b00c0149039d34864346e90e1 (**main**, **feature/login**)
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:07:32 2024 +0900
 
    Update README
 
 
commit 070ebaca5cd8eda7a17b5759dbe2b06c986e876c
Author: CodingMax <biz.codingmax@gmail.com>
Date:   Fri Mar 29 18:07:24 2024 +0900
 
    Initial commit
 
shell
$ ls -al
 
total 24
drwxr-xr-x   6 codingmax  staff   192  3 29 18:36 .
drwxr-xr-x  10 codingmax  staff   320  3 29 17:47 ..
drwxr-xr-x  14 codingmax  staff   448  3 29 18:36 .git
-rw-r--r--   1 codingmax  staff  2047  3 29 17:37 .gitignore
-rw-r--r--   1 codingmax  staff  1066  3 29 17:37 LICENSE
-rw-r--r--   1 codingmax  staff    13  3 29 18:31 hello.txt
shell
$ git checkout HEAD~2 -- README.md
$ % ls -al
 
total 32
drwxr-xr-x   7 codingmax  staff   224  3 29 18:38 .
drwxr-xr-x  10 codingmax  staff   320  3 29 17:47 ..
drwxr-xr-x  14 codingmax  staff   448  3 29 18:38 .git
-rw-r--r--   1 codingmax  staff  2047  3 29 17:37 .gitignore
-rw-r--r--   1 codingmax  staff  1066  3 29 17:37 LICENSE
-rw-r--r--   1 codingmax  staff    15  3 29 18:38 README.md
-rw-r--r--   1 codingmax  staff    13  3 29 18:31 hello.txt
shell

이렇게 git checkout 명령어를 활용하는 다양한 방법을 알아보았는데요, 상황에 맞게 적절히 사용한다면 Git으로 더욱 효과적인 버전 관리를 할 수 있을 거예요. 예제를 직접 따라해 보면서 익숙해지는 것도 좋겠죠?

lecture image

위 다이어그램은 git checkout을 사용해 브랜치와 커밋 사이를 이동하는 과정을 도식화한 것입니다. 실제로 명령어를 사용해 가면서 그림과 같은 동작이 일어나는지 확인해 보세요!