'Git -- how to have one file flow into another?

What I need is less than a branch, and submodule doesn't appear to be a fit.

I have a repo with three files:

  • /foo.bar
  • /foo1.bar
  • /foo2.bar

This isn't really production code. It is demonstration code.

foo.bar is a stand alone function, and I will now build a real piece of code from it in /foo3.bar.

In a perforce context, I would branch foo.bar to foo3.bar and then start working on foo3.bar. This would give me all the history of foo.bar, and allow changes to foo.bar to be integrated into foo3.bar.

Another way to say it: I need to rename foo.bar to foo3.bar. However, live in VCS land and must always retain the full commit history. Imagine further: Over the history of foo9.bar, it has been named foo.bar, foo3.bar, and a few other names before arriving at foo9.bar. How is the full history, including all commits and all renames, done?

(And then my original scenario simply retains one of the earlier files in addition to the subsequent names. For example, foo.bar is renamed to foo3.bar, but then foo3.bar persists into the future and also becomes foo9.bar.)

How do I do this in git?

git


Solution 1:[1]

You're looking for file identity.

Files, in Git, do not have identity. It's simply not there. Git has only commit-level identity. As Kaz notes in comments, Git will do a "best effort" attempt at rediscovering file identity after-the-fact.

Because Git assumes, by default, that files with the same name are "the same file", you can get as close as you like to file identity by ensuring that the file's name never changes. To make this work with what you're doing, consider giving the file an internal numeric ID as its true name and storing this in, e.g., a hidden directory .source/flatfile1234. Replace the user-oriented names foo3.bar with symbolic links.

Git doesn't really handle symbolic links terribly well either—they can't be merged—but now you have your file identity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 torek