git commands


git checkout

Switch branches or restore files. Newer git splits these jobs into git switch and git restore, but checkout still does both.

git checkout <branch>
Save

Angle brackets are yours to fill in. Windows, Linux, macOS.

Examples for git checkout

  • Create a new branch and switch to it

    git checkout -b <branch>
    Save

    Starts the branch at the current commit. The newer equivalent is git switch -c <branch>.

    gittoolcheckoutsubcommand-bcreate it first<branch>argument
  • Discard changes to a file

    git checkout -- <path>
    Save

    Puts the file back to how it is in the index, which is the last commit unless you staged it. The newer equivalent is git restore <path>.

    The uncommitted changes to that file are gone. Git keeps no copy of them.

    gittoolcheckoutsubcommand--what follows is a path<path>path
  • Check out a branch from the remote

    git checkout <branch>
    Save

    If no local branch has that name but exactly one remote has, git creates a local branch that tracks it. If git says the branch does not exist, run git fetch first.

    gittoolcheckoutsubcommand<branch>the name without origin/
  • Get one file from another branch

    git checkout <branch> -- <path>
    Save

    Copies the file as it is on <branch> into your working tree and stages it. Your current branch does not change.

    Replaces your copy of the file, including any uncommitted changes to it.

    gittoolcheckoutsubcommand<branch>argument--flag<path>path
  • Look at an old commit

    git checkout <commit>
    Save

    Moves you to a detached HEAD: you are on that commit, not on a branch. To go back, check out the branch again. If you commit here and want to keep it, create a branch with git checkout -b <name> first.

    gittoolcheckoutsubcommand<commit>a hash, tag or HEAD~n

Flags

-b <branch>
Create a new branch and switch to it.
-B <branch>
Create the branch, or reset it if it exists, and switch to it.Resetting an existing branch drops its commits from that branch.
--track -t
Set the new branch's upstream to the remote branch it starts from.
--
Everything after this is a path, not a branch. Use it when a file and a branch have the same name.