git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Following history of a copied file from another indirect branch

From
Brian Gernhardt <brian@gernhardtsoftware.com>
Date
Oct 21, 2010, 19:39 UTC
Message-ID
<9089D1F5-A19B-4030-A6ED-463F250E450B@gernhardtsoftware.com>
In-Reply-To
<4CC08AA5.8070502@workspacewhiz.com>
On Oct 21, 2010, at 2:47 PM, Joshua Jensen wrote:
> It has become a necessity to copy a file from one long-lived branch to another.  It is not possible to merge the branches at this time.
> 
> I would like to have 'git gui blame' follow the copy back through its original history, but I don't believe Git has metadata for storing this.  Something along the lines of a 'followparent' in the commit object, for instance, would allow the revision walking code to wander the history down an alternate line.
Git stores no per-file metadata.  The closest we come is .gitattributes and .gitignore.
> By comparison, integrates work at a file level in Perforce.  That means I can integrate a file from one branch to another, and parentage is stored such that I can follow the file back through its history.
> 
> Are there any facilities to do this now?
Git simply does not have the idea of the history of a file.  Nothing in git will help merge "just a file" from one branch to another.  Either we have merged the two commits or not.
HOWEVER...
You can use git-filter-branch to create a new branch that contains only that single file and only the commits that affected it.  Something like the following (untested):
	# Merging "file" from branch "src" to branch "dest"
	git checkout -b temp src
	git filter-branch --prune-empty --index-filter="git read-tree --empty; git add file"
	# Since you describe the branch as "long-running", I'd suspect you'll have to wait a while here.
	git checkout dest
	git merge temp
	git branch -d temp
	git branch -D refs/original/temp
This will go faster if you have a ramdisk/tmpfs to perform the filtering in.  (git-filter-branch is very I/O intensive.)  Something like the following in place of the `git filter-branch` invocation above:
	mkdir /tmp/filter-branch # Assuming /tmp is tmpfs or similar
	git filter-branch -d /tmp/filter-branch --prune-empty --index-filter="git read-tree --empty; git add file"
	rm -rf /tmp/filter-branch
You could use --msg-filter to add the SHA-1 of the original commits to the "file history" branch.  Something like --msg-filter='cat;echo;echo From: $GIT_COMMIT'
I would recommend using cherry-pick to pull any further changes to the file across branches (be careful of commits that touch more than that file!).  I think git-filter-branch could be used to keep the one file branch up to date, but that is likely more effort than it's worth.  I would specifically advise against merging the single file branch into both "src" and "dest", as I think any later merge of the two would find these commits as a merge-base.
~~ Brian Gernhardt
Previous: Joshua JensenNext: Joshua Jensen
Message 2 of 4 in “Following history of a copied file from another indirect branch”
  1. Joshua JensenOct 21, 2010
  2. Brian GernhardtOct 21, 2010
  3. Joshua JensenOct 21, 2010
  4. Johannes SixtOct 22, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.