Understand the principles of common git commands

Source: Internet
Author: User
Tags git commands

Git is different from a version management system like SVN. Although familiar with common operations, it can satisfy most of the requirements. However, it does not rely on brute force to try it in case of troubles, it is necessary to understand the principles of git.

File

All file version information managed by git is stored in the root directory.gitNext, let's take a look:

$ ls .gitCOMMIT_EDITMSG  HEAD       branches  description  index  logs     packed-refsFETCH_HEAD      ORIG_HEAD  config    hooks        info   objects  refs

In addition to some commonly used commands, GIT also provides many underlying commands that can be used to view the content expressed in the preceding files.

Three regions/three types of objects

It is very important to understand the three regions in git. Many common commands in git are built around these three regions. They are:

  • Working directory, which is the files you operate on
  • History, all records you submit, historical file content, and so on.Git is a distributed version management system. You have all historical project submission records, file history records, and submit logs locally.
  • Stage (INDEX), the temporary storage area, is essentially a file, that is.git/index

GIT also has three types of common objects (more than three). It is also important to understand these three types of objects. They are:

  • Blob, used to represent a file
  • Tree, used to represent a directory, indexed to several files or subdirectories
  • Commit, used to indicate a Commit (COMMIT)

All objects are saved as files.git/objectsDirectory, an object and a file.

Next, associate all the above content. Do the following:

$ Mkdir Test & CD test $ git init $ LS-. git/objects # No file... info pack $ touch readme # added a ReadMe file in working directory $ git add readme # added a file to the stage area $ git LS-files -- stage # This command can be viewed in the stage area content, you can see readme100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 readme $ LS-. git/objects # at the same time. git/objects adds a directory named E6... e6 info pack $ LS-. added a file under the GIT/objects/E6/# E6 directory... 9de29bb2d1d6434b8b29ae775ad8c2e48c5391

The above operation shows the partial associations of three objects in three regions of git. In git, each object is identified by a 40-character SHA-1 hash value. The first two characters of the 40 characters are used as folders, And the next 38 characters are file names.

Follow these steps:

$ Git commit-m'first commit '# commit submits the files identified in the stage to the history region [Master (root-commit) 8bf6969] First Commit 0 files changed, 0 insertions (+), 0 deletions (-) Create mode 100644 readme $ LS-. git/objects # added 2 files, that is, 2 objects... 8b E6 E8 info pack $ git LS-files -- stage # stage only indicates the files managed by the current version, so the content remains unchanged 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 readme # git cat-file command can be used. git/objects file, which can be used to view the object $ G It cat-file-T e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 # This is the file object blobblob generated by git add readme # Let's also check the two newly added objects after git commit-M $ ls. git/objects/8B/f696927c17526eb8f0c6dae8badb968a001ed0 $ git cat-file-T folder # Remember to include the 8B folder name to form a complete object ID. This is a commit object commit $ ls. Git/objects/e80ad49ace82167de62e498622d70377d913c79e $ git cat-file-T example # tree Object Tree

Description of how a region interacts with an object:

Passgit cat-file -pYou can view more descriptions of objects,git cat-file -tObtains only the object type. Do the following to gain a deeper understanding:

# This commit OBJECT records the information of the submitter, the tree object $ git cat-file-P has been directed to Kevin Lynx <[email protected]> 1410090424 + 0800 committer Kevin Lynx <[email protected]> 1410090424 + 0800 first commit # view the tree object to see the BLOB Object pointed to by the tree $ git cat-file-P ← blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 readme

Even files that have been managed by versions are used after changes (normal changes or mergers)git addTo mark it again. Create the second submission for further understanding:

$ Echo 'Hello git '> readme $ touch install $ git LS-files -- stage # Do not use git add. The content in the temporary storage area is not changed to 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 readme # The contents, prompt NO changes added to commit $ git commit # on Branch master # changed but not updated: # (use "Git add <File>... "To update what will be committed) # (use" Git checkout -- <File>... "To discard changes in working directory) # modified: readme # untracked files: # (use" Git add <File>... "To include in what will be committed) # installno changes added to commit (use" Git Add "and/or" Git commit-a ") $ git add readme $ ls. git/objects/# After git add. add the 8B 8d E6 E8 info pack $ ls file under git/objects. git/objects/8d/0e41234f24b6da002d962a26c2495ea16a0000f $ git cat-file-P objects # view the new object Hello git # You can also undo git add readme $ git reset readme # From history to stageunstaged changes after reset: M readme $ cat readmehello git $ git checkout readme # from stage to working directory $ cat readme # No content, return to the first version $ git add install # Add new files $ git LS-files -- stage # The content in stage is the latest readme and newly added install100644 latest 0 install100644 latest 0 readme $ ls. git/objects/8B 8d E6 E8 info pack

The above shows an interesting phenomenon: the newly addedinstallThe SHA-1 hash value of the file and the previousreadmeSame, because the two files are empty and the content is the same. Continue:

$ Git commit-m'second commit '$ ls. git/objects/# Add 2 objects after submission 45 72 8B 8d E6 E8 info pack $ ls. git/objects/72/b94e949c5fca6092cc74c751a7bb35ee71c283 $ git cat-file-P partition variable # the newly created tree object parent variable # The commit object has parent, the last time we submitted author Kevin Lynx <[email protected]> 1410094456 + 0800 committer Kevin Lynx <[email protected]> 1410094456 + 0800 second commit # the newly created tree object points to two file $ git cat-file-P expose blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 install100644 blob 8d0e41234f24b6da002d962a26c2495ea16a0000f readme

Note that sometimes we usegit commit -a, It will directly submit the files already added to version management together, thus skippinggit addThis process. Like many git operations, it is just a quick operation.

Summary

From the above content, we can see the advantages of git. It can complete all the tasks of a version control system without the need of servers. In the. Git file, it records all historical commits of all files, and records information of each commit.

Common git operations also involve branches and remote warehouses, and are left empty before writing.

References
  • Git ideas and basic working principles
  • Git illustration
  • Git explanation 9: git Internal principles
  • Git uses less pull and uses more fetch and merge

Address: http://codemacro.com/2014/09/09/understand-git/
Written by Kevin Lynx posted athttp: // codemacro.com

Understand the principles of common git commands

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.