Copy files from a Git branch


If you’re using Git, there may be a time where you need to copy a file from another branch.

How would you go about doing that without cherry picking?

Easy. You can use checkout and let me show you how with an example.

Given the following:

git status
On branch master
nothing to commit, working directory clean

Create and commit a file on another branch:

git checkout -b other_branch
echo hello > world.txt
git add world.txt
git commit -m "Create world"

Switch back to the master branch:

git checkout master

Now to copy the file, we checkout the file from the other branch:

git checkout other_branch world.txt # git checkout <branch> <path>

Now your file has been staged to your current branch:

git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

      new file:   world.txt

You can do this for single files or whole directories.



Please support this site and join our Discord!