user3715648
user3715648

Reputation: 1588

How can I link an existing directory to an existing git repo

I have a folder foo, without any git integration

/foo
    file1.txt
    file2.txt

and my own repo, which already has several branches, including master

https://my-fake-repo

I would like to link foo to the master at https://my-fake-repo, such that if I then ran 'git diff', I would see the differences between /foo and https://my-fake-repo's master.

I want the equivalent of checking out https://my-fake-repo, deleting the contents, and pasting the files from /foo into it. I was hoping I could do this with just some Git commands, but i'm not familiar enough with Git and my research is failing. I attempted to do something like:

cd /foo
git init
git remote add origin https://my-fake-repo
git branch master --set-upstream-to origin/master

but master doesn't exist yet, so it doesn't work...

Upvotes: 15

Views: 18961

Answers (3)

k7h-maecus
k7h-maecus

Reputation: 1

Another way is to clone your repository in folder folderB, then copy folderB/.git into folderA.

Finally git status inside folderA should list desired changes.

Upvotes: -1

Hampden123
Hampden123

Reputation: 1258

Not sure how you got into this use case. But another way is to simply clone the remote to a new folder, /foo2, and then copy /foo over to /foo2. Then you can see the changes if any, and proceed normally to discard/commit the differences.

Upvotes: 1

janos
janos

Reputation: 124824

I want the equivalent of checking out https://my-fake-repo, deleting the contents, and pasting the files from /foo into it.

A simple way to achieve that is to pretend as if /foo is the work tree of the repository, by doing this inside the local clone of my-fake-repo:

GIT_WORK_TREE=/foo git diff

This will effectively compare the content of /foo with the content of the repository. Content that is in /foo but not in the repo will be shown as added, content that is not in /foo but exists in the repo will be shown as deleted. And so on.

This trick will work with all other Git commands as well. So be careful, because if you run commands that modify the working tree (for example checkout or reset), they will modify the content of /foo.

But I'm wondering if this is really your ultimate way of working with /foo and the Git repository. You also mentioned "linking" to the Git repository. The proper way to interact between repositories is by using remotes. You could convert /foo to a Git repository, register my-fake-repo as a remote, and then use normal Git operations between the two repos, without resulting to trickeries like at the top of this post.

cd /foo
git init
git add --all
git commit -m 'Add files'
git remote add origin https://my-fake-repo
git fetch origin
git diff origin/master

Upvotes: 16

Related Questions