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.
Understanding the git checkout command
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.
Basic syntax
The basic syntax for checking out a file or directory from another branch is:
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.
Examples
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:
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:
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:
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:
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:
git checkout c0a1234 -- file.txt
Here c0a1234 is the commit hash, and file.txt is the file you want to checkout.
Advanced usage
Checking out from a remote branch: Sometimes, the branch you want to checkout from isn't available locally. First, fetch the remote branches:
Terminalgit fetch originThen, use the
git checkoutcommand with the remote branch:Terminalgit checkout origin/remote-branch-name -- file.txtUsing
git checkoutfor creating a new branch: If you want to create a new branch and start from a specific branch, you can do this:Terminalgit checkout -b new-branch existing-branchThis command creates and checks out
new-branchstarting fromexisting-branch.
Best practices
- 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 stashif necessary: If you have ongoing work but need to checkout files from another branch, consider usinggit stashto save your current work:This way, your local changes are saved and restored after you've checked out the necessary files.Terminalgit stash push -m "Saving my changes"git checkout other-branch -- file.txtgit stash pop
For further reading see the official Git documentation.