How do I clone into a non-empty directory?

Git

Git Problem Overview


I have directory A with files matching directory B. Directory A may have other needed files. Directory B is a git repo.

I want to clone directory B to directory A but git-clone won't allow me to since the directory is non-empty.

I was hoping it would just clone .git and since all the files match I could go from there?

I can't clone into an empty directory because I have files in directory A that are not in directory B and I want to keep them.

Copying .git is not an option since I want refs to push/pull with and I don't want to set them up manually.

Is there any way to do this?

Update: I think this works, can anyone see any problems? -->

cd a
git clone --no-hardlinks --no-checkout ../b a.tmp 
mv a.tmp/.git .
rm -rf a.tmp
git unstage # apparently git thinks all the files are deleted if you don't do this

Git Solutions


Solution 1 - Git

This worked for me:

git init
git remote add origin PATH/TO/REPO
git fetch
git reset origin/master  # Required when the versioned files existed in path before "git init" of this repo.
git checkout -t origin/master

NOTE: -t will set the upstream branch for you, if that is what you want, and it usually is.

Solution 2 - Git

In the following shell commands existing-dir is a directory whose contents match the tracked files in the repo-to-clone git repository.

# Clone just the repository's .git folder (excluding files as they are already in
# `existing-dir`) into an empty temporary directory
git clone --no-checkout repo-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.
mv existing-dir/existing-dir.tmp/.git existing-dir/

# Delete the temporary directory
rmdir existing-dir/existing-dir.tmp
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.
git reset --hard HEAD

Solution 3 - Git

A slight modification to one of the answers that worked for me:

git init
git remote add origin PATH/TO/REPO
git pull origin master

to start working on the master branch straight away.

Solution 4 - Git

Warning - this could potentially overwrite files.

git init     
git remote add origin PATH/TO/REPO     
git fetch     
git checkout -t origin/master -f

Modified from @cmcginty's answer - without the -f it didn't work for me

Solution 5 - Git

Here's what I ended up doing when I had the same problem (at least I think it's the same problem). I went into directory A and ran git init.

Since I didn't want the files in directory A to be followed by git, I edited .gitignore and added the existing files to it. After this I ran git remote add origin '<url>' && git pull origin master et voíla, B is "cloned" into A without a single hiccup.

Solution 6 - Git

I have used this a few moments ago, requires the least potentially destructive commands:

cd existing-dir
git clone --bare repo-to-clone .git
git config --unset core.bare
git remote rm origin
git remote add origin repo-to-clone
git reset

And voilá!

Solution 7 - Git

Another simple recipe seems to work well for me:

git clone --bare $URL .git
git config core.bare false

My main use case for checking out to a directory with existing files is to control my Unix dotfiles with Git. On a new account, the home directory will already have some files in it, possibly even the ones I want to get from Git.

Solution 8 - Git

This worked for me:

cd existing_folder
git init
git remote add origin path_to_your_repo.git
git add .
git commit
git push -u origin master

Solution 9 - Git

I had a similar problem with a new Apache web directory (account created with WHM) that I planned to use as a staging web server. I needed to initially clone my new project with the code base there and periodically deploy changes by pulling from repository.

The problem was that the account already contained web server files like:

.bash_history
.bash_logout
.bash_profile
.bashrc
.contactemail
.cpanel/
...

...that I did not want to either delete or commit to my repository. I needed them to just stay there unstaged and untracked.

What I did:

I went to my web folder (existing_folder):

cd /home/existing_folder

and then:

git init
git remote add origin PATH/TO/REPO
git pull origin master
git status

It displayed (as expected) a list of many not staged files - those that already existed initially from my cPanel web account.

Then, thanks to this article, I just added the list of those files to:

**.git/info/exclude**

This file, almost like the .gitignore file, allows you to ignore files from being staged. After this I had nothing to commit in the .git/ directory - it works like a personal .gitignore that no one else can see.

Now checking git status returns:

On branch master
nothing to commit, working tree clean

Now I can deploy changes to this web server by simply pulling from my git repository. Hope this helps some web developers to easily create a staging server.

Solution 10 - Git

Maybe I misunderstood your question, but wouldn't it be simpler if you copy/move the files from A to the git repo B and add the needed ones with git add?

UPDATE: From the git doc:

> Cloning into an existing directory is only allowed if the directory is empty.

SOURCE: http://git-scm.com/docs/git-clone

Solution 11 - Git

Here is what I'm doing:

git clone repo /tmp/folder
cp -rf /tmp/folder/.git /dest/folder/
cd /dest/folder
git checkout -f master

Solution 12 - Git

The following worked for me. First I'd make sure the files in the a directory are source-controlled:

$ cd a
$ git init
$ git add .
$ git commit -m "..."

Then

$ git remote add origin https://URL/TO/REPO
$ git pull origin master --allow-unrelated-histories
$ git push origin master

Solution 13 - Git

this is work for me ,but you should merge remote repository files to the local files:

git init
git remote add origin url-to-git
git branch --set-upstream-to=origin/master master
git fetch
git status

Solution 14 - Git

I was looking for something similar, and here's what I came up with:

My situation is one where I have an active web tree and I was trying to create a remote repository for it without moving any of the files in the current web tree. Here's what I did:

  1. Go to the web tree and run git init
  2. Go to the intended location of the repository and run: git clone --bare /path/to/web/repo
  3. Edit the config file in my remote repo and remove the [remote "origin"] section.
  4. Add a [remote "origin"] section to .git/config in the web tree pointing to the new remote repo.

Solution 15 - Git

I liked Dale's answer, and I also added

git clone --depth 2 --no-checkout repo-to-clone existing-dir/existing-dir.tmp
git branch dev_new214
git checkout dev_new214
git add .
git commit
git checkout dev
git merge dev_new214

The shallow depth avoided a lot of extra early dev commits. The new branch gave us a good visual history that there was some new code from this server that was placed in. That is the perfect use branches in my opinion. My thanks to the great insight of all the people who posted here.

Solution 16 - Git

I got the same issues when trying to clone to c/code

But this folder contains a whole bunch of projects.

I created a new folder in c/code/newproject and mapped my clone to this folder.

git for desktop then asked of my user and then cloned fine.

Solution 17 - Git

I was looking for a different question when I stumbled on this: How to clone into an existent directory where the files weren't there?

At any rate, i did just this recently for some non-source-controlled copies of files on a couple of servers.

cd <target directory>
git init
git remote add origin <repository URI>
git fetch
git branch -f master origin/master
git reset
git show HEAD:.gitignore > .gitignore

This:

  • Initializes an empty repo directory for you
  • Adds a remote for the repository you intend to connect to
  • Retrieves the repository folder contents (/.git)
  • Forces the default branch from git init to track origin/master
  • Unstages all changes created by the fetch (I don't precisely understand the mechanism for staging all the files on fetch)
  • Copies in the repository's .gitignore file (which you'll want if you're going to use it)

For my question, the answer was the git reset --hard HEAD in Dale Forester's answer.

Alternative instructions for arbitrary branches and remote names

git init
git remote add <remotename> <repository URI>
git checkout -b <localbranchname>
git fetch <remotename> <remotebranchname>
git branch -f <localbranchname> <remotename>/<remotebranchname>
git reset
git show HEAD:.gitignore > .gitignore

As above, this does:

  • Initializes an empty repo directory for you
  • Adds a remote for the repository you intend to connect to
  • Sets the local branch name. Optional, but recommended to keep branches straight if you're doing more than one branch on the same environment
  • Retrieves the repository folder contents (/.git)
  • Forces the local branch from git checkout -b <localbranchname> to track <remote>/<remotebranchname>
  • Unstages all changes created by the fetch (I don't precisely understand the mechanism for staging all the files on fetch)
  • Copies in the repository's .gitignore file (which you'll want if you're going to use it)

If you do a git status at the end of this, you'll see unstaged changes for any files in the current directory (the working directory) whether the working directory has anything to do with the repository structure or not.

Solution 18 - Git

This question has a lot of answers, and I don't think any of them cover the option of initializing the A as a Git repo and then pulling from B to A.

# Turn `A` into a Git repo and check in files
git -C A init
git -C A add --all
git -C A commit -m "Add existing files"

# Set `B` as a remote
git -C A remote add local-B file:///B

# Fetch & merge B 'master' to A 'master'
git -C A pull --allow-unrelated local-B 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
QuestionDale ForesterView Question on Stackoverflow
Solution 1 - GitcmcgintyView Answer on Stackoverflow
Solution 2 - GitDale ForesterView Answer on Stackoverflow
Solution 3 - GitmohsaiedView Answer on Stackoverflow
Solution 4 - GitJohnFFView Answer on Stackoverflow
Solution 5 - GitBjornSnoenView Answer on Stackoverflow
Solution 6 - GitKuttKatreaView Answer on Stackoverflow
Solution 7 - GitKen WilliamsView Answer on Stackoverflow
Solution 8 - GitMike6679View Answer on Stackoverflow
Solution 9 - GitVladoView Answer on Stackoverflow
Solution 10 - GitRoberto AloiView Answer on Stackoverflow
Solution 11 - GitPhilip KirkbrideView Answer on Stackoverflow
Solution 12 - GitKevin Le - KhnleView Answer on Stackoverflow
Solution 13 - GitRODNEY ZHANGView Answer on Stackoverflow
Solution 14 - GitLendrickView Answer on Stackoverflow
Solution 15 - GitDmitri R117View Answer on Stackoverflow
Solution 16 - GitTomView Answer on Stackoverflow
Solution 17 - GitChristopher EberleView Answer on Stackoverflow
Solution 18 - GitshadowtalkerView Answer on Stackoverflow