How can I cache untracked (binary) files associated to a branch?

Assume I am one of many developers working with git on a huge code project, which has an incredibly long compilation time.

Now, my personal day-to-day changes are usually limited, and the codebase is decently modular, which means that as far as my personal work is concerned, compiling from scratch is a price that only has to be paid once. Afterwards, I can recompile only the modules I modified.

On the other hand, I frequently have to fix bugs in (at least two) branches which reflect states of the codebase completely distinct from the branch I work on. Those two branches are indeed the result of work from the entire development team, and often have entire modules rewritten or added. Having compiled everything before pushing a bugfix is mandatory.

My first approach was to switch from my development branch every time a bugfix is needed, then clean, bugfix, recompile from scratch, push, switch back to the original branch, clean again, recompile - the delay for doing this is intolerable.

I then moved to keeping three separate checkouts of the codebase on my machine. This solves the "costly recompilation" problem (my changes in every branch are incremental again), but it makes the question "what am I developing on right now ?" more complex to answer (since it depends on the current directory), and it makes synchronization of these three copies complex (it multiplies all the routine push/remote update/pull commands I have to do by three).

A simpler solution would be to have the ability to save the state of a directory (untracked, where I generate compiled files) every time my checkout "leaves"a branch, and to recall the state of that directory, if it exists, every time I "enter" (checkout) a branch.

  • Are there git commands that would help me achieve this behavior ?
  • If not, is there a tool that would allow me to do that ?
  • If not, does git provide checkout hooks that could help me script this behavior ?


I would suggest to use stashes, they are pretty close to what you need.

In recent versions of git, you can stash untracked files with the --include-untracked option. You can also give messages to your stashes and recall them, so you can check in which stash must be applied after checking out a branch.

#before leaving branch
git stash save --include-untracked "compiled stuff for my_branch"
git checkout another_branch
#what stash contains compiled stuff for another_branch?
git stash list
git stash pop stash@{n}

You can script a bit to make things more practical...

I'm afraid I do not have exact answers as I did not have to deal with a case like yours, but anyway I'd like to provide possible hint: Git is able to use several distinct work trees "connected" to a single repository (read more in this answer for instance), so instead of maintaining multiple checkouts, you can just use multiple work trees with a single repository.

I should add that I like @CharlesB's solution better (conceptually) but on the other hand I would measure: supposedly git stash would effectively copy your files when turning them into blobs to store in the stash which would a) temporarily double the space occupied by those huge files on the filesystem; b) have unknown long-standing effect on the size of the Git's object database size.

My 2 cents : keep track of the compilation directory as a submodule - but as I haven't used this git feature yet, I can only point you to the doc page...

Need Your Help

WCF Nhibernate Service with Integrated Security=True error

wcf security nhibernate service integrated

I am working on a WCF Service(implemented with Fluent NH), and it is hosted as a Windows Service.

convert long to byte[] returns unreadable format

java android fileoutputstream bufferedoutputstream

I am trying to convert long to bytes because I want to write the time stamp in seconds in a file.