Git Compare files before commit
Asked Answered
I

4

33

Is there a functionality in git where I could compare my local files to a git source control prior to committing changes?

Isaacs answered 11/6, 2012 at 23:30 Comment(3)
Are you trying to compare your local git repository against a remote github repository?Radio
Yes, similar to VS.NET TFS where you can do a compare of all changed files prior to checking it all in.Isaacs
possible duplicate of Git: How to view file diff before commitMilitiaman
S
65

Of-course you can do.

  1. Using git diffcommand without any arguments: will compare each modified files in your file system against the files in the current checked-out branch (or) tag.

  2. Using git diff <tag(or)branch name>: will compare each modified files in your file system against the files in the specified branch (or) tag.

  3. Using git diff <path/to/file_name (or) path/to/folder>: will compare the specified file or files in the folder in your file system against the current checked-out branch (or) tag.

  4. Using git diff <tag1(or)branch1 name> <tag2(or)branch2 name>: will compare all modified files between two branches / tags.

There are many options, you can pass to 'git diff' command to format your output. Here I've listed a few:

  • git diff --name-only : Show only names of changed files, not the contents.
  • git diff --name-status : Show only names and status of changed files.
  • git diff --cached (or --staged) : compares only the files which are staged/indexed.

for more information: execute git diff --help in your git bash.

FYI: git diff will generate output in command line. If you want to see the output in some visual tools, use git difftool.

Using git difftool: you can configure the git to use diff/merge tool to compare files. Checkout this link: use Winmerge inside of Git to file diff

You can pass all git diff arguments and options to git difftool as well.

Serous answered 12/6, 2012 at 7:21 Comment(1)
Is there a way I can do the same on Github?Ingroup
T
12

I like to use

git status

It will show you what files have changed and what you are tracking. You can then use

git diff

or the more GUI friendly

gitk

To see the diff.

Travesty answered 11/6, 2012 at 23:40 Comment(1)
Also if you want to check what has been staged (in the index) you can use git diff --cached.Wenoa
S
1

I prefer to use this method.

A script for comparing

    #!/bin/sh
(                              # execute in a subshell so you can continue
                               #   working in the current shell
    set -o xtrace              # bash setting that echos each command before it's executed
    > /tmp/auto_bcompare_log   # truncate existing log file
    BCOMP_PATH=/usr/bin/bcompare
    BRANCH="$1"                # get branch argument from command line
    TEMPDIR=`mktemp -d`        # get a temp directory
    CWD=`pwd`                  # remember the current directory
    git clone $CWD $TEMPDIR
    cd $TEMPDIR
    git checkout $BRANCH
    cd $CWD
    $BCOMP_PATH $CWD $TEMPDIR
    rm -rf $TEMPDIR
) >> /tmp/auto_bcompare_log 2>&1 < /dev/null & # background and redirect
                                               # stdout/stderr/stdin

The above script is definitely _not_ written by me. I took it from the net, but not sure of the sources. Save this in your repo as compare.sh. Make sure you give the correct path for the bcompare file.

For comparing before commit.

./compare.sh <branch_you_want_to_compare>

What this script basically does is that, it checkouts the branch that you give as a parameter to a temp directory and opens up for a comparison with your pwd. With this you can review your changes before committing. I hope this is what you want.

Sapajou answered 12/6, 2012 at 3:53 Comment(2)
Uh. How is that different to git diff <branch_you_want_to_compare>?Dupre
kostix: It'll just give you in a unified diff format. Not a clean compare like what beyond compare does.Sapajou
H
-3

Since commits are easy to amend/change before they're pushed, I find it easier to commit, and then at the log to see what I changed.

In case it's not what I'm expecting, amend, reset, rebase -i, etc...

Hasheem answered 11/6, 2012 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.