Git: Copy a file from one branch to another

git

Let say you are on the master branch and you want to do some devs without messing up your master. What one usally do is create a new branch dev based on the main branch (in this case master) and do all the developments there.

Sometimes you may want to copy just one or two files from this dev branch without merging the whole branch with master.

The easiest way to do so is the following (assuming you are in another branch than dev):

git checkout dev -- path/to/your/file

Note that you can also get a whole folder, all the files inside this folder will be copied:

git checkout dev -- path/to/your/folder

For more informations on checkout, take a look at the man page git checkout

2 thoughts on “Git: Copy a file from one branch to another

  1. Normally you don’t need the `–` there, and you can do simply `git checkout branchname path/to/file`. You only need the `–` before the path in special cases, for example: 1. when `path/to/file` starts with a “-“, or 2. when you don’t specify a branch (to use the current branch), and you have a branch named “path/to/file”

Leave a Reply