Reputation: 18957
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
Upvotes: 729
Views: 487169
Reputation: 2126
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
git branch --set-upstream-to=origin/master master
to start working on the master branch straight away.
Upvotes: 143
Reputation: 1583
The following example is using `main' as branch name (the default branch).
cd LOCAL/FOLDER
rm -rf .git
git init -b main
git remote add origin CLONE/SSH/URL
git pull origin main
Now you can commit your existing files in the dir.
git add .
git commit -m anyMessage
git push --set-upstream origin main
From next time you can use git push
without the --set-upstream origin main
.
Upvotes: 0
Reputation: 391
This approach always works for me:
cd [folder name]
git init //initializing the git
git remote add origin [repository clone address]
git pull origin [branch-name] //pulling the desired branch
Upvotes: 0
Reputation: 15438
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.
If you already have a cloned local repo in B,
cp -r $DirB/{*,.*} $DirA/
That's a --recursive
copy of B's contents into A.
You need the {*,.*}
so it gets the hidden crap like .git/
and .gitignore
, but be aware that this will overwrite any files they have in common. If you always want any version in A over any version in B, add -n
(--noclobber
); or use -i
(--interactive
) to cherry pick which overwrites to allow or prevent.
Maybe git pull
in B first to make sure it's up to date, but otherwise, this should handle it in one step for simple files and directories.
Upvotes: 0
Reputation: 9315
Open the target directory and try this:
git init
git remote add origin <remote repo>
git pull origin main --allow-unrelated-histories
Upvotes: -1
Reputation: 107
Delete the original cloned directory or make it so it is not named the same. Then you can clone it. It worked for me.
Upvotes: 0
Reputation: 10887
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
Upvotes: 7
Reputation: 117166
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.
Upvotes: 883
Reputation: 24015
Another simple recipe seems to work well for me:
git clone --bare $URL .git
git config --unset core.bare
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.
Upvotes: 23
Reputation: 73
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:
git init
to track origin/masterfetch
)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:
git checkout -b <localbranchname>
to track <remote>/<remotebranchname>
fetch
)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.
Upvotes: 3
Reputation: 13913
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
Upvotes: 0
Reputation: 191
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á!
Upvotes: 19
Reputation: 423
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.
Upvotes: 1
Reputation: 741
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
Upvotes: 52
Reputation: 3797
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.
Upvotes: 8
Reputation: 2852
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.
Upvotes: 1
Reputation: 22899
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
Upvotes: 5
Reputation: 373
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
Upvotes: 4
Reputation: 6136
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
Upvotes: 8
Reputation: 313
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.
Upvotes: 28
Reputation: 18957
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
Upvotes: 188
Reputation: 917
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:
git init
git clone --bare /path/to/web/repo
[remote "origin"]
section.[remote "origin"]
section to .git/config in the web tree pointing to the new remote repo.Upvotes: 3
Reputation: 31015
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
Upvotes: 5