Git: What's the best practice to "git clone" into an existing folder?

Git

Git Problem Overview


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.

git-clone doesn't allow me to clone into an existing folder. What is the best practice here?

Git Solutions


Solution 1 - Git

This can be done by cloning to a new directory, then moving the .git directory into your existing directory.

If your existing directory is named "code".

git clone https://myrepo.com/git.git temp
mv temp/.git code/.git
rm -rf temp

This can also be done without doing a checkout during the clone command; more information can be found here.

Solution 2 - Git

Don't clone, fetch instead. In the repo:

git init
git remote add origin $url_of_clone_source
git fetch origin
git checkout -b master --track origin/master # origin/master is clone's default

Then you can reset the tree to get the commit you want:

git reset origin/master # or whatever commit you think is proper...

and you are like you cloned.

The interesting question here (and the one without answer): How to find out which commit your naked tree was based on, hence to which position to reset to.

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

git clone your_repo tmp && mv tmp/.git . && rm -rf tmp && git reset --mixed

Solution 7 - Git

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.

Solution 8 - Git

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.

Solution 9 - Git

git init
git remote add origin git@github.com:<user>/<repo>.git
git remote -v
git pull origin master

Solution 10 - Git

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

Solution 11 - Git

There are two approaches to this. Where possible I would start with a clean folder for your new git working directory and then copy your version of things in later. This might look something like*:

mv $dir $dir.orig
git clone $url $dir
rsync -av --delete --exclude '.git' $dir.orig/ $dir/
rm -rf $dir.orig

At this point you should have a pretty clean working copy with your previous working folder as the current working directory so any changes include file deletions will show up on the radar if you run git status.

On the other hand if you really must do it the other way around, you can get the same result with something like this:

cd $dir
git clone --no-checkout $url tempdir
mv tempdir/.git .
rmdir tempdir
git reset --mixed HEAD

Either way, the first thing I would do is run something like git stash to get a copy of all your local changes set aside, then you can re-apply them and work through which ones you want to get committed.

* Both examples assume you start out on the shell in the parent directory of your project.

Solution 12 - Git

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

Solution 13 - Git

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

Solution 14 - Git

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.

Solution 15 - Git

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 [email protected]/somerepo/ .

Solution 16 - Git

For reference, from the Gitlab Commandline instruction:

Push an existing folder

cd existing_folder
git init
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master

Or Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin <url>
git push -u origin --all
git push -u origin --tags

Solution 17 - Git

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

git pull origin master

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - GitamicitasView Answer on Stackoverflow
Solution 2 - GitAndreas KreyView Answer on Stackoverflow
Solution 3 - GitalexwenzelView Answer on Stackoverflow
Solution 4 - Gituser1055643View Answer on Stackoverflow
Solution 5 - GitjhwistView Answer on Stackoverflow
Solution 6 - Gitreturn1.atView Answer on Stackoverflow
Solution 7 - GitokTalkView Answer on Stackoverflow
Solution 8 - GitAndrewView Answer on Stackoverflow
Solution 9 - GitnwilloView Answer on Stackoverflow
Solution 10 - GitAmirtha RajanView Answer on Stackoverflow
Solution 11 - GitCalebView Answer on Stackoverflow
Solution 12 - GitMike ChenView Answer on Stackoverflow
Solution 13 - GitMustapha GHLISSIView Answer on Stackoverflow
Solution 14 - GitThorSummonerView Answer on Stackoverflow
Solution 15 - GitJohn FView Answer on Stackoverflow
Solution 16 - GitDocuemadaView Answer on Stackoverflow
Solution 17 - GitMohsin MahmoodView Answer on Stackoverflow