Git: How to Checkout a File from Another Branch?

Learn via video courses
Topics Covered

Overview

In many scenarios where we want to check out some file(s) from different branches (perform the git checkout file from another branch) or we want to update some specific file(s) and directory(es) of our current branch with files and directories of another branch, then Git provides us several ways to do so. It is quite helpful as we do not need to perform merging, we can directly checkout files. In a situation where a branch is not ready to be merged and we want to check out some files then we can use this Git feature using any of these three commands: git checkout command, git restore command, and git show command.

Pre-Requisites

The prerequisites for learning the topic - Git checkout file from another branch, can be a basic understanding of Version Control Systems, Branching, and Git. Before learning about how to perform: the Git checkout file from another branch, let us discuss them briefly.

A version control system is a tool in software dev that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. Apart from that, we must know the concepts of the git checkout command and the git switch command as both of these commands will help us to change the branch.

Before learning how to change the Git repository, we must be familiar with the concept of branching. A branch is an independent line of dev that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the dev is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel dev. The command can create, rename, list, and delete branches.

Introduction

To learn how we can perform Git checkout files from another branch, let us first learn why do we need it with the help of an example.

Suppose you are working on a big project along with numerous developers. You guys have made a version of the project and now you want to add some new feature(s). So for adding any new feature, we would create a branch as it is the most common and obvious thing. Suppose that we have made some features and in a branch, we have made a registration page. Another feature residing in the other branch will have the same registration page. Now rather than making the same page again, you can copy that file from that feature branch to your current feature branch. Now copying or checking out files of another branch can be performed in various ways. Let us learn about the various ways to perform - the git checkout file from another branch.

In many scenarios where we want to check out some file(s) from different branches or we want to update some specific file(s) and directory(es) of our current branch with files and directories of another branch, then Git provides us several ways to do so. This method is quite helpful as we do not need to perform merging, we can directly checkout files. In a situation where a branch is not ready to be merged and we want to check out some files then we can use this Git feature.

To perform the Git checkout file from another branch, we have three ways:

  1. By using git checkout command
  2. By using git restore command
  3. By using git show command

Let us learn about them and the command in the next section(s).

Method1: Use the Git Checkout Command

Suppose we have two dev branches namely: dev-A, and dev-B. Apart from this you also have a test.txt file in both the dev-A, and dev-B branches. Now if you want to copy or check out the test.txt file from dev-B to the branch dev-A.

Now in such scenarios, we can use the git checkout command along with the path of the file that has to be copied to the current branch. The overall command for the same is:

We can even get multiple files by specifying the names of each of the files using the above command. We can even copy an entire folder (directory) using the above command.

Finally, we can check the status of the current branch using the git status command.

What is this git checkout command? Well, for switching to the other branch, we can use the git checkout command. The Git checkout is one of the most important commands used in Git. It is used to switch between various versions of any target entity. The Git checkout command mainly works with three distinct entities i.e. files, commits, and branches. The git checkout command lets us switch between the branches.

Method2: Use Git Restore Command

Suppose we have the same branch and file example and we want to perform checking out without using the git checkout command then we can use the git switch command along with the git restore command. Both of these commands are pretty new as they were introduced in version 2.23 in 2019.

What is the working of both these commands? Well, the git restore command is used to restore the working tree and the git switch command is used to switch between branches.

Now, how can we perform: the git checkout file from another branch using the git switch command along with the git restore command?

Steps:

  1. Switch to the branch to the branch where we want to check out the desired file using the command:
  1. Name the file that we want to checkout using along with the git restore command:

In the above last step, we need to add the file name as well as the destination branch name along with the --source flag.

  1. In the last step, we only need to perform the commit and push operation.

Method3: Use Git Show Command

Suppose we have the same branch and file example and we want to perform checking out without using the git checkout command then we can use the git show command. The git show command is mainly used to get the expanded details of the various Git object.

Now, how can we perform: the git checkout file from another branch using the git show command?

Steps:

  1. Switch to the branch using git switch.
  1. Get the file to the current branch from that branch.
  1. In the last step, we only need to perform the commit and push operation.

As we can see here that we need to specify the relative path of the file from our root directory.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc.
  • In many scenarios where we want to check out some file(s) from different branches or we want to update some specific file(s) and directory(es) of our current branch with files and directories of another branch, then Git provides us several ways to do so.
  • It is quite helpful as we do not need to perform merging, we can directly checkout files. In a situation where a branch is not ready to be merged and we want to check out some files then we can use this Git feature using any of these three commands: git checkout command, git restore command, git show command.
  • We can use the git checkout command along with the path of the file that has to be copied to the current branch to get a copy of the desired file in our current branch.
  • We can even get multiple files by specifying the names of each of the files using the above command. We can even copy an entire folder (directory) using the above command.
  • We can also use the git switch command along with the git restore command to get a copy of the desired file in our current branch.
  • We can also use the git switch command along with the git show command to get a copy of the desired file in our current branch.
  • The git show command is mainly used to get the expanded details of the various Git object. The git show command is mainly used to get the expanded details of the various Git object. T