简体   繁体   中英

How to squash the old commit and a recent merge commit

When two commits were made by me one after another, I can squash them with

git rebase -i origin/{branch}~1 {branch}

But now the sequence of commits is different:

  1. My commit
  2. Merge with upstream: Merge branch 'master' of github.com:...
  3. This was pushed to origin.

Now git log and my pull request show both my original commit and the merge commit. But the above rebase command only shows other's commits that came from upstream, and no my merge commit for some reason.

So how can I squash commits if git rebase doesn't even show them?

Rebase normally removes merge commits entirely. The reason is simple enough: rebase copies commits, by turning them into changes and then applying those changes to a new base (hence the name "re-base"). Once copied, you "abandon" the original commits in favor of the shiny new copies:

... <--B <--C   <-- master
        \
         D <--E <--F   <-- branch

becomes:

... <--B <--C   <-- master
             \
              D' <--E' <--F'  <-- branch

where D' is the shiny new copy of D , and so on. In other words, rebase collects up a string of commits such as DEF , turns them into "what's different from B to D , what's different from D to E , and what's different from E to F . Git can then apply the same changes to C to make D' , then re-apply the D -vs- E changes to D' , and so on.

It's generally unwise to rebase any published (pushed) commit, unless you pre-arrange this with everyone else who shares that pushed-to repository so that they understand and will be able to coordinate with this. Essentially, they—the other people—are using the commits you're abandoning in favor of the shiny new ones; so they must also abandon those commits in favor of the shiny new ones, or else they will bring back the old / dirty / broken / whatever commits.

Merge commits have no clear way to be turned into changes, as they have two previous (or parent ) commits, rather than just one, and the changes in the merge are the result of combining the changes from each parent. (It's possible to create a new merge, but this is pretty tricky, and it's not safe to combine this with a normal interactive rebase.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM