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

前端 未结 14 2467
梦如初夏
梦如初夏 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:26

    I followed this multi-step process to move code to the parent directory and retained history.

    Step 0: Created a branch 'history' from 'master' for safekeeping

    Step 1: Used git-filter-repo tool to rewrite history. This command below moved folder 'FolderwithContentOfInterest' to one level up and modified the relevant commit history

    git filter-repo --path-rename ParentFolder/FolderwithContentOfInterest/:FolderwithContentOfInterest/ --force
    

    Step 2: By this time the GitHub repository lost its remote repository path. Added remote reference

    git remote add origin git@github.com:MyCompany/MyRepo.git
    

    Step 3: Pull information on repository

    git pull
    

    Step 4: Connect the local lost branch with the origin branch

    git branch --set-upstream-to=origin/history history
    

    Step 5: Address merge conflict for the folder structure if prompted

    Step 6: Push!!

    git push
    

    Note: The modified history and moved folder appear to already be committed. enter code here

    Done. Code moves to the parent / desired directory keeping history intact!

提交回复
热议问题