

This makes sure you always have a reference to your new commits. The point is, your development should always take place on a branch-never on a detached HEAD. When you inevitably check out another branch (e.g., to merge your feature in), there would be no way to reference your feature: If you were to start developing a feature while in a detached HEAD state, there would be no branch allowing you to get back to it. This is a warning telling you that everything you’re doing is “detached” from the rest of your project’s development. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD” state.

Internally, the git checkout command simply updates the HEAD to point to either the specified branch or commit. Remember that the HEAD is Git’s way of referring to the current snapshot. Now that we’ve seen the three main uses of git checkout on branches, it's important to discuss the “detached HEAD” state. Git checkout -b <branchname> git reset -hard origin/<branchname> Detached HEADS To find out what branches are available and what the current branch name is, execute git branch. Usage: Existing branchesĪssuming the repo you're working in contains pre-existing branches, you can switch between these branches using git checkout.
Checkout in sourcetree code#
The difference between the two commands is that clone works to fetch code from a remote repository, alternatively checkout works to switch between versions of code already on the local system. The git checkout command may occasionally be confused with git clone. In addition, branches also facilitate several collaborative workflows. It makes it ridiculously easy to try new experiments without the fear of destroying existing functionality, and it makes it possible to work on many unrelated features at the same time. Having a dedicated branch for each new feature is a dramatic shift from a traditional SVN workflow. Think of it as a way to select which line of development you’re working on. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

The git checkout command lets you navigate between the branches created by git branch. The focus for the majority of this document will be checkout operations on branches.Ĭhecking out branches is similar to checking out old commits and files in that the working directory is updated to match the selected branch/revision however, new changes are saved in the project history-that is, it’s not a read-only operation. In the Undoing Changes topic, we saw how git checkout can be used to view old commits. In addition to the definition of "checkout" the phrase "checking out" is commonly used to imply the act of executing the git checkout command. The git checkout command operates upon three distinct entities: files, commits, and branches. In Git terms, a "checkout" is the act of switching between different versions of a target entity. It will cover usage examples and edge cases. This page is an examination of the git checkout command.
