Why git add before git commit

Source: Internet
Author: User
Tags diff using git

1. Git add is an easy-to-query command. The SVN add action in Subversion is to add a file to version control, and git add has a completely different meaning.

At the same time, Git diff--cached is the difference between the files in the stage and a non-intuitive command.

GitHub's 2008 blog, also mentioned, is prone to confusion:
https://github.com/blog/196-gittogether-2008
http/learn.github.com/p/normal.html

Things like making use of the term ' stage ' for things this happen in the index (such as using ' git diff-staged ' instead o F ' git diff-cached ') is being worked on. I ' m excited that staging files may soon is done via ' git stage ' rather-than/in-addition-to ' git add '. This is the nice for new users who often has a hard time seeing so you have the to keep ' git add ' ing to stage your changes.

In fact, in subsequent versions of Git, two changes were made:
Git stage as a synonym for git add
git diff--staged the same command as Git diff--cached
For ease of understanding, it is recommended to use Git stage and git diff--staged both commands, and git Add and git diff--cached These two commands, just to keep up with the previous compatibility to make reservations.

2. What are the benefits of increasing the stage?
There are two main benefits, one is batch, phased submissions, one is to take a snapshot, easy to rewind
2.1 submitted in batches to reduce the granularity of commit
For example, you have modified a.py, b.py, c.py, d.py, where a.py and c.py are a function-related modification, and b.py,d.py belongs to another feature-related modification. Then you can use:
Git stage a.py c.py
Git commit-m "function 1"
Git stage b.py d.py
Git commit-m "function 2"

2.2 Phased Submissions
For example, you modified the file hello.py, modified some later, did the Git stage heello.py action, the equivalent of the current hello.py to do a snapshot, and then made some changes, this time, if directly using Git commit to submit, Only the first snapshot is submitted, and the current content is saved in the working workspace.

Current changes, you need to do a git stage again to submit.

The subtle differences between the two, see:
http/learn.github.com/p/normal.html

Because of the Git feature, it is important to note that each time you submit, you need to make sure that the relevant changes have been made to the stage, or you may just submit some incomplete changes.
For example, you changed some of the content, the stage, and then you made some changes, and then submitted, then, the subsequent changes, and did not submit.

2.3 File snapshots for easy fallback
Once you've made some changes, go to git stage, and then at any moment, you can fall back to the state of the stage:

Git checkout--hello.py

3. Git diff, git diff--staged and git diff head difference

When a file is made to the stage, and then some changes are made, then:

Git diff shows the difference between the current workspace file and the stage area file

Git diff--staged shows the difference between the stage area and head files

Git diff HEAD shows the difference between the workspace and the last file submitted

See the examples section of git help diff for details.

With git status, you can see that one file may be at the same time in two different ways. For details, see:
http/learn.github.com/p/normal.html

4. The difference between reset and checkout
When the file is added to the stage area, if you want to remove from the stage, use Reset, and the workspace file does not make any changes, such as:
git reset hello.py
This command is the inverse of git stage hello.py.

When the file joined the stage area, and then made some changes, then found that the following changes have problems, want to fall back to the state of the stage, using the checkout command:
git checkout hello.py

5. You can use the git commit-a command to skip the git stage command and submit it directly

6. Best Practices:
Do a phased modification, but can not do a single submission, then git stage a bit
If there is a problem, you can checkout back at any time
Before submitting, use Git status,git diff HEAD to carefully see if the required submission
Git commit-a, guaranteed to submit all content

Why git add before git commit

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.