20

I've read in various FAQs that git doesn't explicitly track renames/moves, preferring to look for identical (or in some situations similar?) files. That's great, but will it cope with this situation: a friend's remote repository has a new feature (i18n) involving some new files at debian/po/*.po. I have my own fork of this project, and want to merge this feature but put the files at just po/*.po (I can do that as two commits, or whatever is necessary). I expect the remote repo will continue to receive updates to the feature, and I want to just merge/cherry-pick those commits and have them applied to the files in my new location. Can git do that, perhaps with some sort of mapping of "these files have moved over here now"? Or is it more pain than it's worth and should I just accept the slightly odd debian path in my repo?

3 Answers 3

45

use git mv and everything will be AOK.

Why not just try it instead of asking? You can always reset easily in git. :)

6
  • 21
    Well, I just wanted to see if anyone pointed out any gotchas that could crop up later if something strange that I didn't try happens in the remote repo and I didn't frobble the correct magic chicken when I did the move or that merge. I've only been using git a few weeks, and still have a (un)healthy dose of paranoia left over from lesser VCSs. :-) Nov 16, 2009 at 0:25
  • If the files wildy diverge then it will have trouble tracking the change, but it'll have that problem whether you rename it or not ;)
    – Pod
    Nov 16, 2009 at 0:27
  • Thanks, it does indeed all look OK so far. Nov 16, 2009 at 1:14
  • 4
    FYI: "git mv" is no different from renaming the file yourself and commiting the changes like that. It's just easier.
    – Pod
    Nov 16, 2009 at 11:30
  • 1
    There are some gotchas, see: git.661346.n2.nabble.com/… Aug 5, 2015 at 16:24
3

Git lacks a mechanism to indicate that you expect there to be path renames and updates when doing the diff between two projects/branches etc.

There are the various file rename options available (such as -M and --patience) but can be slow.

As already stated, the path renames don't affect the repository itself because it is simply a snapshot of your content (blobs) and structure (tree nodes). If all you have done is added an extra top level directory then all the trees and blobs below are unchanged and require zero additional storage. All you need is the one tree node for your commit and one tree node for the new tld. Dead easy. Git handles that part no problem.

It is only when you want to do a comparison (and any patches) that it matters. It would be nice to have say a -P option that indicates that you expect some path renames and for the diff to thereby cope easily. It's not good seeing 200 file deletes and 200 new files ;-)

Finding out how to add a -P option is another one of my 'to do' list items (I hope I get some time for it).

2

Alternatively, rename the files using mv, and invoke git add --all to stage all the delete operations and add the untracked files (the new file names). Without --all, you must explicitly stage the delete operations and new file names separately. Don't use git add --update, either, as this will stage all the delete operations, but won't add the new file names.

Note that you'll likely want to perform these operations in a local branch so that you don't inadvertently push them to your friend's master branch or your friend doesn't pull them into his master branch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.