の--squash
オプションgit merge
は便利な場合があり、git1.4.1以降で使用できます。これにより、マージの効果がステージングされますが、コミットは作成されません。したがって、143eff
が押しつぶされたコミットに含める最も古いコミットであり、現在のブランチがmaster
であり、「1か月前」のコミットがdcb7e5
である場合、次のことができます。
# Save the old position of "master" by creating a branch old-master:
$ git checkout master
$ git branch old-master
# Create and checkout a branch called "new-master" that's at the old commit:
$ git checkout -b new-master 143eff
# Stage the effects of merging the "one month ago" commit:
$ git merge --squash dcb7e5
Updating 143eff3..dcb7e5b
Fast-forward
Squash commit -- not updating HEAD
[... status output showing the staged changes ..]
# Create the squashed commit:
$ git commit -m "A commit squashing history up to a month ago"
# (You could use --amend if you want them to be squashed into 143eff
# instead of being a commit after that.)
git diff dcb7e5 new-master
これで、それらが実際に同じであることを確認できます。
次に、残りの作業をnew-masterにリベースします。
$ git rebase --onto new-master dcb7e5 master
それはあなたが望む歴史を持つべきであるリベースにあなたを残すでしょmaster
う。(繰り返しますが、これはとで確認できますgit diff old-master master
。マスターをgithubにプッシュするときは、履歴を書き換えたのでgit log
追加する必要があります。--force
# Push master to github, with "--force", since you've rewritten history:
$ git push --force origin master
new-master
これで、スカッシュコミットにあるを削除できます。
git branch -d new-master
どうやらgithubはgit gc --auto
プッシュで実行されるので、すぐにスペースを節約できるはずです...