git checkout

Git checkout acts in different ways, depending on what you pass as the argument to it.

Passing a file or folder name to git checkout

If you pass a file or folder name to it, git checkout will throw away the working changes from that file or folder. You can also use the -p flag to selectively throw out only specific lines of changes.

This command is great for cleaning up and removing little debug statements and things that you added at some point but don’t need any more and don’t want to end up in any commit.

Learn how to use “git checkout -p”

Passing a commit reference to git checkout

If you pass a commit reference to git checkout (such as as branch name, commit id, or tag), Git will attempt to change your current context to that commit, effectively doing the following:

  • Setting your current HEAD to the commit specified
  • Setting the current branch to the branch specified (if you specified a branch)