Git's Four

Source: Internet
Author: User
Tags using git version control system

1. Management modification

Git tracks and manages changes, not files, compared to other version control systems.

Why git manages changes, not files. Next do the test, we modify the Readme.txt, append a line of content:

echo "Git tracks changes." >> Readme.txt

Then add it via git add

$ git add readme.txt$ git status

Next, we modify the Readme.txt content again, and change the last line to the following:

Git tracks changes of files.

Git commit Commit

$ git commit-m "git tracks changes" [Master D4f25b6] git tracks changes1 file changed, 1 insertion (+)

Using GIT status to see the status of each operation, you can see that the second modification, git commit, is not committed.

Git management is modified, when you use the git add command, the first modification in the workspace is put into staging area, ready to commit, however, the second modification in the workspace is not put into staging area, so git commit is only responsible for staging area changes submitted, that is, the first modification was submitted, The second modification will not be submitted.

How git tracks changes, each time it is modified, and if it doesn't add to staging area, it won't be added to the commit.

2. Revocation of changes

If you add a line of files in the Readme.txt, and feel bad, you can delete the new add, restore to the original.

Git will tell you thatgit checkout-file can discard changes to the workspace:

$ git checkout--readme.txt

Command git checkout--readme.txt means to undo all changes to the Readme.txt file in the workspace, here are two things:

    • One is that Readme.txt has not been put into the staging area since the modification, and now, the undo changes back to the same state as the repository;
    • One is that the Readme.txt has been added to the staging area and modified, and now the undo changes go back to the state that was added to the staging area.

All in all, let this file go back to the state of the last git commit or git Add.

Now, look at the contents of the Readme.txt file:

$ cat Readme.txtgit is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files.

Now look at several situations, how to undo the changes

1) When you mess up the contents of a file in your workspace and want to discard the workspace changes directly, use the command git checkout-file.

2) When you have not only changed the contents of a file in the workspace, but also added to the staging area, you want to discard the changes, in two steps:

    • The first step with the command git reset HEAD file, went back to 1;
    • The second step is to press the 1 operation.

3) If you have submitted an inappropriate modification to the repository, you want to revoke this submission, refer to the version fallback section, but only if it is not pushed to the remote library.

3. Delete Files

In git, deleting is also a modification, first adding a new file Test.txt to git and committing:

$ git add test.txt$ git commit-m "add test.txt" [Master 94cdc44] Add test.txt 1 file changed, 1 insertion (+) Create mode 1 00644 Test.txt

In general, you usually delete the unused files directly in the File Manager, or delete them with the RM command:

$ RM test.txt

At this point, git knows that you deleted the file, so the workspace and repository are inconsistent, and the git status command immediately tells you which files were deleted:

$ git status...no changes added to commit (use "git add" and/or "Git Commit-a")

Now that you have two options, one is to remove the file from the repository, delete it with the command git rm and git commit:

$ git rm test.txtrm ' test.txt ' $ git commit-m "Remove test.txt"

The file is now deleted from the repository.

The other is wrong, because the repository is still there, so it is easy to restore the deleted files to the latest version:

$ git checkout--test.txt

Git checkout is actually replacing the workspace version with the version in the repository, which can be "one-click Restore", regardless of whether the workspace is modified or deleted.

Command git rm to delete a file. If a file has been submitted to the repository, then you never have to worry about accidentally deleting it, but be careful that you can only recover files to the latest version and you will lose what you modified after the last commit.

Git's Four

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.