

For example, if you were switching a branch (aspect A), you’d probably also want Git to set the content of your files to reflect their state in the branch you were switching to (aspect B). Granted, these aspects are intertwined, with B being a corollary of A. Changing HEAD to point to a branch or a commit, and.6 But generally, we see that git checkout deals with two aspects of the Git repository: That’s not all it can do there are other possible variations through its long/short options, perhaps as a result of Git’s growth from its open source contributors. When given a commit, git checkout sets HEAD to point to.When given a branch, git checkout sets HEAD to point to.Phew, there are quite a few things that git checkout can do! If you were to switch away to another branch, and not point a reference to your new commit, there is a chance your new commit will be lost through garbage collection. For example, if you were in this state and made a new commit to add padding to a header, here’s what your Git history would look like: If you were to make a new commit while in this state, HEAD would advance accordingly, but these commits would not be reachable through the usual Git references, like branches and tags. (In fact, you can perform the equivalent operation by invoking git checkout with the -detach argument). To confirm your hypothesis, you can explore your project’s state as-of commit f7884 and set the contents of the files in your Git repository correspondingly via:Īpart from setting the contents of your files, it also sets HEAD to point to the commit f7884, unlike a branch in the second operation we looked at: For example, let’s say you see a page to be laid out weirdly, even though you remember it being pixel-perfect when you last worked on it about a week ago, with commit f7884. In addition to setting HEAD to point to a named branch, you can also point it to a commit, which brings us to another git checkout operation. Both HEAD and the branch would reference the same commit. By setting HEAD to, for example, a branch, as in the second operation we looked at, Git would report changes by comparing it against the contents of the branch that HEAD points to 4. But how does Git determine when a file has changed? One of Git’s roles is to track content, and it helps us to know what changes we have. As the concept of HEAD is pretty important, let’s take a look at what HEAD is before continuing our exploration of git checkout. However, instead of saying “setting the branch we’re on,” it’s more accurate to say that git checkout sets HEAD to point to. When given a branch, git checkout sets the branch we’re on to.When given a file path, git checkout sets to its contents in the index if is provided, git checkout sets to its contents in.Let’s add that our list of what git checkout does: This checks out the last branch you were on, much like how cd - in your shell changes you back to the last directory you were in. You can do so by calling git checkout with one file path or more: Perhaps you were trying something out and made some changes to the files in your local Git repository, and you now want to discard those changes. Let’s take a look at what git checkout can do, and an alternative (or two) that can make for a friendlier user experience in Git.

Learner: What can I run to discard changes to a file?Įven if you have the commands memorized, there have likely been times when you had to pause after typing a git checkout command while you tried to match it with the operation you had in mind (e.g., “I just typed git checkout … to do X, but I thought git checkout does Y, does this really do what I want?") Learner: What do I run to change the branch I’m on?

But a single command doing more than one thing might produce a suboptimal user experience for someone learning Git. I have found git checkout to be a command that I reach for pretty frequently, as it performs more than one operation. If you’re like me and you’ve worked with Git for some time, you might have a couple of commands committed to your memory-from git commit for recording your changes, to git log for sensing “where” you are.
