Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to connect multiple local folders to one repository in Github?

Tags:

git

github

Question

I have two different folders with different paths and I want to link them to the same repository. For illustration purposes, the two different folders are

Folder 1: C:\Users\Tea\Folder1
Folder 2: C:\Users\Tea\Folder2

Folder 1 was originally linked to my repository but I wanted to link Folder 2 to the repository too so I can push a file from there as well.

Here's what I tried

cd C:\Users\Tea\Folder2
git init
git add .
git commit -m "initial commit from Folder 2"
git remote set-url origin --push --add http://github.com/<my username and repository> 

The last line was done according to this post: pull/push from multiple remote locations

However, I got an error message when doing

git push origin master

asking me to do a git fetch. More specifically it wrote:

! [ rejected ] master -> master(fetch first)
error: failed to push some refs to 'https://github.com/...'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull...') before pushing again

So I did git pull origin master and got these messages instead

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I tried merging but got

fatal: refusing to merge unrelated histories

so I decided to do

git push -f origin <branch>

according to Cannot push to GitHub - keeps saying need merge without knowing exactly what "losing all commits" meant (I thought it just meant that the current commit messages would be lost).

Turns out this was a bad decision on my part since the new folder I linked to the git repository just replaced my old git repository entirely (i.e. all the old files are now gone).

tl;dr: How do I link two separate folders to one Github repository? Thanks in advance!

like image 803
IceTea Avatar asked Jul 16 '18 10:07

IceTea


People also ask

Can GitHub repository have folders?

Creating code repositories is a cinch but common methods of organization, like how to create a folder in Github, aren't intuitive. The process is super easy but it takes some getting used to compared to typical OS-approaches. Essentially, it's a matter of typing in a forward slash to indicate folder structure.

Can I have multiple local Git repositories?

However, git is great for working with several repositories. These additional repositories can be stored locally, or accessed via network connection.


Video Answer


2 Answers

I have two different folders with different paths and I want to link them to the same repository. For illustration purposes ...So is there a way I can link two separate folders to one Github repository, preferably without having to merge them?

Tl;Dr

No, GIT doesn't work like this. You can link one to a single remote, you can't link multiple local repos to one remote.


git init
git add NewFolder
git commit -m "web automation basics"
git remote set-url origin --push --add http://github.com/<my username and repository>
  • git init creates a new repo
  • You then add some stuff
  • and link it to a remote

git push origin master However, I got an error message...asking me to do a git pull

You're then tring to push this to your remote, but this remote was already linked to another repo, so GIT thought your local had conflicts because they weren't the same.

So I did git pull origin master and got these messages instead

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So you now tried a pull and GIT rejected it because the two repos we're totally different. You tried to merge but this (obviously) failed because they have nothing in common.

so I decided to do

git push -f origin <branch>

At this point you essentially overwrote your remote with your local copy (-f is the force flag, you forced your change onto the repo, ignoring conflicts, etc.). so there will be a commit, deleting everything that was in the repo and adding everything from your second repo.

like image 156
Liam Avatar answered Oct 16 '22 08:10

Liam


YES, POSSIBLE !

I use Symlink in many of my project. It's a nice "trick".

  • Create/checkout main repository folder anywhere.
  • Then create hard symlinks of .../any_path/Folder1 and .../another-path/Folder2 into it.

That's all. In this way, you can have different path/folders combined in your local repo (Remember, they will be mirrors of origin folders).

like image 6
T.Todua Avatar answered Oct 16 '22 08:10

T.Todua