1

Say I have 100 commits in my branch that I've been working on for 3 weeks. Occasionally (every day, really) I pull from origin/master and merge it into my branch.

How could I (easily) go about squashing all of my commits into one commit without messing up history? If I squash all of my commits into one somehow, would I destroy the merged origin/master pulls when my pull request gets moved into origin/master?

1 Answer 1

8

"Squashing" and "preserving history" are approximately direct opposites in terminology.

If you mean that you want to make a single commit that includes only your changes and not the ones from upstream master, you would probably want to rebase onto origin/master and then squash from there. You could do all of this from a single invocation of interactive rebase:

git fetch origin
git rebase -i origin/master

and then change all of the lines after the first from pick to squash.

8
  • so I have my branch "foo" checked out. I run git rebase -i origin/master and I see a ton of commits.. If I squash them all, then push to master, the commits that i previously merged in and squashed are permanently gone from origin/master's history? (e.g. I git reset head back to commit before my squash commit. will everything be the same when i reset?)
    – tester
    Commented Mar 6, 2012 at 18:01
  • Do you see commits in the list that are already in master? If so, something is wrong.
    – Amber
    Commented Mar 6, 2012 at 18:08
  • yeah most of the commits that I see are things that I've merged/automerged during git pulls.. it's also in reverse order (my latest commit is at the bottom). any ideas?
    – tester
    Commented Mar 6, 2012 at 18:32
  • Latest commit being at the bottom is normal. Have you done a git fetch origin recently? Doing git pull doesn't actually automatically update the origin/master pointer.
    – Amber
    Commented Mar 6, 2012 at 18:42
  • 1
    I just added the git fetch origin bit earlier, since it's a good thing to do before starting a rebase regardless.
    – Amber
    Commented Mar 6, 2012 at 19:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.