Search code examples
gitbranchcheckout

What exactly git checkout folder does?


When I git checkout branch -- app/ expect overwrite all my local files in app folder by the content of app folder from another branch. But in reality it overwrite same named files, but don't delete files that exists only in my app folder. And the resulting folder is not the same as in target branch. Why does it work this way and how to checkout whole folder without leaving old files?


Solution

  • When used with a tree-ish and a path, git-checkout checks out matching files in the path from the tree.

    It does not touch other files. If you want to copy that folder exactly from another branch, you probably have to

    git rm path
    git commit
    git checkout branch -- path
    

    But why do you need to do this anyway?

    Alternately, I suppose you could clone the repository in another directory nearby, and then just

    git -C newrepo checkout branch
    mv newrepo/path oldrepo/path
    rm -r newrepo