Skip to content

Instantly share code, notes, and snippets.

@alexander-alegre
Created November 6, 2020 20:23
Show Gist options
  • Save alexander-alegre/3bd9864b9018082469101aba97cf88e4 to your computer and use it in GitHub Desktop.
Save alexander-alegre/3bd9864b9018082469101aba97cf88e4 to your computer and use it in GitHub Desktop.
Script to rewrite a branch's history
#!/usr/bin/env python3
# Rewrite whole history for a branch
import os
import subprocess
from pathlib import Path
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def get_active_branch_name():
head_dir = Path(".") / ".git" / "HEAD"
with head_dir.open("r") as f: content = f.read().splitlines()
for line in content:
if line[0:4] == "ref:":
return line.partition("refs/heads/")[2]
current_branch = get_active_branch_name()
print("Current branch: " + current_branch)
base_branch = input("Branch to reset current branch from: ")
# check if branch exists
# branch_exists_command = "git branch --list " + base_branch
branch_exists_command = "git show-ref --verify refs/heads/" + base_branch
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
print("Checking if " + base_branch + " exists...")
print(bcolors.OKCYAN + branch_exists_command + bcolors.ENDC)
branch_exists = os.system(branch_exists_command)
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
if (branch_exists != 0):
print(bcolors.FAIL + base_branch + " does not exists" + bcolors.ENDC)
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
exit()
new_commit_message = input("New message: ")
reset_command = "git reset $(git merge-base " + base_branch + " " + current_branch + ")"
add_all = "git add --all"
commit_message = "git commit -m '" + new_commit_message + "'"
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
print(bcolors.OKCYAN + "Running: " + reset_command + bcolors.ENDC)
os.system(reset_command)
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
print(bcolors.OKCYAN + "Running: " + add_all + bcolors.ENDC)
os.system(add_all)
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
print(bcolors.OKCYAN + "Running: " + commit_message + bcolors.ENDC)
os.system(commit_message)
print(bcolors.HEADER + "\n|-----------------------------------------------------------------------------| \n" + bcolors.ENDC)
print(bcolors.OKGREEN + "Done. \n" + bcolors.ENDC)
@alexander-alegre
Copy link
Author

Install

get the script

wget https://gist.githubusercontent.com/alexander-alegre/3bd9864b9018082469101aba97cf88e4/raw/ec70084a8fe5fc1cb57f20378295e23b09c7f7dc/gitrewrite.py

make the script an executable

chmod +x gitrewrite.py

add alias to .bashrc or .zshrc

alias gitrewrite="/PathToWhereYouSavedIt/gitrewrite.py"

source file

source ~/.zshrc or source ~/.bashrc

Using

  1. gitrewrite
    • you will be asked for a base branch (usually your development branch develop or master)
      • If the give branch does not exists, it will fail and exit
    • If the branch exists you will be asked for the new commit message
      • The script will reset the branch, add all the files and commit with the supplied message
      • All commands that are ran are printed on the console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment