Usage of git diff

Source: Internet
Author: User
Tags using git

Usage of git diff There are three major parts of the Git commit: Working tree, index file, commit

of these three parts:

Working tree:Is the directory you work in, and whenever you make changes in your code, the state of the working tree changes.
Index file: is a working tree and commit bridge, and whenever we use the Git-add command to register, the content of the index file changes, and the index file is synchronized with the working tree.
Commit: It's the last stage, only commit, and our code really goes into the git repository. We use Git-commit to commit the content in index file to commit.
To summarize:
git diff: is to see the difference between the working tree and the index file.
git diff--cached: is to see the difference between the index file and commit.
git diff HEAD: It's a look at the difference between working tree and commit. (You must not forget that the head represents the latest commit information)

To illustrate this relationship more clearly, give an example.

[Email protected]]$ cat MAIN.C
#include <stdio.h>
int main (int argc,char *argv[])
{
printf ("hello.\n");
printf ("He was a student.\n");
return 0;
}


Then git init, git Add. , Git commit;
After that you modify the source code to:

[Email protected]]$ cat MAIN.C
#include <stdio.h>
int main (int argc,char *argv[])
{
printf ("hello.\n");
printf ("He was a student.\n");
printf ("He is born in finland.\n");
return 0;
}


At this point you git add, but do not have to execute the git commit command. Then you change the source code to:
    1. [Email protected]]$ cat MAIN.C
    2. #include <stdio.h>
    3. int main (int argc,char *argv[])
    4. {
    5. printf ("hello.\n");
    6. printf ("He was a student.\n");
    7. printf ("He is born in finland.\n");
    8. printf ("He is very clever!\n");
    9. return 0;
    10. }
Copy Code
This time, you perform the following three commands, carefully review, I believe you will find them three difference!
$ git diff
$ git diff–cached
$ git diff HEAD
In this case, basically the git diff command has a more in-depth understanding, and now you use GIT status to see the output, it looks like this:

[[Email protected]]$ git status
# on Branch Master
# changes to be committed:
# (use "git reset HEAD <file> ..." to Unstage)
#
# MODIFIED:MAIN.C
#
# Changed but not updated:
# (use "git add <file> ..." To update what would be committed)
#
# MODIFIED:MAIN.C
#很明显可以知道:
changes to be committedIndicates that it already exists in index file but has not yet been committed.
Changed but not updatedIndicates that the working tree has been modified but has not yet been registered to the index file using git Add.

Well, for Git diff usage, simply review it here.

Usage of git diff

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.