3

In a project I work on there's a weird system in which I can essentially feed in new commits to be appended to master. Instead of commiting the patches as they are put in, they're slightly modified in its metadata (the content is 100% identical though). Unfortunately, this means that my local commit IDs do not match up with the remote once they've gone through the system. I.e., imagine I have a local copy of the remote repository with three commits:

C1 -> C2 -> C3

Now I locally create three new commits locally, X4 to X6

C1 -> C2 -> C3 -> X4 -> X5 -> X6

I then submit X4 into the system, which is changed into C4 (content identical, hash different). That means the remote now has

C1 -> C2 -> C3 -> C4

and my local one

C1 -> C2 -> C3 -> X4 -> X5 -> X6

I would want to pull from the remote C4, drop my local X4 and append X5 to `C4, so that I locally get:

C1 -> C2 -> C3 -> C4 -> X5 -> X6

Right now I'm doing this by locally running git format-patch, then git reset --hard origin/master and git pull, then git am with only X5 and X6. It's an annoying and tedious process, is there a better way?

  • I have updated the answer to remove the optional step – Saurabh P Bhandari Jan 09 '20 at 13:06
  • Note that if the diff from C3 to C4 matches the diff from C3 to X4, `git rebase` will omit C4 on its own. If it's *not* doing that, the snapshot in X4 must differ from that in C4, enough so that the two `git diff`s produce different `git patch-id`s. – torek Jan 09 '20 at 17:36

2 Answers2

1

Local Repo

C1---C2---C3---X4---X5---X6 (master)

Remote Repo

C1---C2---C3---C4 (origin/master)

Fetch the changes from remote (origin)

git fetch origin

Local Repo

C1---C2---C3---X4---X5---X6 (master)
           \
            C4 (origin/master)

Rebase current branch (master) onto C4 and leave out X4

git rebase --onto C4 X4

Local Repo

C1---C2---C3---C4---X5---X6 (master)
                \
                 (origin/master)

More info on rebase onto

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
0

An interactive rebase may do the trick.

You should be able to do a git pull --rebase=i
In the interactive UI, to discard your X4 commit replace "pick" by "drop" or "d".

rebase doc : https://git-scm.com/book/en/v2/Git-Branching-Rebasing

Abel
  • 688
  • 6
  • 19