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
--detachsit on a commit with no branch attached
-fthrow away local changes to switch anyway

git checkout -b feature/login

branches off the current commit and moves there

git checkout main

switches branch, same as git switch main

git checkout -- src/app.ts

discards 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