Fix license in old Git commits

此生再无相见时 提交于 2019-12-08 00:40:52

问题


I have a currently private GitHub repository which exists for a few years now. In the README.md file there is a license, which is not accurate any more.

Now I would like to turn this private repo into a public one, with the fixed license. Anyway, all of the old commits still contain the old (wrong) license.

How could I solve this? My first idea was to squash the repository to a single commit and destroy everything else, but maybe there is a better way to achieve what I want.

As you can see from the other question, keeping history is not important, but OTOH it doesn't hurt as well.

What actually hurts is having old commits with the wrong license.

Any ideas?

PS: I want to ensure that nobody is able to get an old commit, not even if they know the commit's id. So, the solution to this must also pay attention to updating the remote repositories, such as GitHub.


回答1:


You can use filter-branch to do this. First write a small script that rewrites the tree for a given commit. e.g., Following is something that changes something to something else in the README.md file only if it exists.

if [ -f README.md ]; then
    sed 's/something/something else/g' README.md > tmp
    mv tmp README.md
fi    

Save this as change.sh and then run the following

git filter-branch --tree-filter "/bin/bash $(pwd)/change.sh" HEAD

This will rewrite all the commits going back from HEAD. If you've made a mistake, you can go back to the earlier tree using git reset and try again.



来源:https://stackoverflow.com/questions/43863765/fix-license-in-old-git-commits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!