본문 바로가기
Tools/Git

[Git] checkout 대신 switch와 restore를 사용하기

by A6K 2022. 9. 17.

예전부터 git 명령을 사용한 유저에게 가장 많이 사용되는 명령을 꼽으라면 아마도 checkout이 다섯 손가락 안에 나올 것이다. 그런데 어느순간부터 git --help 로 git 명령을 출력했을 때 checkout에 대한 설명이 없어졌다.

git --help 를 실행하면 다음 결과를 얻게 된다.

$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone             Clone a repository into a new directory
   init              Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add               Add file contents to the index
   mv                Move or rename a file, a directory, or a symlink
   restore           Restore working tree files
   rm                Remove files from the working tree and from the index
   sparse-checkout   Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
   bisect            Use binary search to find the commit that introduced a bug
   diff              Show changes between commits, commit and working tree, etc
   grep              Print lines matching a pattern
   log               Show commit logs
   show              Show various types of objects
   status            Show the working tree status

grow, mark and tweak your common history
   branch            List, create, or delete branches
   commit            Record changes to the repository
   merge             Join two or more development histories together
   rebase            Reapply commits on top of another base tip
   reset             Reset current HEAD to the specified state
   switch            Switch branches
   tag               Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch             Download objects and refs from another repository
   pull              Fetch from and integrate with another repository or a local branch
   push              Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

출력된 도움말을 살펴봐도 checkout에 대한 설명이 없다.

이유는 간단하다. git의 2.23버전부터 checkout 명령을 대신할 switch 명령과 restore 명령이 도입되기 때문이다. checkout 명령이 이름과 다르게 이런저런 용도로 사용되어 혼란을 줄 수 있기 때문에 두 명령으로 분리하기 시작한 것이다.

git switch

git checkout 명령의 가장 큰 사용처는 브랜치를 변경하는 부분이다.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

이런식으로 'git checkout <branch name>' 명령을 실행하면 입력한 브랜치로 전환된다. 만약 브랜치가 없다면 -b 옵션을 이용해서 생성할 수도 있다.

$ git checkout -b test
Switched to a new branch 'test'

checkout 명령의 이 기능이 switch 명령으로 대체되었다. checkout 명령의 -b 옵션은 switch의 -c 옵션(create)으로 대체되었다.

$ git switch -c test
Switched to a new branch 'test'
$ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

브랜치를 새로 생성할 때 switch -c 를 이용했다. 만약 HEAD가 아니라 특정 브랜치나 커밋에서 새로운 브랜치를 만들고 싶으면 브랜치 이름 뒤에 커밋을 지정하면 된다.

$ git switch -c new_branch 7fa28a
Switched to a new branch 'new_branch'

git restore

git checkout의 또 다른 기능은 working tree의 파일 수정을 복원하는 것이다. 예를 들어 README.md 파일이 수정되었다고 하자. 작성을 하다가 수정사항을 롤백하고 이전에 커밋된 상태로 돌리고 싶은 경우가 있다.

이 때, checkout -- README.md 명령을 이용했었다.

$ git checkout -- README.md

하지만 눈에 딱 들어오지 않았다. 이 기능이 restore 명령으로 바뀌었다.

$ git restore README.md

추가로 stage에 들어간 수정 내용을 커밋에서 제외하고 싶을 때, reset 명령을 사용했었는데 이제 restore 명령을 사용하면 된다.

$ git reset HEAD README.md
$ git restore --stated README.md

 

댓글