Git Basic Use Method

Source: Internet
Author: User
Tags how to use git using git

One of the advantages of git is that every local project is a complete repository, except that it needs to be pulled from the network and pushed to the network, and all the other operations can be completed locally. This article simply describes how to use git locally to manage the file, and the next article will take a look at the branch management.
depending on the state of the file in Git, it can be divided into three working areas:
1) Working folder
If you take Git to manage the project's source code, the working folder is a workspace. The source files can be divided into three categories depending on whether they are included in the GIT management process:
1.1) Untracked: Not included in the GIT management process
1.2) Tracked: Included in the GIT management process
There are three categories that have been incorporated into the GIT management process:
A) modified, not staged (changes not staged for commit)
b) modified, staged, uncommitted (changes to is committed)
c) submitted, unaltered (commited)
1.3) Not included in the GIT management process (defined in the. gitignore file)

2) Staging Area
Files that are not included in the GIT management process and files that are in a state in the GIT management process can be added to the staging area using the Add command:
          git add a.file

At this point, the a.file will be saved to the staging area.

3) Local Warehousefor files that have been saved in the staging area, you can use the commit command to add them to the local repository, such as the following:
          Git commit-m "commit staged Files"

Specifies the-m parameter, which can be followed by the gaze information for the submitted content.
Assuming that the-m parameter is not specified, Git jumps to a VI input interface and waits for the user to enter the gaze information before committing.

Using git to develop work on a local branch, the general process is as follows:
1) First Use GIT status to view the current working folder status:
$ git status# on branch masternothing to commit (working directory clean)

2) Add a file Test.txt, and then look at its status, such as the following:
$ Touch Test.txt
$ git status# on branch master# untracked files:#   (use "git add <file> ..." to include in what'll be committed) # #       Test.txtnothing added to commits but untracked files present (with "Git add" to track)

As you can see, in untracked files: There is a test.txt file, and the following line indicates that no files need to be committed, but there are non-tracked files (not tracked, that is, not included in the GIT management process), when the Test.txt file is in a state that is not included in the management process. In the second case, change the files that were included in the GIT management process and see their status:
$ echo "ADD content to end of the A.txt" >> a.txt
$ git status# on branch master# changes not staged for commit:#   (use "git add <file> ...." To update what would be Committed) #   (use "Git checkout--<file> ..." to discard changes in working directory) # #       Modified:   a.txt## untracked files:#   (use "git add <file> ...." To include in what'll be committed) # #       Test.txtno Change S added to commit (use "git add" and/or "Git Commit-a")

Can see that for the A.txt file, it has been included in the GIT management process, so it shows the changes not staged for commit, the state a described above). 3) Use the git add command to add it to the staging area
For files that are not included in the GIT management process, the Add command can be incorporated into the GIT management process and added to the staging area at the same time.
For files that have been included in the GIT management process, the Add command adds the changed files to the staging area.
Remember that only files that are added to the staging area can be submitted to the local repository.
$ git add test.txt
$ git status# on branch master# changes to being committed:#   (use "git reset HEAD <file> ..." to Unstage) # #       New File:   test.txt## changes not staged for commit:#   (use "git add <file> ..." To update what would be committed) #   (use "Git checkout--<file> ..." to discard changes in working directory) # #       modified:   a.txt#

Using the Add command, add the Test.txt file to the staging area (changes to is commited), and you can see that the test.txt is already in the staging area, waiting to commit, and its status is B in the above state. Similarly, use the Add command to add a.txt, such as the following:
$ git Add a.txt
$ git status# on branch master# changes to being committed:#   (use "git reset HEAD <file> ..." to Unstage) # #       Modi Fied:   a.txt#       new file:   test.txt#

As you can see, the above two files are added to the staging area. 4) Use the git commit command to save the file from the staging area to the local repository. One thing to keep in mind here is that the commit action is to save the contents of the staging area to the local repository. Files that have been modified but not yet added to the staging area will not be submitted. At the command line, enter:
Git commit

Git launches a text editor that lets the user enter the submission instructions for this time, such as the following:
# Please enter the commit message for your changes. Lines starting# with ' # ' would be ignored, and a empty message aborts the commit.# on branch master# changes to be Committ ed:#   (use "git reset HEAD <file> ...." To Unstage) # #       modified:   a.txt#       new file:   test.txt

When the user enters the submission information, the content saved in staging area will be saved to the local repository.
$ git commit[master d15838d] Commit the changes 1 files changed, 1 insertions (+), 0 deletions (-) Create mode 100644 test.t Xt

As you can see, after a successful commit, Git calculates a hash value (as above, the first few are: d15838d), as the identity value of this commit, can be viewed using git log. In addition, the ability to specify the-m parameter on the git commit command line followed by the submitted gaze information, GIT will directly submit the content specified by-m as the submission information, directly to the local repository, no longer need to edit the interface.
$ git commit-m "Add new Files"

Here, you can see the general flow of git on a local branch such as the following:
(0) in the working folder, create the file.
(1) Edit the file and use Add to enter the staging area.
(2) Use commit to save the file of the staging area to the local repository.
(3) Edit the file, use Add to add it to the staging area, back to (2) step, so loop.

For files that have been included in the GIT management process, after each change, there is a need to add (to the staging area) and commit (commit to the local repository), which is more cumbersome and allows you to specify the-a parameter directly in the Commit command, and you can run two operations in one step, such as the following:
$ git commit-am "Add and then Commit"

However, for files that are not included in the GIT management process, you cannot do this, you must first explicitly use the Add command to incorporate it into the GIT management process, save it in the staging area, and then commit.
5) Use Git rm to turn the file from tracking status to non-tracked state, i.e. no longer included in the GIT management process, such as the following:
$ git rm b.txtrm ' b.txt '

To view its status:
$ git status# on branch master# changes to being committed:#   (use "git reset HEAD <file> ..." to Unstage) # #       Dele Ted:    b.txt#

Similarly, this deletion will be saved to the staging area and submitted directly, which is actually removed from the local repository, such as the following:
$ git commit-m "delete b.txt" [Master 2daa294] Delete b.txt 0 files changed, 0 insertions (+), 0 deletions (-) Delete mode 1 00644 B.txt

At this point, the B.txt file is also deleted from the working folder.
$ lsa.txt  c.txt  d.txt  test.txt

If you just want to delete this file from the GIT management process and you want to save the file in a working folder, you need to specify the number of--cached, for example, the following:
$ git rm--cached d.txtrm ' D.txt '

View status to discover that D.txt already exists under Untracked files:
$ git status# on branch master# changes to being committed:#   (use "git reset HEAD <file> ..." to Unstage) # #       Dele Ted:    d.txt## untracked files:#   (use "git add <file> ..." to include in what'll be committed) # #       D.txt

After submitting, look at the git status:
$ git commit-m "remove d.txt from git" [Master d625705] Remove d.txt from git 1 files changed, 0 insertions (+), 1 deletion S (-) Delete mode 100644 D.txt
$ git status# on branch master# untracked files:#   (use "git add <file> ..." to include in what'll be committed) # #       D.txtnothing added to commits but untracked files present (with "Git add" to track)

Finally, you can use the log command to view the commit information multiple times, such as the following:
$ git logcommit d62570527d545ae2677708ac2f9b015aab2df86fauthor:linmiansheng <[email protected]>Date:   Tue June 14:31:10 +0800    remove d.txt from Gitcommit 2daa294a500bf9b3b0af7fe586353d882f0c3592author:linmiansheng <[email protected]>date:   Tue June 14:27:50 +0800    Delete b.txtcommit D15838d91e2c09e5e25a0a348e4dfb27c7e6b928author:linmiansheng <[email protected]>date:   Tue June 17 14:18:28 +0800    Commit the Changescommit B52882cca685b024991dec8b976c35a1cbc6c9cfauthor:linmiansheng <[email Protected]>date:   Tue June 17 14:11:13 2014 +0800

End.

Git Basic Use Method

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.