Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Compare files before commit

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

like image 701
mallows98 Avatar asked Jun 11 '12 23:06

mallows98


People also ask

How do I see diff before commit?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged . first.

How do I compare files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

What is git diff command?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


1 Answers

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.

like image 123
Karthik Bose Avatar answered Oct 02 '22 18:10

Karthik Bose