I would like to rename/move a project subtree in Git moving it from
/project/xyz
to
/components/xyz
If I
It is possible to rename a file and keep the history intact, although it causes the file to be renamed throughout the entire history of the repository. This is probably only for the obsessive git-log-lovers, and has some serious implications, including these:
Now, since you're still with me, you're a probably solo developer renaming a completely isolated file. Let's move a file using filter-tree
!
Assume you're going to move a file old
into a folder dir
and give it the name new
This could be done with git mv old dir/new && git add -u dir/new
, but that breaks history.
Instead:
git filter-branch --tree-filter 'if [ -f old ]; then mkdir dir && mv old dir/new; fi' HEAD
will redo every commit in the branch, executing the command in the ticks for each iteration. Plenty of stuff can go wrong when you do this. I normally test to see if the file is present (otherwise it's not there yet to move) and then perform the necessary steps to shoehorn the tree to my liking. Here you might sed through files to alter references to the file and so on. Knock yourself out! :)
When completed, the file is moved and the log is intact. You feel like a ninja pirate.
Also; The mkdir dir is only necessary if you move the file to a new folder, of course. The if will avoid the creation of this folder earlier in history than your file exists.