Skip to content

Instantly share code, notes, and snippets.

@ordovician
Created November 5, 2012 19:24
Show Gist options
  • Save ordovician/4019776 to your computer and use it in GitHub Desktop.
Save ordovician/4019776 to your computer and use it in GitHub Desktop.
Change git history without modifying the tree blobs pointed to by commits
#! /usr/bin/env ruby
if ARGV.empty?
puts "Usage: git-change-history base [new-branch-name]"
puts " a list of SHA-1 for commits are given on STDIN. These are commits are added onto base"
puts "Example:"
puts " git log --pretty=\"%H\" master | git-change-history.rb base mynewbranch"
puts ""
puts "Use 'git log --pretty=\"%H %s\"' to test if you have the right SHA-1 commit hashes listed"
exit
end
# What branch or SHA-1 to to put all the commits comming on STDIN
base = ARGV.first
parent = base
STDIN.read.split("\n").reverse_each do |commit|
tree = `git log -1 --pretty=format:"%T" #{commit}`
#message = `git log -1 --pretty=format:"%B" #{commit}`
parent = `git log -1 --pretty=format:"%B" #{commit} | git commit-tree #{tree} -p #{parent}`
end
if ARGV.length == 2
`git branch #{ARGV[1]} #{parent}`
else
`git branch temp #{parent}`
end
@ordovician
Copy link
Author

I wrote this tool so that when doing conversion of SVN repositories to git, I could more easily deal
with repositories where code had been moved around a lot. This tool allows you to specify some commits
you want placed on top of another commit.

You can not simply use rebase for this, because it deals with differences. In these cases you do not
want to perform any merge. You just want to add the commit as it is. No modification of the tree blob it points to.

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