git checkout
git
쓰는 꼴
git checkout <branch> | git checkout -b <new> | git checkout -- <file>브랜치를 옮기고 파일도 되돌리는 옛 명령으로, git 2.23에서 switch와 restore로 갈라졌습니다 — checkout -- <file>은 커밋하지 않은 수정을 되찾을 수 없게 지웁니다.
자주 쓰는 옵션
| 옵션 | 뜻 |
|---|---|
| -b <new> | create the branch and switch to it |
| -B <new> | create or reset it to the current commit |
| -- <file> | overwrite the file with the staged version, losing your edits |
| --track <remote>/<branch> | start a local branch that follows a remote one |
| --detach | sit on a commit with no branch attached |
| -f | throw away local changes to switch anyway |
예시
git checkout -b feature/loginbranches off the current commit and moves there
git checkout mainswitches branch, same as git switch main
git checkout -- src/app.tsdiscards uncommitted edits to that file; they are gone
되돌리는 방법이 여럿이라 헷갈립니다 — 가리키는 곳을 옮기는 것과 새 커밋을 얹는 것은 다릅니다.
읽는 방법
- 대괄호 [ ]는 넣어도 되고 안 넣어도 되는 자리입니다.
- 점 셋 …은 여러 개를 이어 쓸 수 있다는 뜻입니다.
- 옵션은 대소문자를 가립니다 — -r과 -R이 다른 명령도 있습니다.
자주 묻는 것
Q. git checkout은 무엇을 하나요?
브랜치를 옮기고 파일도 되돌리는 옛 명령으로, git 2.23에서 switch와 restore로 갈라졌습니다 — checkout -- <file>은 커밋하지 않은 수정을 되찾을 수 없게 지웁니다.
Q. 어떻게 치나요?
git checkout <branch> | git checkout -b <new> | git checkout -- <file> — 대괄호는 생략할 수 있는 자리입니다.
Q. 옵션은 몇 개나 자주 쓰나요?
여기 정리한 것은 6개입니다. 전체 목록은 man git에 있습니다. 이 명령은 git 갈래입니다.
같이 보는 명령
man 페이지: man git-checkout