Move a file/directory but still merge changes easily?

前端 未结 3 897
轮回少年
轮回少年 2021-01-03 23:17

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 gr

相关标签:
3条回答
  • 2021-01-03 23:57

    use git mv and everything will be AOK.

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

    0 讨论(0)
  • 2021-01-03 23:58

    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).

    0 讨论(0)
  • 2021-01-04 00:17

    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.

    0 讨论(0)
提交回复
热议问题