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

后端 未结 15 1495
情书的邮戳
情书的邮戳 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:44

    You can do it by typing the following command lines recursively:

    mkdir temp_dir   //  Create new temporary dicetory named temp_dir
    git clone https://www...........git temp_dir // Clone your git repo inside it
    mv temp_dir/* existing_dir // Move the recently cloned repo content from the temp_dir to your existing_dir
    rm -rf temp_dir // Remove the created temporary directory
    
    0 讨论(0)
  • 2020-11-28 17:49

    This is the Best of all methods i came across

    Clone just the repository's .git folder (excluding files as they are already in existing-dir) into an empty temporary directory

    1. git clone --no-checkout repo-path-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.

    1. mv existing-dir/existing-dir.tmp/.git existing-dir/

    Delete the temporary directory

    1. rmdir existing-dir/existing-dir.tmp

    2. 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.

    1. git reset --mixed HEAD
    0 讨论(0)
  • 2020-11-28 17:50

    To clone a git repo into an empty existing directory do the following:

    cd myfolder
    git clone https://myrepo.com/git.git . 
    

    Notice the . at the end of your git clone command. That will download the repo into the current working directory.

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

    if you are cloning the same repository, then run the following snippet through the existing repository

    git pull origin master
    
    0 讨论(0)
  • 2020-11-28 17:51

    Usually I will clone the initial repository first, and then move everything in the existing folder to the initial repository. It works every time.

    The advantage of this method is that you won't missing anything of the initial repository including README or .gitignore.

    You can also use the command below to finish the steps:

    $ git clone https://github.com/your_repo.git && mv existing_folder/* your_repo
    
    0 讨论(0)
  • 2020-11-28 17:52

    Using a temp directory is fine, but this will work if you want to avoid that step. From the root of your working directory:

    $ rm -fr .git
    $ git init
    $ git remote add origin your-git-url
    $ git fetch
    $ git reset --mixed origin/master
    
    0 讨论(0)
提交回复
热议问题