Today I found myself in a position where a Git repo I am contributing to was to going to have its history significantly altered. Sensitive information was to be completely removed from the repo, and when done the repo would basically be a single commit on the master branch, with all other branches deleted.
But there were several open pull requests against master that would not get merged before this work was carried out. I had to save the commits in these PRs offline until the repo had been adjusted, then reapply them and re-open the PRs.
Here’s one way that this can be done:
Commit range to patch files:
Ensuring you have your topic branch checked out and that it has some commits on it,
1 2 3 4
you can make a patch out of each commit with the git format-patch command:
1 2 3
For each commit, we now have a patch file that can be saved offline and reapplied later:
1 2 3
To reapply, move the patches to a directory outside of the repo (I moved mine to my home dir), checkout master and use the git am command:
1 2 3 4 5 6
Now the commits have be reapplied onto master. From here you can create a new branch, push it and re-open your pull requests.