Last active
January 21, 2020 11:26
-
-
Save tovask/7a0170f79019b219dff72e68bdf6d1fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tl;DR; Don't believe to a git history... | |
# IMPORTANT: THESE COMMANDS CAN CAUSE DAMAGE, USE IT AT YOUR OWN RISK | |
# https://git-scm.com/docs/git-filter-branch | |
git log --all --decorate --oneline --graph | |
git log --all --decorate --graph --abbrev-commit --pretty=fuller | |
git log --pretty=format:"%h%x09%an%x09%ae%x09%ai%x09%s%x09%d%n%x09%cn%x09%ce%x09%ci" | |
# first of all, initialize a git repository | |
git init | |
git config user.name "<YOUR_NAME>" | |
git config user.email <YOUR_EMAIL> | |
git config push.default simple | |
git remote add origin <REPO_URL> | |
#git push origin master | |
# when first pushing | |
git push --set-upstream origin master | |
#git branch --set-upstream-to=origin/master master | |
# to change an existing remote url | |
git remote set-url origin <NEW_URL> | |
### to pull all remote branches | |
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done | |
git fetch --all | |
git pull --all | |
### rewrite old content | |
####remove content from history (only first level, not recursive into directories!) | |
git filter-branch --force --tree-filter 'sed -i "s/ .*@gmail\.com//g" *' -- --all | |
git filter-branch --force --tree-filter 'sed -i "/@gmail\.com/d" *' -- --all | |
git push origin --force --all | |
git push origin --force --tags | |
#### remove files | |
git filter-branch --force --prune-empty --index-filter 'git rm --cached -r -q -- . ; git reset -q $GIT_COMMIT -- keep_this keep_that_too' -- --all | |
#git filter-branch --force --prune-empty --subdirectory-filter keep_this_folder -- --all | |
### change date | |
date="<TARGET_DATE>" | |
LC_ALL=C GIT_COMMITTER_DATE="$date" git commit --date "$date" | |
LC_ALL=C GIT_COMMITTER_DATE="$date" git commit --amend --no-edit --date "$date" | |
git filter-branch --env-filter \ | |
'if [ $GIT_COMMIT = <TARGET_COMMIT_HASH> ] | |
then | |
export GIT_AUTHOR_DATE="<TARGET_DATE>" | |
export GIT_COMMITTER_DATE="<TARGET_DATE>" | |
fi' | |
git filter-branch --env-filter ' | |
date="<TARGET_DATE>" | |
export GIT_AUTHOR_DATE="$date" | |
export GIT_COMMITTER_DATE="$date" | |
' | |
git filter-branch --env-filter ' | |
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" | |
' | |
# date -d ${GIT_COMMITTER_DATE%% *} | |
git filter-branch --force --env-filter \ | |
'shift=" + 1 day" | |
if [ $GIT_COMMIT = <TARGET_COMMIT_HASH> ] | |
then | |
export GIT_AUTHOR_DATE="$(date --date="$(date --date ${GIT_AUTHOR_DATE%% *})$shift")" | |
export GIT_COMMITTER_DATE="$(date --date="$(date --date ${GIT_COMMITTER_DATE%% *})$shift")" | |
fi' | |
# one-liner | |
git filter-branch --force --env-filter "if test $GIT_COMMIT = '3b0f476ca46d925b37990784f578bad424206e65'; then GIT_AUTHOR_DATE='<TARGET_DATE>'; GIT_COMMITTER_DATE='<TARGET_DATE>'; fi" | |
# go wild: https://github.com/angusshire/greenhat | |
### change author and committer | |
git filter-branch --env-filter ' | |
OLD_EMAIL="<OLD_EMAIL>" | |
CORRECT_NAME="<NEW_NAME>" | |
CORRECT_EMAIL="<NEW_EMAIL>" | |
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] | |
then | |
export GIT_COMMITTER_NAME="$CORRECT_NAME" | |
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" | |
fi | |
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] | |
then | |
export GIT_AUTHOR_NAME="$CORRECT_NAME" | |
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" | |
fi | |
' --tag-name-filter cat -- --branches --tags | |
git filter-branch --force --env-filter ' | |
CORRECT_NAME="<NEW_NAME>" | |
CORRECT_EMAIL="<NEW_EMAIL>" | |
export GIT_COMMITTER_NAME="$CORRECT_NAME" | |
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" | |
export GIT_AUTHOR_NAME="$CORRECT_NAME" | |
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" | |
' --tag-name-filter cat -- --branches --tags | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment