Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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...

like image 980
user3715648 Avatar asked Aug 08 '17 20:08

user3715648


People also ask

How do I upload a folder to an existing repository?

Drag and drop the file or folder you'd like to upload to your repository onto the file tree.


1 Answers

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
like image 88
janos Avatar answered Oct 04 '22 19:10

janos