Throughout our previous articles about Git, we have talked about the git checkout command on multiple occasions. This command is used to switch between branches and restore the files in the working tree. You can also use it to operate files, folders, and commits. Additionally, the phrase checking out implies the act of git checkout command execution.
Merge conflicts in Git are old news. Every developer that has already been in such a situation knows that merge conflicts can compromise files and that steps need to be taken to save the work. In that case, it is often the best solution to take the fresh version of the file from another branch, bring it to your own, and apply the changes.
So far, we’ve usually been using the git checkout command to check out a branch. However, interestingly enough, we can also use it to check out a single file or a whole folder from an entirely separate branch. And that’s precisely the topic of today’s article.
So, stick with us till the end of the article as our web development agency in Chicago explores ways to checkout a file from another branch in Git. Strap up, and let’s go.
How to copy a file from another branch with the git checkout command?
In this section, we’ll talk about the way to copy a single file from one branch to another by using the git checkout command without merging branches.
Let’s say one of our branches contains a file with an update or a bug fix that we wish to pass onto another branch. Merging the entire body of code would be impractical, and we also don’t want to overwrite the chosen file. Let’s see how we can achieve such a feat.
First, let’s check on which branch we are:
Now, let’s create a file and commit it to a different branch:
git checkout -b new_branch
git add test.txt
git commit -m “Create test”
After we’ve created the test.txt file and committed it to another branch, let’s go back to the master branch:
git checkout master
Finally, let’s command Git to check out the file from another branch in order to copy the file:
git checkout new_branch test.txt
Our file is now copied to our current branch. We can verify it by using the following command:
How to copy multiple files from another branch with the git checkout command?
To copy multiple files, simply enter:
git checkout <other_branch_name> — path/to/those/files
Note that the double dash (–) is optional, but it is often used to avoid confusion. Let’s look at a couple of examples:
git checkout — <xyz>
This command tells Git to check out the file xyz from HEAD and overwrite local changes. On the other hand, the command in the following example could mean something completely different:
git checkout xyz
This command can also mean that we wish to check out a branch named xyz if the branch by that name exists.
When <paths> or — patch is specified, the git checkout command won’t switch branches. Instead, it will update the specified paths in the working tree from the index file or from a named <tree-isn> (Git’s way of referring to a particular commit or tree). So, you can use the <tree-ish> argument to refer to a specific tree-ish (such as a specific commit, tag, or tree) in order to update the index for the given paths before you update the working tree.
How to copy a folder from another branch with the git checkout command?
Aside from checking out files, your workflow may require copying an entire folder from a separate one into your current branch. You can quickly execute this with the following command:
git checkout <other_branch_name> — path/to/the/folder
How to copy files or folders from another branch commit with the git checkout command?
Besides copying from another branch, Git also allows you to pick a specific commit from which you wish to copy files or folders. To do so, enter the following:
git checkout <commit_hash> <relative_path_to_file_or_directorium>
Here is an important tip to remember in order to ensure that your work is done correctly:
By using the commit hash, you will be able to pull files from any commit of your choice. You can also opt for wildcards. However, it is important to wrap them in single quotes, so they do not get interpreted by the shell.
How to copy a version of a file using the git show command?
Alternatively, you can use the git show command to get the same outcome. Here is how:
git show comit_hash:path/to/file > path/to/file
Get a file using the git restore command
The same functionality can be achieved using git switch followed by git restore. Here is an example:
Let’s say we have two branches named branch_A and branch_B. If we decide to pull the file test.txt from branch_B to branch_A, we simply type:
git switch branch_A
git restore –source branch_B — test.txt
The git checkout command vs. the git clone command
We’ve noticed that many people get confused about the difference between git checkout and git clone. While they do seem to have similar functions, they are not interchangeable.
The git checkout command is used to switch code versions on the local system, while the git clone command operates to fetch code from a remote repository.
This concludes yet another one of our articles about this amazing version control system. We hope to have provided you with plenty of helpful information throughout our Git series. Make sure to regularly visit our blog pages as there is much more to come.
Since coding is your passion, we suggest you check our take on why custom-coded websites outperform themes and builders. Also, feel free to schedule a call and get in touch with our agency. We offer web development services, and our team is eager to work closely with you in order to deliver a top-tier product.