How to remove commits from git history but otherwise keep the graph exactly the same, including merges?

后端 未结 5 911
礼貌的吻别
礼貌的吻别 2021-01-14 12:25

What I have:

---A----B-----C-----D--------*-----E-------> (master)
                     \\      /
                      1----2 (foo)
         


        
5条回答
  •  终归单人心
    2021-01-14 13:18

    The first thing to understand is that commits are immutable objects. When you rewrite history as you propose, you will end up with a completely different set of commits. The parent is part of each commit's immutable hash, among other things that you can't change. If you do what you propose, your history will look like this:

         D'-----E'-----> (master)
        /
    ---A----B-----C-----D--------E-------> (abandoned)
                         \      /
                          1----2 (foo)
    

    To acheive this, you would simply rebase D..E onto A and reset master to E'. You can (but really don't have to) then rebase 1..foo onto D'.

    A much simpler, and in my opinion correct, way would be to just delete the file in a new commit:

    ---A----B-----C-----D--------E-----F-----> (master)
                         \      /
                          1----2 (foo)
    

    Here F is the result of git rm that_file. The purpose of git is to maintain history. Pruning it just because it doesn't look pretty isn't productive (again, my opinion). The only time I would recommend the former option is of the file in question has sensitive information like passwords in it.

    If, on the other hand, scrubbing the file is what you want, you will have to take more extreme measures. For example: How to remove file from Git history?

提交回复
热议问题