简体   繁体   中英

Compare git branch with rebased branch

I rebased a fairly old topic branch onto master. Since there were quite a few conflicts during the rebase, I'd like to compare the old topic branch with the rebased one, to make sure I didn't accidentally remove or screw up any of the changes on topic. The closest I've gotten to this is diffing the results of git diff master...topic and git diff master...topic-rebased . This kind of works, but there's a lot of noise in the final diff from changes in the context code, line numbers, commit hashes, etc, in addition to it not being a very robust solution. Is there an easier way to do this?

I was struggling with this same issue and came up with similar ideas as Ryan and Adam Dymitruk and found them not very satisfactory: Comparing the final diff is tricky and also doesn't show you where the "error" was introduced if you find it.

My current rebase workflow includes comparing each rebased commit with original one, so I can spot and correct potential errors as they appear and don't have to redo the rebase. I'm using the following pair of git alias to facilitate this:

rc = !git diff -w $(cat .git/rebase-merge/stopped-sha) > .git/rebase-merge/current-diff
rd = !git diff -w $(cat .git/rebase-merge/stopped-sha) | diff --suppress-common-lines .git/rebase-merge/current-diff - | cut -b 1-2 --complement | less

git rc stores the diff between HEAD the latest revision from the branch that is being rebased. After replaying the following commit, git rd compares this stored diff to the diff between the new HEAD and the next commit on the branch being rebased. Therefore this shows you only the difference ("error") introduces by replaying this last commit.

After inspecting the diff, call git rc to update the stored diff and continue the rebase.

Instead of manually calling git rc and git rd you could even add them to your git-rebase-todo so they're called automatically after each commit is replayed.

You probably would want to diff the effective changes (patches) produced by each:

diff <(git log master..topic -p) <(git log master..old-place-of-topic -p)

This would effectively remove any changes introduced in master.

If you don't care about the commit history from your topic branch you could redo the rebase and add the --squash flag. This will give you a single commit on top of your master branch where you can and go through the changed files file by file. I would also add the flag --no-commit to the rebase so that I could review the changes prior to committing the git rebase.

git checkout master
git rebase --squash --no-commit topic
//review changes with your favourite git tool
git commit

If you don't want to redo the rebase an external diff tools like KDiff3 might help you.

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