7

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!

IceTea
  • 598
  • 1
  • 6
  • 19
  • Why would you want two unmerged directories connected to the same repo? Should they be on different *branches*? – jonrsharpe Jul 16 '18 at 10:15
  • @jonrsharpe because on my local environment they are under different projects despite being about the same topic. I'm quite new to Github, so I'm not too sure why I would need two different branches for that. After all won't the branches merge into the master eventually? – IceTea Jul 16 '18 at 10:17
  • It's totally unclear to me what you're trying to do or why you think separate directories is a good idea. As a rule of thumb, the earlier you merge the better; it only gets harder with time. *"won't the branches merge into the master eventually?"* - I don't know, this is your system and you've not told us enough about it. In some cases, e.g. using a `gh-pages` branch to deploy a site to GitHub pages, that branch will always be separate. – jonrsharpe Jul 16 '18 at 10:18
  • @jonrsharpe I'm really sorry if this post is confusing, but as someone who's new to Github, I don't get why I can't just link multiple directories to one repository. These are two completely different folders on my computer with very different contents, so I wouldn't want to merge them. Plus, I've written above that the merge has failed because of "unrelated histories". If it turns out that the best solution is to just create a new directory solely for this github repository, I'll be fine with that answer too. – IceTea Jul 16 '18 at 10:21
  • Your git repo is telling your your behind, `Updates were rejected because the tip of your current branch is behind` i.e. the repo exists. Yet your calling `git init` to create a new repo. This doesn't make any sense. Either your repo exists, in which case you clone it or it doesn't and you init it. You can't do both – Liam Jul 16 '18 at 10:31
  • It seems your trying to store two local repos in one remote repo and it keeps overwritting. This isn't how GIT works. Local repos are exact copies of remote repos. You can't store 2 locals in one remote, you need 2 remote repos. `git push -f origin` is a good way totally kill a remote repo... I think you need to read and understand the [GIT docs](https://git-scm.com/doc) because right now your totally misunderstanding some core concepts. – Liam Jul 16 '18 at 10:33
  • If they're two different folders with very different contents and you don't want to merge them **why do you want them to be the same repo?!** Maybe just... don't do that? – jonrsharpe Jul 16 '18 at 10:39
  • @jonrsharpe well if it makes things clearer I'm trying to run automated tests for two projects. So the test scripts will be in their respective project folders instead of being in the same folder. However, I want to keep track of all these test scripts I'm making, so that's why I wanted to push them to the same repository... – IceTea Jul 16 '18 at 11:10

3 Answers3

8

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.

Liam
  • 27,717
  • 28
  • 128
  • 190
8

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

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • I found this reference to Windows SymLinks useful: howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/ – Scott Simpson Mar 12 '20 at 17:04
-1

Try this: Use your C:\Users\Tea folder as repo and then .gitignore all subfolders that are not relevant for your project.