简体   繁体   中英

Git: How to rebase and squash “Merge remote-tracking branch” to the later commit?

I've created a pull request which has the commits as shown below. However, I am required to rebase the Merge remote-tracking branch 'original/master' into avsp commit.

git log output

commit eb4b301a88f02adccecdc09b82e4fc4a12656d34 (HEAD -> avsp, origin/avsp)
Author: Nikhil P <nikhilap@blah.com>
Date:   Thu May 2 20:26:45 2019 -0700

    Address some review comments

commit 0ba34d2145688c133152e7db1d8bf29f503ab34c
Merge: 5e7c1a31 beacedb0
Author: Nikhil P <nikhilap@blah.com>
Date:   Thu May 2 00:32:02 2019 -0700

    Merge remote-tracking branch 'original/master' into avsp

commit beacedb0b2b2e4ceb1b8b626b94f66ba592356a4
Author: Guy Harris <guy@alum.mit.edu>
Date:   Wed May 1 00:23:08 2019 -0700

    Remove the IPv6 payload length checks for checksumming.

    If there isn't an IPv6 payload, there isn't any TCP or UDP packet, and
    there's no TCP or UDP header to checksum, so there's no need for the
    check (it's not there for IPv4).

Expected git log

commit eb4b301a88f02adccecdc09b82e4fc4a12656d34 (HEAD -> avsp, origin/avsp)
Author: Nikhil P <nikhilap@blah.com>
Date:   Thu May 2 20:26:45 2019 -0700

    Address some review comments

commit beacedb0b2b2e4ceb1b8b626b94f66ba592356a4
Author: Guy Harris <guy@alum.mit.edu>
Date:   Wed May 1 00:23:08 2019 -0700

    Remove the IPv6 payload length checks for checksumming.

    If there isn't an IPv6 payload, there isn't any TCP or UDP packet, and
    there's no TCP or UDP header to checksum, so there's no need for the
    check (it's not there for IPv4).

I tried to rebase and squash the last 2 commits into 1 so I tried

git rebase -i HEAD~2

This resulted in around ~450 commits

  1 pick 111e17e8 Don't use CMAKE_C_STANDARD, it doesn't work on all versions of CMake.
  2 pick 32f8eded Initialize C_ADDITIONAL_FLAGS where we start setting it.
   ** Snipped 400 more commits here ** 
451 pick beacedb0 Remove the IPv6 payload length checks for checksumming.
452 pick eb4b301a Address some review comments

Now when try to pick the eb4b301a commit and squash the rest, I see this error below:

error: cannot 'squash' without a previous commit
You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
Or you can abort the rebase with 'git rebase --abort'.

If it is not possible to rebase, is it possible to simply delete the commit from the commit history? Would there be a side effect because of that?

1) To perform a squash operation in an interactive rebase, you need to have a parent commit of the commit being squashed. The reason being - commit being squashed gets squashed in to the parent commit :)

Hence, you can not squash the last commit as it does not have a commit to be squashed in to.

In your case, if you put

451 squash beacedb0 Remove the IPv6 payload length checks for checksumming.
452 pick eb4b301a Address some review comments

you will be able to rebase.

2) I guess you will be able to get rid of the merge commit just by performing an interactive rebase without doing any specific operation. Try doing this:

git rebase -i HEAD~2

It will show up 450+ commits because of the merge commit and present a list to pick/squash/delete.

Do not change any operation and proceed [ESC + :wq in case of Windows OS]. Now once it says Successfully rebased, check git log.

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