4

Lets us say I have a file filename in a git repository in subirectory dir1 with a history something like this:

v7 --- v8 --- v9 --- v10

The file is part of a larger repository with various subdirectories, but only the above changesets affect this file. Now I have dug up an old version of that file (filename too) that has by mistake been committed to another subdirectory dir2. This older version only "exists" in one changeset, say:

v4

The problem now is, can I rewrite the history of these (to git) two files so that it appears as if filename has only existed in dir1 resulting in a history something like this:

v4 --- v7 --- v8 --- v9 --- v10

I guess I could try removing the file in dir2 and committing it as a new version of the file in dir1, but that will mess up the actual history of the file so that what is actually the oldest version of the file will appear as the newest.

I have very little experience with "tweaking" the history of git repositories, so I do not know how to do this, but I am guessing rebase may be the answer? I am the only one working on the repository, so my only concern will be sync'ing these changes across two computers.

I guess what I want to do is really just leave the file originally in dir2 where it is in the history, but I want to change the commit called v4 above to make the file a version of the file in dir1.

6
  • 1
    Git is smart enough to see that two files are identical even if they are in different directories, so if you are doing this to save space it isn't necessary. I'm not entirely sure what it is you want to change, but what you probably want is an interactive rebase. Read this, esp. the "Splitting a Commit" section. This will let you go back and edit previous commits. Nov 27, 2013 at 17:14
  • Space is not an issue. Instead of having filename in dir2, I want it to become a previous version of filename in dir1 (which it really is). I will try your link to see if I understand how to do it from that description, thanks. Nov 27, 2013 at 22:39
  • 2
    If I understand you right, you want to make it look like all versions of filename were in dir1 and never in dir2? You will need to go and edit your commit v4 using rebase -i v4, choose edit for that particular commit, and then change it. Nov 28, 2013 at 5:56
  • This seems to be the way to go. During the subsequent replaying of commits, I encountered some conflicts on that file, though. Selecting what seemed to be the newer version of the file to resolve each of these, it seems it has now boiled the history of the file down to fewer changesets!? This wasn't exactly what I expected. I don't dare to push anything now. I will need to check it against the other copies of the repository when I get back to work tomorrow. Nov 28, 2013 at 20:58
  • That suggests that the file in dir2 wasn't a version of the file in dir1, at least not in a linear way. I know Git will usually refuse to make commits that don't include any changes, so some of your conflict resolutions probably made it so that the file didn't change, and git simply eliminated those commits. You might want to redo the rebase, and try to merge changes together for conflicts rather than choose one of the versions. Nov 29, 2013 at 14:51

0

You must log in to answer this question.

Browse other questions tagged .