git filter-branch -f --tree-filter "if link:\$(grep_'Apache'_somefile)[\$(grep 'Apache' somefile)]; then sed -i -e '2,16d' somefile; fi"
I’m unsure about the legality of doing something like this. I think though that this probably shouldn’t be used if you’ve already released your project. If however you have not yet released and have changed your mind to use a different license prior to its release, this may be just the post for you.
I recently was working on a project that prior to releasing, I decided upon using the Apache V2 license. After something thinking though (and about 10 commits), I decided I wanted to release this project under the copyleft GPL v2 license. Unfortunately though, I had already commited the LICENSE file as well as put the shortened license header at the top of my program’s files. Thankfully, git has a solution to fix this problem. However, we will have to fix this in two steps since we will be rewriting a certain file as well as deleting another entirely (LICENSE).
git filter-branch -f --tree-filter "if link:\$(grep_'Apache'_somefile)[\$(grep 'Apache' somefile)]; then sed -i -e '2,16d' somefile; fi"
What this does is modify the contents of file somefile. Effectively, for
each commit in history (git filter-branch --tree-filter
), this checks if the
file somefile contains the string Apache. If it does, it then uses sed to
do an inline edit to delete lines 2-16 (those are the lines containing my
license header). You will likely need to change those since not all license
headers are the same length (and don’t start at line 2).
Now that we’ve cleaned out the license header, we just need to remove the LICENSE file from all of history so we can put a new one in. To do this, we’re going to use the --index-filter switch.
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch ./LICENSE'
Something to note about the git rm command we just ran. Notice the --ignore-unmatch switch. That will make git rm return a 0 status even if the specified file is not found. Basically, that means that it will keep the git filter-branch command from exiting when it happens upon a commit where the file doesn’t currently exist.
Category:Git