Git command usage experience and git usage experience

Source: Internet
Author: User
Tags git commands

Git command usage experience and git usage experience

I recently learned about git commands to operate on the database and found that although there are not many git commands (of course not few), they are very powerful in combination, more importantly, many commands have different effects in different States. This blog post summarizes the git commands and details the usage scenarios of some commands.

Git command Summary
Command Function
Git init Initialize the current folder as the default git Library
Git add file name Add a file to the git Library
Git rm file name Delete a file from the git Library
Git status View tasks in the current database
Git diff file name View the differences between the workspace and the temporary storage area before adding.
Git commit-m "modify content" Submit the modification content to differentiate the version
Git checkout-file name Restore the file to the last delivery before adding, and restore the file to the temporary storage area before commit.
Git log [-pretty = oneline] Show what this library has done
Git relog View the command history to determine which version to return.
Git reset HEAD file name You can cancel the modification of the temporary storage area.
Git reset-hard HEAD ^ Roll back to the previous version
Git reset-hard version Roll back to a specific version

Note that if many files in a library use the same version number, after the version number is changed, all corresponding files will be rolled back;

Basic commands

Git creates a library. We modify files in the workspace, and there can be many files in the workspace;
Git add can add modified files or added files in the work area (working tree) to the temporary storage area (index file );
Git commit submits the changes in the index file to the Branch. The default partition is master;

Git diff

Diff is the meaning of difference. view the difference. Depending on the work tree, the index file, and the master, the efficacy is different -_-!
1. git diff file name: view the difference between the working tree and index file. That is to say, you can use this to see what you have changed before adding the changes;
2. git diff-cached: Check the difference between the index file and the master. After adding, if you forget what the modifications are made to all files, use this;
3. git diff HEAD file name: view the differences between the working tree and the master. After adding, you can use this to check what is changed for each file;

Example

Create a file hello. cpp in the workspace.

# Include <iostream> using namespace std; int main () {cout <"Quick Start" <endl; return 0 ;}

Then add it to the version library.

$ Git add hello. cpp $ git commit-m "new hello. cpp "[master 86a0afb] new hello. cpp 1 file changed, 7 insertions (+) create mode 100644 hello. cpp $ git status is located in the branch master and has no files to submit. It is a clean workspace.

Modify the hello. cpp file.

# Include <iostream> using namespace std; int main () {cout <"quick use of dual-stick" <endl; return 0 ;}

Add to temporary partition

$ Git add hello. cpp $ git status is located in the change to be submitted by the master of the branch: (use "git reset HEAD <file>..." to withdraw from the temporary storage area) Modify: hello. cpp

Modify the hello. cpp file.

# Include <iostream> using namespace std; int main () {cout <"quick use of dual-stick, um?" <endl; return 0 ;}

At this time, the files in the workspace, temporary storage area, and version area are different.

$ Git status: the change to be submitted by the master branch: (use "git reset HEAD <file>... "withdraw from the temporary storage area) Modify: hello. cpp has not been saved for future changes: (use "git add <file>... "update the content to be submitted) (use" git checkout -- <file>... "discard workspace changes) Modify: hello. cpp

Run the git diff command to view details.

$ Git diff hello. cppdiff -- git a/hello. cpp B/hello. cppindex 4cbc284 .. 32531fa 100644 --- a/hello. cpp ++ B/hello. cpp @-+ @ using namespace std; int main () {-cout <"quick use of dual-stick" <endl; + cout <"quick use of dual-stick, well, why? "<endl; return 0 ;}

The first row is the content in the temporary storage area, and the second row is the content in the workspace. It means that the git diff file name is compared with the content in the temporary storage area and workspace. In fact, if you use git diff-file name to achieve the same effect, the usage of-will be discussed later.

$ Git diff -- cacheddiff -- git a/hello. cpp B/hello. cppindex 4cadd9e .. 4cbc284 100644 --- a/hello. cpp ++ B/hello. cpp @-+ @ using namespace std; int main () {-cout <"Quick Start" <endl; + cout <"Quick Start with dual-stick" <endl; return 0 ;}

Row 10th is the version library content, and row 11th is the content in the temporary storage area. It indicates that git diff-cached compares the version library with the content in the temporary storage area.

$ Git diff HEAD hello. cppdiff -- git a/hello. cpp B/hello. cppindex 4cadd9e .. 32531fa 100644 --- a/hello. cpp ++ B/hello. cpp @-+ @ using namespace std; int main () {-cout <"Quick Start" <endl; + cout <"Quick Start with dual-stick, well, why? "<endl; return 0 ;}

Compare the content of the version library with the workspace;

Git checkout-file name

Case 1. git checkout-file name, which replaces the workspace with a version area file before the file is added to the temporary storage area after modification,
Case 2. After the files are added to the cache area and before the commit is reached the version area, the files in the temporary storage area are used to overwrite the workspace,

$ Git checkout -- hello. cpp $ cat hello. cpp # include <iostream> using namespace std; int main () {cout <"getting started with dual-stick" <endl; return 0;} dragon @ dragon-virtual-machine :~ /Code/learngit $

Case 3. After the file is added to the cache area, the file is modified again before the commit to the version area. If you want to overwrite the file in the version area, you can use either of the following methods:
Method 1:

$ Git checkout HEAD hello. cpp $ cat hello. cpp # include <iostream> using namespace std; int main () {cout <"Quick Start" <endl; return 0 ;} $ git status is located in the branch master and has no files to submit. It is a clean workspace.

HEAD is the meaning of the current version library. By the way, the temporary storage area is cleared.
Method 2:

$ Git reset HEAD: M hello. cpp $ git checkout -- hello. cpp $ cat hello. cpp # include <iostream> using namespace std; int main () {cout <"Quick Start" <endl; return 0 ;}

First clear the temporary storage area, then use git checkout-file name, and use version to replace the workspace, which is equivalent to case 1 .;

Git Delete and restore operations

In the work zone, we can directly run the rm command to manually delete an object, but this does not change in the version library. Therefore, if you want to delete this file from the version library. You can directly use the git rm file name without manual deletion.
Then git commit-m "is deleted ..." Submitted to the version library. However, if you do not delete the database, it can be restored.
Case 1. manually delete files that are not submitted to the temporary storage zone, use git checkout-file name, and overwrite the work zone with version files
Case 2. Delete using git rm. Not submitted yet

$ Lshehe hello. cpp readme.txt $ git rm hello. cpp rm 'Hello. cpp '$ lshehe readme.txt t $ git status is located in the change to be submitted by the branch master: (use "git reset HEAD <file>... "withdraw from the temporary storage area) Delete: hello. cpp $ git checkout HEAD hello. cpp $ lshehe hello. cpp readme.txt $ git status is located in the branch master and has no files to submit. It is a clean workspace.

Of course, you can also use the second method of git checkout.
Case 3. Submit and delete the file. You can use version rollback to restore the file, and press git reset in the above git command table.

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.