How do I clone into a non-empty directory?

前端 未结 16 1299
庸人自扰
庸人自扰 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:34

    I was looking for something similar, and here's what I came up with:

    My situation is one where I have an active web tree and I was trying to create a remote repository for it without moving any of the files in the current web tree. Here's what I did:

    1. Go to the web tree and run git init
    2. Go to the intended location of the repository and run: git clone --bare /path/to/web/repo
    3. Edit the config file in my remote repo and remove the [remote "origin"] section.
    4. Add a [remote "origin"] section to .git/config in the web tree pointing to the new remote repo.
    0 讨论(0)
  • 2020-11-22 06:35

    Warning - this could potentially overwrite files.

    git init     
    git remote add origin PATH/TO/REPO     
    git fetch     
    git checkout -t origin/master -f
    

    Modified from @cmcginty's answer - without the -f it didn't work for me

    0 讨论(0)
  • 2020-11-22 06:36

    This worked for me:

    cd existing_folder
    git init
    git remote add origin path_to_your_repo.git
    git add .
    git commit
    git push -u origin master
    
    0 讨论(0)
  • 2020-11-22 06:37

    Another simple recipe seems to work well for me:

    git clone --bare $URL .git
    git config core.bare false
    

    My main use case for checking out to a directory with existing files is to control my Unix dotfiles with Git. On a new account, the home directory will already have some files in it, possibly even the ones I want to get from Git.

    0 讨论(0)
  • 2020-11-22 06:37

    Here is what I'm doing:

    git clone repo /tmp/folder
    cp -rf /tmp/folder/.git /dest/folder/
    cd /dest/folder
    git checkout -f master
    
    0 讨论(0)
  • 2020-11-22 06:41

    Maybe I misunderstood your question, but wouldn't it be simpler if you copy/move the files from A to the git repo B and add the needed ones with git add?

    UPDATE: From the git doc:

    Cloning into an existing directory is only allowed if the directory is empty.

    SOURCE: http://git-scm.com/docs/git-clone

    0 讨论(0)
提交回复
热议问题