Jonas Mechtheim
Jonas Mechtheim

Reputation: 69

Rebasing commits that are identical in content, but different in hash

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?

Upvotes: 4

Views: 643

Answers (2)

Saurabh P Bhandari
Saurabh P Bhandari

Reputation: 6762

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

Upvotes: 1

Abel
Abel

Reputation: 798

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

Upvotes: 0

Related Questions