Read Anthropic’s case study about Graphite Reviewer

git checkout a file from another branch

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide will explain how to use the git checkout command to retrieve files or directories from another branch, including specific scenarios like checking out from the main branch, retrieving a single file, and dealing with multiple files or directories.

The git checkout command is primarily used for switching between different branches in a Git repository. However, it can also be used to update specific files or directories in your current working directory with versions from other branches. This is useful for reverting changes to a previous state, or for incorporating changes from another branch without fully switching your working context.

The basic syntax for checking out a file or directory from another branch is:

Terminal
git checkout <branch_name> -- <file_path>

Here, <branch_name> is the name of the branch you want to pull the file or directory from, and <file_path> represents the path to the file or directory within the repository that you wish to checkout.

1. Checking out a single file: If you need to retrieve just one file from another branch, specify the file path after the branch name. For example, to check out file.txt from the develop branch, you would use:

Terminal
git checkout develop -- file.txt

This command will replace file.txt in your current working directory with the version from the develop branch.

2. Checking out multiple files: You can also checkout multiple files simultaneously by listing all the paths:

Terminal
git checkout develop -- file1.txt file2.txt dir/file3.txt

This will retrieve file1.txt, file2.txt, and file3.txt from the directory dir from the develop branch.

3. Checking out a directory: To checkout an entire directory (and its contents) from another branch, provide the directory path:

Terminal
git checkout develop -- path/to/directory/

This retrieves the specified directory and all its contents from the develop branch to your current working branch.

4. Checking out files from the main branch: If you're working in a branch and need a file from the main branch, you would use:

Terminal
git checkout main -- example.txt

This command will bring example.txt from the main branch into your current branch.

5. Checking out a specific file from a commit: If you need a version of a file from a specific commit, you can reference the commit hash:

Terminal
git checkout c0a1234 -- file.txt

Here c0a1234 is the commit hash, and file.txt is the file you want to checkout.

  • Checking out from a remote branch: Sometimes, the branch you want to checkout from isn't available locally. First, fetch the remote branches:

    Terminal
    git fetch origin

    Then, use the git checkout command with the remote branch:

    Terminal
    git checkout origin/remote-branch-name -- file.txt
  • Using git checkout for creating a new branch: If you want to create a new branch and start from a specific branch, you can do this:

    Terminal
    git checkout -b new-branch existing-branch

    This command creates and checks out new-branch starting from existing-branch.

  • Avoid overwriting local changes: Be careful when checking out files over modified or unsaved local changes. Git will overwrite local changes with the version from the branch you are checking out from, which could lead to data loss if the changes are not committed.
  • Use git stash if necessary: If you have ongoing work but need to checkout files from another branch, consider using git stash to save your current work:
    Terminal
    git stash push -m "Saving my changes"
    git checkout other-branch -- file.txt
    git stash pop
    This way, your local changes are saved and restored after you've checked out the necessary files.

For further reading see the official Git documentation.

0 min read
0
Share
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2