Git graphic analysis

Source: Internet
Author: User
Tags git commands

The file content in git is not actually stored in the index (. Git/Index) Or submit objects, but store them in the database in the form of Blob (. Git/objects) And use the SHA-1 value for verification. Index files use identifiers to list related blob files and other data. For submission (TreeIs also used to identify the hash value. The tree corresponds to the folder in the working directory, and the tree or BLOB Object in the tree corresponds to the corresponding subdirectories and files. Each commit stores the identification code of its upper-level tree.

If you use the detached head to submit the job, the last commit will be referenced by the reflog for head. However, after a period of time, it will become invalid and eventually be recycled.git commit --amendOrgit rebaseVery similar.

The GIT model can be abstracted as a remote repository -- remote, a local third-level Repository: level1 -- working directory level2 -- stage (INDEX) level3 -- repository (History)
Git commands can be understood as transferring data between warehouses. Each Command is input and output to each warehouse.

Easy to remember, it can be divided into low-level input and high-level input. Note that each level is not necessarily transferred between adjacent levels. Cross-level transfer can be achieved through the Parameter options of the GIT command,

For example, common git checkout git reset git commit

  Low level input High level input
Working directory Manually created Git checkout/git stash
Stage (INDEX) Git add Git Reset
History (repository) Git commit Git pull
Remote Git push -

Based on the preceding table and the combination of git commands and various parameters, data can be transferred between warehouses,

Note that, no matter how the parameters change, except for git reset, the repositories of the above commands are determined according to the table above. The parameters and options determine the data source.

Basic usage

The above four commands copy files between the working directory, the temporary directory (also called the index), and the warehouse.

  • git add filesPut the current file into the temporary storage area.
  • git commitGenerate and submit a snapshot for the saved area.
  • git reset -- filesUsed to cancel the last timegit add filesYou can also usegit resetUndo All files in the saved area. (The operation object is head)
  • git checkout -- filesCopy the file from the temporary storage area to the working directory to discard local modifications. (The purpose is working directory)

You can usegit reset -p,git checkout -p, Orgit add -pEnter the interaction mode.

You can also skip the temporary storage area and directly Extract files from the warehouse or directly submit the code, as shown below:

 

  • git commit -a Equivalent to runningGit addAdd all files in the current directory to the temporary storage area and run the task again.Git commit.
  • git commit filesThis operation includes the last submission and the file snapshot in the working directory. Files are added to the temporary storage area.
  • git checkout HEAD -- filesRoll back to the last copy commit.

Diff

There are many ways to view the changes between two commits. The following are some examples.

Essentially, two warehouses are randomly specified in the four levels described in the preface for comparison.

4

Commit

  When submitting, Git creates a new commit with the file in the temporary storage areaAnd set the current node as the parent node. Then, point the current branch to the new submission node. The current branch isMaster. Before running the command,MasterPointEd489, After submission,MasterPoint to new nodeF0cecAndEd489As a parent node

5

InMasterBranch grandfather NodeMaintThe Branch is submitted once, And the generated1800b. In this way,MaintBranches are no longerMasterThe grandfather node of the branch. In this case, merging (or derivation) is required.

6

Usegit commit --amend. Git uses the same parent node as the current commit for a new commit, and the old commit will be canceled.

7

Checkout

The checkout command is used to copy files from historical submissions (or temporary regions) to the working directory, or to switch branches.

  When a file name is specified(Or enable the-P option, or enable both the file name and-P option), git copies the file from the specified commit to the temporary storage area and working directory. For example,git checkout HEAD~ foo.cWill submit the nodeHead ~(That is, the parent node of the currently submitted node)foo.cCopy to the working directory and add it to the temporary storage area. (If no submission node is specified in the command, the content will be copied from the saved area .)

Note that the current branch will not change (the head points to the original place ).

8

  If no file name is specifiedWhen a (local) branch is providedHeadThe ID will be moved to that branch (that is, We "Switch" to that branch), and the content in the temporary storage area and working directory willHeadThe corresponding submission nodes are consistent. All files in the new submitted node (a47c3 in) will be copied (to the temporary storage area and working directory); files only exist in the old submitted node (ed489) will be deleted; files that do not belong to the preceding two types will be ignored and will not be affected.

9

  If neither the file name nor the branch name is specifiedBut a tag, remote branch, SHA-1 value, or imageMaster ~ 3Something similar,An anonymous branch calledDetached head(SeparatedHeadID). This allows you to easily switch between previous versions. For example, if you want to compile git of version 1.6.6.1, you can rungit checkout v1.6.6.1(This is a label rather than a branch name), compile, install, and switch back to another branch, for examplegit checkout master. However, when the commit operation involves a separate head, its behavior is slightly different. For details, see below.

10

Submit operation when the head identity is in the separation status

WhenHeadWhen you are in the isolated state (not attached to any branch), The submit operation can be performed normally, but no named branches will be updated. (You can think this is updating an anonymous branch .)

11

Once you switch to another branch, for exampleMaster, Then the submission node (possibly) will no longer be referenced, and then it will be discarded. Note that there will be no reference after this command2 eecb.

12

However, if you want to save the status, you can use the commandgit checkout -b nameCreate a new branch.

13

Reset

The reset command points the current branch to another location and has the selected change working directory and index. It is also used to copy files from the historical warehouse to the index without moving the working directory.

If no option is provided, the current branch points to that commit. If you use--hardThe working directory is also updated.--softOption.

14

If the version number of the submission point is not providedHead. In this way, the branch orientation remains unchanged, but the index will be rolled back to the last commit.--hardOption, and the working directory is the same. (=== Checkout)

15

If the file name (or-pOption), the effect is similar to that of the checkout with a file name, except that the index is updated.

16

Merge

The merge command combines different branches. Before merging, the index must be the same as that currently submitted. If the other branch is the currently submitted grandfather node, the merge command will do nothing. Another scenario is that if the current commit is the grandfather node of another branchFast-forwardMerge. Point to a simple move and generate a new commit

17

Otherwise, it is a real merge. By default (Ed489) And another commit (33104) And their common grandfather nodes (B325c.

The result is to first save the current directory and index, and then work with the parent node33104Make a new commit together.

18

Cherry pick

The cherry-pick Command "copies" A commit node and performs a completely identical new commit on the current branch.

19

Rebase

Derivation is another option for merging commands. Merge the two parent branches for one commit. The commit history is not linear. The history of another branch is reproduced on the current branch, and the submission history is linear.

Essentially, this is a linear automatic cherry-pick

20

The above commands are all inTopicBranch, insteadMasterBranch, inMasterRepeat the branches and direct them to new nodes. Note that the old commit is not referenced and will be recycled.

To limit the rollback range, use--onto. The following commandMasterRepeat the current branch from169a6Recent submissions, namely2c33a.

 

Http://marklodato.github.io/visual-git-guide/index-zh-cn.html

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.