Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created January 13, 2021 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/a465f7a6b5ec02cffaa81011685dca35 to your computer and use it in GitHub Desktop.
Save ZiTAL/a465f7a6b5ec02cffaa81011685dca35 to your computer and use it in GitHub Desktop.
git: rebase, count all commits to get rebase command
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import subprocess
""" GIT LOG EXAMPLE:
0eccd52 (HEAD -> froga) 02 # <- COUNT: FROM THIS
67239d6 01
fb3c480 12
dac05e9 11
cc6f658 10
349b1a5 4
2d188e6 1
2ab6e87 (origin/garapena, garapena) PHP7-49862 menu responsive # <- COUNT: TO THIS
99f7d89 AMP-49862 meta
557d191 AMP-49862 cmp
e1ad2d9 PHP7-49378 berria min js
"""
log = subprocess.getoutput("git log --decorate --oneline")
count = 0
start = False
lines = re.split("\n", log);
branch = ''
for line in lines:
m = re.search("[^\s]+\s+\(HEAD\s+\->\s+([^\)]+)\)", line) # 0eccd52 (HEAD -> froga) 02
if m:
start = True
branch = m.group(1)
continue
else:
m = re.search("[^\s]+\s+\([^\s]+,\s+[^\s]+\)", line) # 2ab6e87 (origin/garapena, garapena) PHP7-49862 menu responsive
if m:
start = False
continue
if start == True:
count = count + 1
print("branch: "+branch)
print("commits: "+str(count))
if count > 1:
print("command to use: git rebase -i HEAD~"+str(count))
print("IMPORTANT: Don't forget to change 'pick' to 'squash' in all of commits except in the first one")
print("CANCEL command: git rebase --abort")
print("More info: https://www.youtube.com/watch?v=V5KrD7CmO4o")
else:
print("none to rebase")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment