If I run git diff
I would expect to see a list of changes of my working directory relative to whatever had been committed before (or a list of the working directory contents if it's a new repository with no commits). Try this example:
$ mkdir temp $ cd temp $ git init $ echo "first line" > test.txt $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.txt nothing added to commit but untracked files present (use "git add" to track)
Let's see a diff of test.txt:
$ git diff
This doesn't give any output!
I would expect to see a diff like + first line
, but instead I get nothing. It doesn't tell me what's going on. People on Stack Overflow tell me to git add
some files so I do:
$ git add . $ git diff
Still nothing!
Git GUI shows the changes.
git status -v
shows the changes.
But for some reason git diff
doesn't show anything.
So my questions are:
git diff
work?Some people at my company are using Git, but the SVN crowd are going to point at this as a case of where Git is too confusing to be usable.
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.
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. Order does matter when you're comparing branches.
The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository. The HEAD in the git command refers to the remote repository.
^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.
git diff
output before adding?Git does not treat files in the filesystem as automatically included in the version control system. You have to add things explicitly into the Git repository (as you are doing by adding the current directory with git add .
).
There is no output to git diff
because Git doesn't see any changes inside your repository, only files outside the repository, which it considers 'untracked' and so ignores when generating a diff.
I found this one of the key differences to version control systems like SVN (along with staging and ignoring directories).
If you want the untracked files to be included, git add
them.
If you don't want them in your repository, add them to your .gitignore
(see git ignore --help
). This is good for C object files or Python .pyc
files.
git diff
output after adding?!So this is slightly different. If you do git status
you will see the file is now in the staging area. This is the area for files that you are about to commit.
When you git add
a new file into the Git repository, it skips the working copy and goes straight into the staging area. This make sense in a way, and git add
always moves files into staging area whether it is tracked or untracked.
To see the differences between the last check in and the staging area do git diff --cached
.
To see the differences between the staging area and your working copy, do git diff
. If there is nothing in the staging area then this is the same as doing a diff between the last check in and your working copy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With