In Git, the checkout
command is used to switch between branches in your repository.
This guide will explain how to use the git checkout
command specifically to switch to the main branch, update it, and handle common tasks related to this branch.
Although the industry has been shifting towards using the term "main" for the default branch, "master" is still sometimes used in many existing repositories. For the purposes of this guide, we'll be exclusively using main
. If your repository is still using the deprecated naming convention of master
simply substitute master
for main
in all of the following commands.
Understanding git checkout
The git checkout
command is used to switch branches or restore working tree files. It updates the files in the working directory to match the version stored in that branch, and it updates the Git's HEAD (the pointer to the commit at the tip of a branch) to point to the specified branch.
Step 1: Checking out the main branch
To switch to the main branch in your local repository run:
git checkout main
This command will switch the current working directory to the main branch, updating the working directory to reflect the state of the specified default branch.
Step 2: Checking out main from the origin
If you're working with remote repositories (often named "origin"), you might need to check out the main branch as tracked on the remote. Before checking out, you should ensure that your list of branches is up-to-date:
git fetch origin
Then, to switch to the main branch exactly as it is on the remote repository:
git checkout origin/main
This command checks out the main branch from the origin remote into a detached HEAD state in your local repository, meaning any commits you make will not be on any branch and can be lost if not handled correctly. It’s mainly useful for inspecting the remote branch or for temporary experiments.
Step 3: Updating the main branch
If you are working directly on the main or main branch and there are updates on the remote, you may want to update your local main branch:
git checkout maingit pull origin main
This sequence of commands first switches to the main branch, then pulls the latest changes from the main branch of the origin remote, merging them into your local main branch.
Step 4: Pulling changes after checkout
Often, you might switch to the main branch and immediately need to update it to ensure it's up-to-date with the remote repository:
git checkout maingit pull
This is a straightforward way to both switch to the main branch and fetch and merge the latest changes from the upstream branch.
Common issues and resolutions
git checkout main
not working: This could occur if the main branch does not exist locally. Make sure you have the main branch by listing all branches withgit branch -a
. If it's missing, you may need to fetch from the remote and then try checking out again.Detached HEAD state: If you find yourself in a detached HEAD state after checking out
origin/main
, you can return to the main branch and merge your changes:Terminalgit checkout maingit merge origin/mainThis will restore your local state to track the mainline of the branch, putting you back into lockstep with the history of the repository.
Branch not up to date: If
git pull
does not update your branch as expected, ensure that your local branch tracks the appropriate remote branch. Check the tracking relationship usinggit branch -vv
and set it with:Terminalgit branch --set-upstream-to=origin/main main
For further reading see the official Git documentation on branch management.
Using the Graphite CLI and gt checkout
Graphite CLI enhances branch management workflows, and the gt checkout
command is a simplified and efficient alternative to git checkout
. It is especially useful for teams leveraging Graphite’s stacked branch workflows.
Step 1: Installing Graphite CLI
Before using gt checkout
, ensure that the Graphite CLI is installed. Install it globally using npm:
npm install -g @graphite/cli
Verify the installation with:
gt --version
Step 2: Checking out the main branch
To switch to the main branch using Graphite CLI, run:
gt checkout main
This command seamlessly switches to the main branch and ensures it integrates properly into any stacked workflows you may have.
Step 3: Updating the main branch
To update the main branch with the latest changes from the remote, use:
gt checkout maingt up
The gt up
command fetches and merges changes from the remote, ensuring your local branch is fully updated.
Step 4: Working with feature branches
Switching to a feature branch in a stack is just as simple:
gt checkout feature-branch
This maintains your branch hierarchy and keeps your workflow consistent with Graphite’s stack-based approach.
For more details on gt checkout
and other Graphite CLI features, see the official documentation.