Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: generate reverse patch [duplicate]

Tags:

git

If I run git diff origin/master, it shows me the changes in my local copy of the files in the repo against the files in the master branch in the remote repository.

I know you can list another parameter, and swap the parameters to get the opposite like this:

git diff origin/branch_a origin/branch_b becomes: git diff origin/branch_b origin/branch_a

...but in my case, I want to compare with local (possibly uncommitted) changes.

Is there a way to do the opposite of git diff origin/master? So basically, the output would be the same, but instead of where it says lines were removed, it would say they were added, and vice-versa.

I know I could write a script to parse the text and reverse it, but I figured there must be a way to do it and I just don't know what it is / can't find the manual page on how to do it.

like image 663
Skeets Avatar asked Dec 13 '18 10:12

Skeets


People also ask

How do I undo a git patch?

When you then run git apply -R git will simply do the opposite to the patch. Using HEAD~3 creates a patch from a single commit for me.

How do I create a patch format in git?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.


Video Answer


1 Answers

Right: git diff commit-specifier compares the given commit's tree to the work-tree, with the commit's tree on the "left side" as a/ and the work-tree on the "right side" as b/. As you noted, it's tough to reverse these as the work-tree is implied by the lack of a second tree or commit specifier.

Fortunately, git diff has a -R option to reverse the two sides, so git diff -R commit-specifier does the trick.

like image 57
torek Avatar answered Oct 18 '22 20:10

torek