gitdirectory

How to duplicate directory in git, tracking new files?


Is there a simple way to create a copy of a folder in a local repository, keeping track of exactly the same files (do not care about commit history, or untracked files)?

The folder contains a hierarchy of both tracked and untracked (but not ignored) files and directories. I basically want to recursively replicate the tracked structure. Is there a way other than copying everything, and then manually adding only the relevant files (there are a lot of them)?


Solution

  • The solution is to git clone the directory. Need to then take care of the remote origin url (which otherwise is set to the clone source). For example:

    # Clone the repository
    git clone <existing directory> <new directory>
    
    cd <new directory>
    
    # Set origin url to the same one as old repository
    git remote set-url origin `git -C <existing directory> config --get remote.origin.url`
    

    Another, slower option is to just clone from a remote repository (github / bitbucket?) which is @Sergey_S 's solution - which means downloading everything again.