Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: move files in history too

Tags:

git

git-branch

is it possible with Git's tools to move files into a new folder while modifying its full history, as if the files had been there from their first add?

I came up on this after merging some repos together: I moved the files from several repos to distinct folders inside one "super" repo, however the merged history behaves very bad with git rebase and git svn tools as totally different files may collide at their old locations of course.

like image 498
dronus Avatar asked Apr 13 '11 22:04

dronus


People also ask

How do I move files and keep my git history?

In order to solve this, we use git mv command. git mv helps us to rename or move files without deleting their previous history.

Does git track moving files?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).

How does git handle moving files?

The git repository distinguishes files by checksum, not by name or location. If you commit, and then move a file to a different location and commit, the file in the before location and the file in the after location have the same checksum (because they have the same content).

How do I rename a directory in git without losing history?

No. The short answer is NO. It is not possible to rename a file in Git and remember the history.


1 Answers

So now this got the job done:

git filter-branch --tree-filter '(ls -A; mkdir TheSubdir; echo TheSubdir) | xargs mv'

Strange but nice: the .git dir stays where it should; maybe git is preventing its movement somehow?

like image 186
dronus Avatar answered Sep 21 '22 11:09

dronus