Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Git restructure my folders without losing history?

Tags:

git

I have a git repo, and currently I'm the only one using it. I want to take all the files and folders under the root folder, and put them inside a new folder.

Current structure:

 main ->   src   res 

New structure:

 main ->   app1     src     res 

Is there a way I can do that so that the files don't lose their git history?

like image 264
Chen Kinnrot Avatar asked May 31 '12 06:05

Chen Kinnrot


People also ask

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

emiller/git-mv-with-history. git utility to move/rename file or folder and retain history with it. # git-mv-with-history -- move/rename file or folder, with history. # Git has a rename command git mv, but that is just for convenience.


1 Answers

TL;DR

Go ahead: move your files and directories around. Just make sure you don't make any edits to files in the same commit as your directory restructuring.

Why It Works

Git is a content tracker, not a file tracker. If you move/rename files, but make no other changes to the content of those files, then Git just rewrites the tree objects. The file blobs are not changed by directory operations; directory location information is stored separately in tree objects.

Actual rename detection is handled by diffing the blobs and trees, and looking for a configurable percentage of similarity between files. This means that Git doesn't really store moves or renames directly; it computes them from differences between commits.

The upshot of all this is that your history is not tied to a particular filename or directory structure. This works really well for most use cases, but stands in contrast to systems like Bazaar that track renames as first-class operations.

like image 81
Todd A. Jacobs Avatar answered Sep 19 '22 13:09

Todd A. Jacobs