Git diff between staged or unstaged changes with a remote branch
Asked Answered
B

3

8

Is there a way to do a git diff between staged/unstaged changes with a remote branch say origin/branch1. Is there a similar option with git difftool?

OR

Is there a way to diff changes in my local file system (forget about staged or unstaged) with git remote branch? All I want is to check my changes before committing them. This is really useful once I am done pulling in changes and resolving conflicts with a remote branch.

Basketwork answered 1/12, 2017 at 2:56 Comment(4)
Why not commit it, and diff the commit one with remote branch which is doable.Spacesuit
Try SourcetreeDenizen
Is there a way to diff changes in my local file system (forget about staged or unstaged) with git remote branch? All I want is to check my changes before committing changes. This is really useful once I am done merging my changes with a remote branch.Basketwork
@MenglongLi she wants to see changes BEFORE committing.Psychomotor
O
19

Is there a way to do a git diff between staged changes and a remote branch say origin/branch1.

Just run:

git diff --cached origin/branch1

(you may use --staged here if you prefer; I use --cached because git rm has --cached but not --staged). This shows you a way to change origin/branch1 to match what you have staged:

$ git show origin/branch1:README
initial version
$ cat README
initial version
second version
$ echo staged >> README && git add README
$ echo replace whole thing > README
$ cat README
replace whole thing
$ git diff --cached origin/branch1
diff --git a/README b/README
index 42549ca..d9074b8 100644
--- a/README
+++ b/README
@@ -1 +1,3 @@
 initial version
+second version
+staged

If you want to see a way to change what you have staged to match origin/branch1, add -R (reverse the order):

$ git diff -R --cached origin/branch1
diff --git b/README a/README
index d9074b8..42549ca 100644
--- b/README
+++ a/README
@@ -1,3 +1 @@
 initial version
-second version
-staged

As you can see, these are very different from comparing without --cached aka --staged, but this too is easy:

$ git diff origin/branch1
diff --git a/README b/README
index 42549ca..acb8b7a 100644
--- a/README
+++ b/README
@@ -1 +1 @@
-initial version
+replace whole thing

Is there a similar option with git difftool?

These same options (--cached or not, and using any name to identify any commit) are also available with git difftool.


It's worth remembering here that there aren't really any remote branches, in Git. There are commits that you have, and you have names for some of your commits. One possible form of name is a remote-tracking name like origin/branch1. Some people like to call this a "remote branch", but it's just your own (local) Git's memory of where origin's branch1 pointed some time ago, when you ran git fetch origin and had your Git pick up their Git's branches.

Ortega answered 1/12, 2017 at 3:29 Comment(1)
Sweet! Thanks torek.Basketwork
P
0

You can use git write-tree to accomplish this. From the docs it "creates a tree object from the current index" -- basically giving a "name" to your staged contents.

Take this sample repository. The upstream contains a single file f with the contents hi

$ git diff --staged
diff --git a/f b/f
index 45b983b..f471c09 100644
--- a/f
+++ b/f
@@ -1 +1,2 @@
 hi
+hello
$ git diff
diff --git a/f b/f
index f471c09..723cb71 100644
--- a/f
+++ b/f
@@ -1,2 +1,3 @@
 hi
 hello
+unstaged

From this, you can see that a diff which adds the line "hello" is about to be staged.

There's also an unstaged diff which adds the line "unstaged"

You can compare this against a remote branch as follows:

$ git diff origin/master $(git write-tree)
diff --git a/f b/f
index 45b983b..f471c09 100644
--- a/f
+++ b/f
@@ -1 +1,2 @@
 hi
+hello

In this case, it happens to be the same as git diff --staged but if we forward the upstream branch a bit:

$ git fetch -q && git show origin/master  | tail -7
diff --git a/f b/f
index 45b983b..5f69508 100644
--- a/f
+++ b/f
@@ -1 +1,2 @@
 hi
+from upstream

Now we see the complete changes:

$ git diff origin/master $(git write-tree)
diff --git a/f b/f
index 5f69508..f471c09 100644
--- a/f
+++ b/f
@@ -1,2 +1,2 @@
 hi
-from upstream
+hello
Petasus answered 1/12, 2017 at 3:4 Comment(0)
K
0

One way to achieve this is

  • Let's say you have staged changes in one folder dir1
  • Checkout the same repo in another folder dir2, switch to the remote branch
  • Use the unix diff command to see the recursive diff of both the
    directories

diff --brief -r dir1/ dir2/

Killingsworth answered 1/12, 2017 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.