Script to rebuild git history, applying code cleanup to every version
Has anyone got a script for git that can go through the history, check out each version, apply a cleanup script, then check the cleaned version into another repository?
I have some code which I've been deve开发者_运维百科loping, but I haven't been consistent with code formatting e.g. tabs vs spaces etc. I'd like to rewrite my entire history to be consistent with the new standards.
The git filter-branch
command does what you need.
For example:
# Make a backup!
cp -r <repo> <repo>.backup
cd <repo>
# Replace tabs with two spaces in all .cpp and .h files in all branches.
git filter-branch --tree-filter \
"find \( -iname '*.cpp' -o -iname '*.h' \) \
-exec sed -i -re 's/\t/ /g' {} \;" -- --all
# Delete branch backups created by 'git filter-branch'.
# From the end of `man git filter-branch`; more cleanup
# suggestions there.
git for-each-ref --format="%(refname)" refs/original/ | \
xargs -n 1 git update-ref -d
# You still have ../<repo>.backup, in case something went wrong.
But be careful... this transforms the git repository.
If somebody has a clone... it will not be connected to your new repo anymore.
From man git filter-branch
:
WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)
精彩评论