1

There is a repository with code.

I want to store some directories and files in the repository, but so that their changes are not reflected in the development history.

For example, there is a catalog .vscode with VS Code settings and a file with notes my_notes.txt, and I want to be able to access them from different computers.

How can I make sure that the specified directories and files are saved in the repository (preferably with the history of changes), but so that their history is not mixed with the history of code development and at the same time they are available in this repository?

For example, I changed the my_notes.txt on one computer and then made a commit:

git commit my_notes.txt <some_magic>
git push <some_magic>

After that I want to get this file on another computer:

git pull <some_magic>

Perhaps orphaned branches are suitable for this, but I could not find how to do so I want.

If I do so:

echo my_notes.txt >> .gitignore
git add .gitignore
git commit -m 'update gitignore'
git checkout --orphan orhf-branch
git rm -r --cached .
rm -rf *
echo 'some text' > my_notes.txt
git add .
git commit -m 'Orphan commit'
git checkout master

then file my_notes.txt disappears from the master branch.

Is it possible to make the file my_notes.txt was present when I switch to the master branch, but the history of its changes was saved in the orhf-branch? And so that I can change it in the master branch, but it doesn't show up as changed or untracked in master?

Wild Zyzop
  • 580
  • 1
  • 3
  • 13

1 Answers1

1

You could add the branch orhf-branch of your own repository as a submodule in your master branch.

For an example of such a technique, see "What's the easiest way to deploy a folder to a branch in git?".

As a submodule, my_notes.txt would be visible in a subfolder (say 'orhf') which is the root folder of the submodule.
Yet, any change/new commit made in the master branch would be separate from the orhf_branch.

Any changes you are making in the orhf_branch and pushing, can be updated in any clone with a git submodule update --remote.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Only if you save the actual file elsewhere (such as in a submodule), though. You can make a symbolic link from `./my_notes.txt` to `orhf/my_notes.txt` to make this more convenient. You must also be aware of the pain of submodules (this is considerably reduced from early days of Git though). – torek Jan 05 '20 at 19:55
  • To be clearer: it is possible, without having to "delegate files" to a separate repository. It can be the same repo, as long as those files can exist in their own folder. I would avoid any symlink. – VonC Jan 05 '20 at 19:56
  • Well, sort of: if you make the submodule repository link to the original repository, it should all work. It's a little confusing, though. – torek Jan 05 '20 at 19:57
  • It is also an old technique used from *years* (2008: http://blog.blindgaenger.net/generate_github_pages_in_a_submodule.html) – VonC Jan 05 '20 at 19:59
  • Interesting. I had not come across this idea before. I think I may just delete my answer, since this is a much less annoying technique than a separate repository... – torek Jan 05 '20 at 20:01
  • The idea is: a submodule is *not always* a separate repository, indeed. It (the submodule) can follow a branch of the same repo. – VonC Jan 05 '20 at 20:02
  • @VonC This is a very interesting idea, thank you! I need some time to figure it out and try. Perhaps I did not formulate the question very precisely. There are many branches in the repository besides `master`, will the `orph` directory be present in all branches, or only in the `master` branch? – Wild Zyzop Jan 07 '20 at 09:43
  • @WildZyzop Adding a submodule is like adding a file: that file will be in all branches only if you merge them. If not, you need to add it separately in each branch. – VonC Jan 07 '20 at 12:26