Search code examples
gitworkflowcheckout

checkout files from a branch into working branch where this files don't exist


I have a branch (feature-4) where i want to add all files in a subfolder (src/peripherals/dma) which are committed in another branch (feature-5).

The checked out branch is feature-4 and does not contain the folder src/peripherals/dma (src/peripherals does exist)

A colleague of mine did push the code which is used for dma into his feature-5 branch.

As stated in the manuel from git checkout and in this link (jasonrudolph.com) git checkout should do this, but neither giving the path of a file nor giving the path to the folder did work.

$ git branch
* feature-4
  feature-5
  master

$ git checkout feature-5 src/peripherals/dma/dma_config.c
error: pathspec 'src/peripherals/dma/dma_config.c' did not match any file(s) known to git

daimonion@ZBOOK-004 MINGW64 ~/K4G1/src/peripherals/dma (feature-4)
$ ls -al
total 4
drwxr-xr-x 1 daimonion 1049089 0 Sep  4 09:13 ./
drwxr-xr-x 1 daimonion 1049089 0 Sep  4 09:13 ../
-rw-r--r-- 1 daimonion 1049089 0 Sep  4 09:13 dma_config.c

I thought git has a problem because the file does not exist, but even if i commit an empty dma_config.c to the branch feature-4 it gives the same error.

How is it possible with git to "merge" some files from one branch to a branch where they don't exist?

Thanks in advance

Regards daimonion


Solution

  • This souldn't be a problem, you can very well checkout a file from another branch even if it doesn't exist on the current branch.

    One possible cause could be that your local version of feature-5 is not up-to-date (and thus doesn't have the commit bringing the wanted file yet), be sure to

    git checkout feature-5
    git pull
    

    before retrying your command.


    Also, pathspecs are supposed to be preceded by --, so rather

    git checkout feature-5 -- src/peripherals/dma/dma_config.c