Is it possible to slim a .git repository without rewriting history?

后端 未结 4 640
礼貌的吻别
礼貌的吻别 2020-12-15 05:24

We have a number of git repositories which have grown to an unmanageable size due to the historical inclusion of binary test files and java .jar fi

4条回答
  •  温柔的废话
    2020-12-15 05:59

    No, that is not possible – You will have to rewrite history. But here are some pointers for that:

    • As VonC mentioned: If it fits your scenario, use BFG- repo cleaner – it’s a lot easier to use than git filter-branch.
    • You do not need to clone again! Just run these commands instead of git pull and you will be fine (replace origin and master with your remote and branch):

      git fetch origin
      git reset --hard origin/master
      

      But note that unlike git pull, you will loose all the local changes that are not pushed to the server yet.

    • It helps a lot if you (or somebody else in you team) fully understand how git sees history, and what git pull, git merge and git rebase (also as git rebase --onto) do. Then give everybody involved a quick training on how to handle this rewrite situation (5-10 mins should be enough, the basic dos and don’ts).
    • Be aware that git filter-branch does not cause any harm in itself, but causes a lot of standard workflows to cause harm. If people don’t act accordingly and merge old history, you might just have to rewrite history again if you don’t notice soon enough.
    • You can prevent people from merging (more precisely pushing) the old history by writing (5 lines) an appropriate update hook on the server. Just check whether the history of the pushed head contains a specific old commit.

提交回复
热议问题