2

I have a Git repository with the following folder structure:

allprojectfiles
--otherfolders
--source
----projectname
------projectname
---------git initilalized here
---------otherfolders
---------workingonthesefiles(bound to remote)

Can I restructure the project to have Git track from the allprojectfiles folder without losing history?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Xdrone
  • 781
  • 1
  • 11
  • 21

2 Answers2

4

Git Tracks Trees and Blobs, Not Folders

The only folder-tracking Git does is in tree commits. So, in the general case, you can move folders around freely and Git will simply write a new tree to the repository.

I recommend using git mv to stage these directory moves in an otherwise clean working tree. If you don't have any file-level changes staged for the commit, then Git history will correctly treat your new structure as a move/rename, rather than an add/delete operation.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • I agree (just saw your tweet). +1 – VonC Jul 15 '13 at 22:08
  • I am still a little confused on this. Should I create a clean directory initilize the dir to the same origin and move the old dir in to the new dir and commit. Is this the correct thaught process for your recommendation? My main issue is the files located above the directory that contains git. – Xdrone Jul 19 '13 at 23:26
  • @xdrone I don't understand what you're asking. Directories above the Git repository have nothing to do with the Git repository. Changes in the parent tree have nothing to do with the repository layout, and changes within the working tree are tracked as tree commits. – Todd A. Jacobs Jul 19 '13 at 23:48
  • Ok, to be clearer, I would like to move the "git-initialized" folder up the folder tree to include the following folders in the parent tree. allprojectfiles, --otherfolders, --source, ----projectname, ------projectname without breaking the history. Thanks again – Xdrone Jul 22 '13 at 20:11
  • @xdrone Why would moving the parents of the working tree break anything? Back up your files, move your directories around, and it should "just work." – Todd A. Jacobs Jul 22 '13 at 21:34
  • Thanks, just moved the git folder up the tree and committed it. – Xdrone Aug 10 '13 at 09:59
1

git does not track renames explicitly. be sure to have one commit that does nothing but the moving (which will be a remove+add at another path). then git commands will autodetect the moves, but sometimes (e.g. if two of the files have the same contents, but logically you can tell which one was renamed) it will fail to trace the proper history. i do not thing you can avoid this without rewriting history.

this brings me to an idea: with git filter-branch it should be possible to extract the folders to another location, i.e. the "correct" location in the new layout, and then re-add the rest. this will totally rewrite your history, though.

mnagel
  • 6,729
  • 4
  • 31
  • 66