Skip to content

Instantly share code, notes, and snippets.

@howardhamilton
Created January 27, 2015 18:05
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 howardhamilton/d4508e44df5d5a25087c to your computer and use it in GitHub Desktop.
Save howardhamilton/d4508e44df5d5a25087c to your computer and use it in GitHub Desktop.
Transfer git commits from one branch to another (useful for rebasing)
#
# commit_transfer.py
#
# I wrote this script in order to transfer commits between branches in a less painful way. The use
# case is rebasing a working branch on its parent in order to use changes that have been pushed there.
# Yes, a rebase does preserve history, but only if you haven't already pushed the branch to a remote.
# If you have, it's best to create a fresh branch from the parent and transfer the commits there.
#
# My tasks are tracked in JIRA, so I write all of my commits with the following convention:
#
# AB-12345 Commit slug goes here
#
# I then run the following command in my old branch to grab all commits with that branch tag:
#
# git log --oneline --grep="AB-12345" > branch.hist
#
# Any other tracking system works as long as the tag doesn't have spaces.
#
# From the new branch I run this script to cherry-pick these commits to the new branch. It
# selects the commits in reverse order, so oldest commit first.
import subprocess
with open('branch.hist', 'r') as f:
lines = f.readlines()
lines.reverse()
for line in lines:
words = line.split()
shah = words[0]
branch = words[1]
command = "git cherry-pick {}".format(shah)
subprocess.call(command, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment