Is it possible to move/rename files in Git and maintain their history?

前端 未结 14 2175
梦如初夏
梦如初夏 2020-11-22 04:42

I would like to rename/move a project subtree in Git moving it from

/project/xyz

to

/components/xyz

If I

14条回答
  •  渐次进展
    2020-11-22 05:38

    To rename a directory or file (I don't know much about complex cases, so there might be some caveats):

    git filter-repo --path-rename OLD_NAME:NEW_NAME
    

    To rename a directory in files that mention it (it's possible to use callbacks, but I don't know how):

    git filter-repo --replace-text expressions.txt
    

    expressions.txt is a file filled with lines like literal:OLD_NAME==>NEW_NAME (it's possible to use Python's RE with regex: or glob with glob:).

    To rename a directory in messages of commits:

    git-filter-repo --message-callback 'return message.replace(b"OLD_NAME", b"NEW_NAME")'
    

    Python's regular expressions are also supported, but they must be written in Python, manually.

    If the repository is original, without remote, you will have to add --force to force a rewrite. (You may want to create a backup of your repository before doing this.)

    If you do not want to preserve refs (they will be displayed in the branch history of Git GUI), you will have to add --replace-refs delete-no-add.

提交回复
热议问题