How do I clone into a non-empty directory?

前端 未结 16 1613
庸人自扰
庸人自扰 2020-11-22 06:20

I have directory A with files matching directory B. Directory A may have other needed files. Directory B is a git repo.

I want to clone directory B to directory A bu

16条回答
  •  半阙折子戏
    2020-11-22 06:46

    In the following shell commands existing-dir is a directory whose contents match the tracked files in the repo-to-clone git repository.

    # Clone just the repository's .git folder (excluding files as they are already in
    # `existing-dir`) into an empty temporary directory
    git clone --no-checkout repo-to-clone existing-dir/existing-dir.tmp # might want --no-hardlinks for cloning local repo
    
    # Move the .git folder to the directory with the files.
    # This makes `existing-dir` a git repo.
    mv existing-dir/existing-dir.tmp/.git existing-dir/
    
    # Delete the temporary directory
    rmdir existing-dir/existing-dir.tmp
    cd existing-dir
    
    # git thinks all files are deleted, this reverts the state of the repo to HEAD.
    # WARNING: any local changes to the files will be lost.
    git reset --hard HEAD
    

提交回复
热议问题