Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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

@yedincisenol
Copy link

Thanks:+1:

@sochi
Copy link

sochi commented Apr 5, 2019

You might want to add --allow-unrelated-histories flag in case the repo added as remote is not empty.

git pull origin master --allow-unrelated-histories

This might be also needed if your repo is created with some preset files, e.g., README.md or .gitignore as first commit (GitHub now offers to do so when creating a repo). Details in https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---allow-unrelated-histories

And perhaps include an optional step to push local changes to the remote:

git push -u origin master

Edit: forgot the most important part.. thanks for the list in a first place! :-)

@amarathota
Copy link

git pull origin master --allow-unrelated-histories

Thank you!

@josephmtinangi
Copy link

Thanks

@adejaremola
Copy link

thanks

@zcarlos
Copy link

zcarlos commented Feb 16, 2020

Thanks

@hejtmii
Copy link

hejtmii commented Apr 1, 2020

Thanks for the initial guide.

I just modified it a bit and was able to do it even without the initial commit like this (while the target repo has its own .gitignore file):

git init
echo * > .gitignore
git remote add origin https://github.com/Kentico/ems-module-kontent-publishing.git
git fetch
git checkout origin/master -b master

Do you see any drawbacks in it?

@ervija123
Copy link

How to clone a git repo to an existing folder (empty)

git init

git remote add origin [your_origin]

git fetch

git checkout origin/master -b master

@HaidarZ
Copy link

HaidarZ commented Apr 9, 2021

Thanks for the initial guide.

I just modified it a bit and was able to do it even without the initial commit like this (while the target repo has its own .gitignore file):

git init
echo * > .gitignore
git remote add origin https://github.com/Kentico/ems-module-kontent-publishing.git
git fetch
git checkout origin/master -b master

Do you see any drawbacks in it?

It works perfectly !

@LaBlatta
Copy link

LaBlatta commented Aug 8, 2021

Thanks! Also this:

git checkout origin/master -b master

Is exactly what I was looking for!

@kokhoor
Copy link

kokhoor commented Sep 25, 2021

Great simple steps. Thanks!

@hrishikeshps
Copy link

Many Thanks @sochi ..

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