2

In my research project, I have code files and a Markdown file called journal.md where I write all my thoughts, meeting notes, etc. I have different branches that allow me to work on different parts of the project without affecting the rest. I would like the journal file to be the same on every branch: I want it to contain everything I wrote in it so far, regardless of the branch that was checked out when I wrote these things. I can think of two ways to do that, but they have big drawbacks:

  • do not track the journal.md file: the issue is that I would like this file to be saved on Github with the rest, so it's backed up and I can pull it if I ever work from another machine
  • get the most recent version of the journal.md file whenever I change branches (e.g. by checking it out from the branch where I made the latest changes): that's a pain and I will definitely forget to do so

Is there a better way to achieve what I want?

Ben
  • 429
  • 4
  • 11
  • Worth noting: tracked-ness is not *branch*-dependent but rather *commit*-dependent. It's the *current commit* that's *branch*-dependent. So tracked-ness is the result of a chain of items, not *directly* a result of a branch. All methods of sidestepping this amount to putting the file into a different repository (even if that "different repository" is this same repository, re-cloned). – torek Aug 20 '21 at 05:49

1 Answers1

1

Yes, provided journal.md is seen in its own folder by other branches, and is maintained alone in its own dedicated branch.

That is an old trick where:

  • you have a file (journal.md) in its own branch named, for instance, 'journal', alone;
  • you declare in each of your other branches a submodule of another branch (seen first here)

That is:

git submodule add -b journal -- /path/to/your/own/repo journal
git commit -m "Add journal branch as submodule"

That will create a folder journal/ with the content of the branch (journal.md)

At any time, you can do a git submodule update --remote and that folder journal/ will be updated with the latest commit of the journal branch (meaning the latest content of journal.md)

If you git config --global submodule.recurse true, a simple git pull in your own branch would be enough.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You can do this without going the separate-repo route. Make a branch and directory for it, `git worktree add` it, then `git add` the new versions as you make them. – jthill Aug 19 '21 at 20:56
  • @jthill Sure. I prefer the submodule approach, where any commit made in each branch *also* records the state of the journal branch at that time. – VonC Aug 19 '21 at 21:05