1

Folder structure

----WorkingFolder

----------|-----.gitone

----------|-----.gittwo

----------|-----other project files

Short: SourceTree doesn't regconize Git repository with custom .git folder name. My Git repository, I've renamed .git to .gitone. With the help of export GIT_DIR=.gitone and GIT_WORK_TREE constant, Git CLI can recognise my WorkingFolder and work fine. However, SourceTree doesn't regconise this when I add WorkingFolder, saying This is not a valid working copy path.

Related:

  1. Two git repositories in one directory?
  2. https://github.com/rrrene/gitscm-next/blob/master/app/views/blog/progit/2010-04-11-environment.markdown

Longer:

WorkingFolder is primary working place, it has 2 remote repositories. But there's the thing: these 2 repos have the same log due to same .git folder. I want them to be discrete and have different logs. That's why I created 2 folder named .gitone and .gittwo using 2 commands mv .git .gitone and mv .git .gittwo. The following code is what I've done so far:

Git CLI root folder: WorkingFolder

export GIT_DIR=.gitone
export GIT_WORK_TREE=<path_to_WorkingFolder>

Why on Earth does this nerd add two repo in a same directory?/Why don't you just create another branch?

I have a repo for working with my team. At the same time, I also want to add a small part of existing code to another repository for personal future reuse. So adding a "personal" branch is not in option list.

I understand I can create another folder, then with every change, I copy changed files and just paste them to another folder. I feel like this is not in very "Git" style and not a proper way. So, how could I add these repo to SourceTree? Or do you have any idea on how to achieve this goal?

Any suggestion is appreciated.

Edit: Haven't figure it out. I managed to get both repos worked for my purpose. For primary development, I just let .gitone folder be with Git ordinal name (.git), SourceTree will work with this repo. For the other purpose (.gittwo), I have to manually control via Git CLI. The question is still open!

Edit: Git-gui is capable of opening log folder that it's assigned to by using git --git-dir=.gittwo gui. However I still prefer SourceTree

How can I replicate your problem?

Clone any Git repo, type mv .git .gitanyname. Try to open the repo with SourceTree

Huy Phạm
  • 888
  • 9
  • 24
  • 2
    Prediction: this is going to result in a lot of confusion. Every time you switch branch in one repo, the other repo will think all the files have changed. You won't inherit any meaningful history from the primary repo into the secondary one, and will have to remember to take snapshots of the files you want at key moments. Just write a script that copies the relevant files to a separate directory, and manage that directory as a normal git repository. – IMSoP Jul 27 '21 at 20:10

1 Answers1

1

I have a repo for working with my team. At the same time, I also want to add a small part of existing code to another repository for personal future reuse.

I would:

  • create the second personal repository folder outside of WorkingFolder
  • use the first WorkingFolder/.git whenever I want to add files from the first repository

That is:

cd /path/to/WorkingFolder
cd ..
git init WorkingFolder2
cd WorkingFolder2
git --work-tree=../WorkingFolder add -- afile-from-WorkingFolder
git status
git commit -m "Import afile-from-WorkingFolder"

That way, SourceTree can still open/visualize both repositories without issue.

If I try with --git-dir=../WorkingFolder/.git, it that means WorkingFolder2 is a duplication of WorkingFolder in term of physical storage on my PC?

Actually, I meant using --work-tree, not --git-dir: you keep your index from WorkingFolder2, but import files from WorkingFolder.
And that, only for the git add command, whenever you need to import/update files from WorkingFolder to WorkingFolder2.

I proposed in the discussion, for Windows 10 (used by the OP):

set GIT_WORK_TREE=C:\path\to\WorkingFolder
cd C:\parth\to\WorkingFolder2
%LOCALAPPDATA%\SourceTree\SourceTree.exe
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `--git-dir` only accepts `log folder` that is created by Git and `--git-dir=../WorkingFolder` is an invalid flag. If I try with `--git-dir=../WorkingFolder/.git`, it that means `WorkingFolder2` is a duplication of `WorkingFolder` in term of physical storage on my PC? I assume `yes` – Huy Phạm Aug 04 '21 at 01:36
  • @HuyPhạm Actually, I meant `--work-tree`, not `--git-dir`. See my edited answer. – VonC Aug 04 '21 at 06:12
  • `git --work-tree=../WorkingFolder add -- afile-from-WorkingFolder` This line working. Instead of typing specifying `--working-tree` flag evey time, I define `GIT_WORK_TREE` `constant`. But SourceTree just doesn't get the `constant` and shows absolute no `Uncommitted changes` (while there is). Furthermore, if `git --work-tree=../WorkingFolder add -- afile-from-WorkingFolder` transfer changes from `WorkingFolder` to `WorkingFolder2`, it's a duplicate in term of space and I'm not sure this is what I'm looking for... – Huy Phạm Aug 04 '21 at 10:38
  • I can already avoid duplication by specifying `git --git-dir= gui` and Git GUI knows what `remote` to compare to and indexs `uncommitted changes`. I just haven't found out how SourceTree do this. If only `SourceTree` allows declaring `flags` – Huy Phạm Aug 04 '21 at 10:44
  • @HuyPhạm Can you try and launch sourceTree from a shell where you have set a ["repository location" environment variable](https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#_repository_locations): see if SourceTree takes said variable into account. – VonC Aug 04 '21 at 11:51
  • Yep, from `WorkingFolder2` I tried ` export GIT_WORK_TREE=../WorkingFolder ` and restart SourceTree: `WorkingFolder2` still can't detect uncommitted changes. Then I tried `export GIT_DIR=../WorkingFolder/.gittwo`, restart SourceTree again and still no luck. While `git gui` works. Guess I missed something, otherwise I just stick with `one SourceTree, one Git GUI` strategy – Huy Phạm Aug 04 '21 at 14:34
  • There was a command to `launch sourceTree from a shell` (`stree`). But I find very little information or download about `SourceTree command line tools` and it seems to be deprecated. Tried `stree .` and `stree`, both returns `command not found` – Huy Phạm Aug 04 '21 at 14:37
  • @HuyPhạm https://stackoverflow.com/q/19663202/6309 should help. What is your OS? – VonC Aug 04 '21 at 18:00
  • I use Windows 10. tried `@echo off start "" "C:\Users\huydu\AppData\Local\SourceTree\SourceTree.exe" --git-dir="/.gittwo" `, and found no luck with both absolute and relative `--git-dir` paths. Created a `SourceTree` shortcut and add flags to `Target`, results are **all** the same: it reopens added repos in the past – Huy Phạm Aug 05 '21 at 01:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235644/discussion-between-vonc-and-huy-phm). – VonC Aug 05 '21 at 05:09