Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZeroDragon/6707408 to your computer and use it in GitHub Desktop.
Save ZeroDragon/6707408 to your computer and use it in GitHub Desktop.
How to clone a git repo to an existing folder (not empty)
  1. First get to the existing directory
    $ cd my/folder/

  2. Now start a new git repository
    $ git init

  3. Identify if the current elements on the directory are needed or not and add them to the .gitignore file. When ready...
    $ vim .gitignore

  4. When ready create the first commit on the server
    $ git add .;git commit -m'my first commit'

  5. Now add the remote from where you want to clone
    $ git remote add origin https|ssh:path/to/the/repository.git

  6. Now just pull and merge with local git
    $ git pull origin master

  7. If you have some merge conflicts resolve them and commit your changes.

You are ready to go!

UPDATE

If your remote repository is NOT empty, you must do what Sochi pointed out here

@kokhoor
Copy link

kokhoor commented Sep 25, 2021

Great simple steps. Thanks!

@hrishikeshps
Copy link

Many Thanks @sochi ..

@SimoneGianni
Copy link

I made sure I didnt' have any file in my existing folder that would collide with the stuff already on the git repo, then I did:

cd existingFolder
git clone https://whatever.com/bla/bla tempfolder
mv tempfolder/* .
mv tempfolder/.* . 
rm -fR tempfolder

Which means:

  1. Clone in a temporary folder
  2. Move all the files to the existing folder (you can resolve conflicts yourself here .. don't copy files that would overwrite stuff, or rename them etc..)
  3. Make sure you move also the .git folder (and other .* folders)
  4. Delete the (now empty) temporary folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment