What's the best practice to “git clone” into an existing folder?

后端 未结 15 1493
情书的邮戳
情书的邮戳 2020-11-28 17:19

I have a working copy of the project, without any source control meta data. Now, I\'d like to do the equivalent of git-clone into this folder, and keep my local changes.

相关标签:
15条回答
  • 2020-11-28 17:31
    git clone your_repo tmp && mv tmp/.git . && rm -rf tmp && git reset --mixed
    
    0 讨论(0)
  • 2020-11-28 17:33

    I'd git clone to a new directory and copy the content of the existing directory to the new clone.

    0 讨论(0)
  • 2020-11-28 17:35

    Just use the . at the end of the git clone command (being in that directory), like this:

    cd your_dir_to_clone_in/
    git clone git@github.com/somerepo/ .
    
    0 讨论(0)
  • 2020-11-28 17:37

    The following i did, to checkout master branch in an existing directory:

    git init
    git remote add origin [my-repo]
    git fetch
    git checkout origin/master -ft
    
    0 讨论(0)
  • 2020-11-28 17:39

    If you are using at least git 1.7.7 (which taught clone the --config option), to turn the current directory into a working copy:

    git clone example.com/my.git ./.git --mirror --config core.bare=false
    

    This works by:

    • Cloning the repository into a new .git folder
    • --mirror makes the new clone into a purely metadata folder as .git needs to be
    • --config core.bare=false countermands the implicit bare=true of the --mirror option, thereby allowing the repository to have an associated working directory and act like a normal clone

    This obviously won't work if a .git metadata directory already exists in the directory you wish to turn into a working copy.

    0 讨论(0)
  • 2020-11-28 17:42

    Lots of answers already to do it the way that the OP asked. But it worth noting that doing it the opposite way around is far simpler:

    git clone repo-url tmp/
    cp -R working/ tmp/
    

    You now have the desired target state - fresh clone + local-changes.

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