refulgentis
refulgentis

Reputation: 2635

Lost git history after reorganizing project folder

I made a commit about a month ago that involved me creating new folder and sub-folders and moving my source code files in between them. I just was looking through my history for the first time since then and realized git has 'lost' history since the original files were deleted and then re-added, I suppose - i.e. when I'm viewing a file in my git GUI (it's under NDA so I can't discuss it directly, but for example, this repository is broken on GitHub too. GitHub clearly shows its detecting the commit as a series of moves.) it only shows history for each file back to when the project folder was reorganized.

After reading a few questions ( Getting Git to Acknowledge Previously Moved Files, How to make git mark a deleted and a new file as a file move?), I'm simply more lost than when I started. It sounds like from those answers that I won't be able to fix this at all? I'd really appreciate any help here.

Upvotes: 23

Views: 9872

Answers (7)

Gian Marco
Gian Marco

Reputation: 23199

In Git there's no concept of file move.

Some tools, like GitHub, consider a commit containing a file named X that has been deleted and a file named Y that has been created a file move.

According to Linus Torvalds, file move is just a special case of refactoring; for this reason Git will not treat it differently. Handling of this special case, like many others, is left to higher-level tools (such as frontends).

For more info on this topic, check this answer from Linus Torvalds.

Upvotes: 12

Steve W
Steve W

Reputation: 71

If you want to move folders around in git, you can use git mv.

I had a bunch of folders in the root of my repository, and wanted to move them into two subdirectories, so I created the two new directories using **mkdir.

Then I moved files and folders one at a time into the new directories like this:

git mv folder1/ newDirectory1/
git mv file1.txt newDirectory2/

etc.

I had a case where I wanted to rename one of the directories to src and I did it like this:

git mv folder2 newDirectory1/src

This resulted in a set of files that looked like this:

repository/
   newDirectory1/
      folder1/
      src/
   newDirectory2/
      file1.txt

After I was finished, I created a new branch called "reorganized" so I wouldn't interfere with work another developer was doing in the master branch. He continued to work on the files, and as he pushed new changes to master, I pulled and merged the changes into my branch, and everything worked as I had hoped. Files that had been moved were referenced properly from their original locations, and received their commits.

Upvotes: 3

Adam Dymitruk
Adam Dymitruk

Reputation: 129574

I would run a script that goes through all the objects. You would need to unpack all the pack files first. The script would check the type, if it is a commit you would see if you are the author. Then check the date. List those and grep for the file name that you want. Once you have the commit of interest, create a branch with

git branch RecoveredWork hash-of-your-commit

and see if you have all that you want with

git log RecoveredWork --graph --decorate

from here you may want to do some filter branching, grafting and/or rebasing to link up the history again.

Upvotes: 0

Jeff Ferland
Jeff Ferland

Reputation: 18292

I can think of a few possibilities. If things were just shuffled around, but don't track, then I presume that this was done in two different commits... one to remove and then one to re-add. Create a new branch in the commit before that, load in the next two commits as one commit, and then append everything after that.

If that is not the case, you may need to look at the commits with git log -M -C

Upvotes: 0

rioki
rioki

Reputation: 6118

As far as I see it you want:

git log --follow some_file.cpp

See http://git-scm.com/docs/git-log for details. I am not sure if that is what you want; but in case of git, git tracks content not files. The problem is that determining that information is really expensive and it is assumed that normally you don't need it...

Upvotes: 6

VonC
VonC

Reputation: 1324447

Did you try settings the config diff.renames?

diff.renames

Tells git to detect renames. If set to any boolean value, it will enable basic rename detection. If set to "copies" or "copy", it will detect copies, as well.

Note: to follow history of a single file across renames, you need to use "git log -p --follow file".

Upvotes: 2

Andrew Aylett
Andrew Aylett

Reputation: 40710

If you're actually missing revisions from your history (it's not completely clear from your question what's actually missing) and it's only been around a month, you probably still have time -- look at the reflog; it keeps a copy of every reference you have checked out, so if you make a mistake you can get back what you had before.

The default is to keep reflog entries for 90 days, they are expired by git gc.

git help reflog

Upvotes: 0

Related Questions