1

When I'm working on a large feature that takes me few days to implement I do many small commits to my local git repository and also pull others' changes from the remote master repository. After the feature is complete, my local git repo looks somehow like:

  1. Local commit: Last step of the feature X
  2. Remote commit: B
  3. Local commit: part of the feature X
  4. Local commit: part of the feature X
  5. Remote commit: A
  6. Local commit: the first step of the feature X.
  7. ...

Now, I would like to push all local commits to the remote repo as a single commit that adds the feature X. Squashing commits with 'git rebase' won't work in this case, because remote commits not done by me (2 and 5) would also need to be squashed, which is not an option.

The only way that I managed to figure out was to have a clean branch with only remote commits, issue 'git diff' against my local development branch and apply a resulting patch onto the clean branch. Then commit patched changes and push. Is there a better way?

3
  • 1
    Sounds like you need to think about your git workflow. I usually recommend doing development in a feature branch and merging or pulling master into it only when absolutely necessary. Once the feature is ready or at least doesn't break anything, it can be merged to master. This way the history stays clean and there's no need for rebasing.
    – opqdonut
    Aug 31, 2012 at 15:36
  • But do you still need to squash intermediate local commits into one final commit or is there a way to avoid this?
    – Jan Wrobel
    Sep 1, 2012 at 8:39
  • If you really want/need to squash, you can do it. One shortcut is to use git merge --squash. But there should be need to squash in the course of normal development IMO.
    – opqdonut
    Sep 1, 2012 at 10:27

1 Answer 1

3

I found the answer: git rebase -i actually allows to reorder commits, and when all local commits are reordered to be in sequence (not interleaved with remote commits), they can be all squashed into one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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