Created
November 6, 2020 20:23
-
-
Save alexander-alegre/3bd9864b9018082469101aba97cf88e4 to your computer and use it in GitHub Desktop.
Script to rewrite a branch's history
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
#!/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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
orsource ~/.bashrc
Using
gitrewrite
develop
ormaster
)