Search code examples
gitgithubgitlabgit-commitgit-push

Is it possible to re-arrange a repository and keep commit history of files that being re-allocated?


I have a big repo that is connected to a remote repository as well.

I want to re-arrange it but I don't want to apply those changes on the current one so I created a new local directory, a new empty remote repository.

I mirrored my current repo to the new empty remote repository.

Then, I connected the new empty directory to that repostiroy and git pull it.

Now I have a full copy of my repository on my local new repository.

I wanted to re-arrange it, and move some files in/outside their current direcotories.

The problem is that when I do it, and then commit & push to the remote repository, I see that all the commit history gets deleted after it moves to the other location.

When I edit files but keep them in their current location, and then commit & push, the changes get uploaded and it keeps the commit history.

So the problem is only when I move the files to other directories inside that mirrored repository.

Is there any way to keep the commit history of the commit that were made before the mirror even if I move files?

Thanks.


Solution

  • Very simple. You create a branch which will remain unchanged, checkout your main branch and perform the changes. You do not need to merge these branches, you will have the old commit history in the branch which was created for this purpose and the new commit history in your main branch, master.

    EDIT

    Let's assume that you are on the master branch.

    Perform:

    git branch archive
    git checkout archive
    git push -u origin archive
    

    This creates a branch in your repository and then updates it on the remote repository.

    Now, perform

    git checkout master
    mv myfolder1/myfile myfolder2
    git add .
    git commit
    git push
    

    View

    git log
    

    You will see that the old history is lost. But now perform

    git checkout archive
    git log
    

    And you will see that in the archive branch you have all the commit history of the file prior of its moving.