Fix license in old Git commits

git, git-rewrite-history, github

Solution

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.

Problem

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.

Original source