The body of the

  1. Basic usage
  2. convention
  3. The command,
    1. Diff
    2. Commit
    3. Checkout
    4. Detached HEAD(Anonymous branch commit)
    5. Reset
    6. Merge
    7. Cherry Pick
    8. Rebase
  4. Technical specifications

Basic usage

The four commands above copy files between the working directory, the staging directory (also called an index), and the repository.

  • git add filesPlace the current file in the staging area.
  • git commitGenerate a snapshot for the staging area and commit.
  • git reset -- filesTo undo the last onegit add filesYou can also use itgit resetUndo all temporary region files.
  • git checkout -- filesCopies files from the staging area to the working directory to discard local changes.

You can use git reset -p, git checkout -p, or git add -p to enter interactive mode.

You can also skip the staging area and fetch files directly from the repository or commit code directly.

  • git commit -a Equivalent to runninggit addAdd all files in the current directory to the staging area before running.git commit.
  • git commit filesMake a commit that contains the last commit plus a snapshot of the files in the working directory. And the file is added to the staging area.
  • git checkout HEAD -- filesRollback to replicate last commit.

convention

Pictures are used in the following form.

The green 5-digit characters indicate the ID of the submission, pointing to the parent node. Branches are shown in orange and point to specific commits. The current branch is identified by the HEAD attached to it. This picture shows the last 5 submissions. Ed489 is the latest submission. The master branch points to the commit and the maint branch points to the grandfather commit node.

The command,

Diff

There are many ways to view changes between commits. Here are some examples.

Commit

At commit time, Git creates a new commit from the files in the staging area and sets the current node as the parent. The current branch is then directed to the new commit node. In the figure below, the current branch is Master. Before running the command, the master points to ED489, and after committing, the master points to the new node f0cec with ED489 as its parent.

Git does the same even if the current branch is the grandparent of a commit. In the figure below, 1800B is generated by a commit on the maint branch, the grandparent of the Master branch. Thus, the maint branch is no longer the grandparent of the Master branch. In this case, a merger (or derivative) is necessary.

If you want to change a commit, use git commit –amend. Git makes a new commit using the same parent as the current commit, and the old commit is cancelled.

Another example is the split HEAD commit, which I’ll talk about later.

Checkout

The checkout command is used to copy files from a historical commit (or staging area) to a working directory, and can also be used to switch branches.

When given a file name (either with the -p option turned on, or both), Git copies the file from the specified commit to the staging area and working directory. For example, git checkout HEAD~ foo.c copies foo.c from the commit node HEAD~(the parent of the current commit node) to the working directory and adds it to the staging area. (If no commit node is specified in the command, the content is copied from the staging area.) Note that the current branch does not change.

When a (local) branch is given instead of a filename, the HEAD flag is moved to that branch (that is, we “switched” to that branch), and the contents of the staging area and working directory are the same as those of the commit node corresponding to the HEAD. All files in the newly committed node (a47C3 in the image below) are copied (to staging area and working directory); Files that only existed in the old commit node (ED489) will be deleted; Files that do not belong to the above two categories will be ignored and not affected.

If you specify neither a file name nor a branch name, but a tag, a remote branch, a SHA-1 value, or something like master~3, you get an anonymous branch called the detached HEAD. This makes it easy to switch between historical versions. For example, if you want to compile git for version 1.6.6.1, you can run Git Checkout v1.6.6.1 (which is a tag, not a branch name), compile, install, and switch back to another branch, such as Git Checkout Master. However, the commit operation behaves slightly differently when it involves a “detached HEAD,” as detailed below.

HEAD identifies the commit action in the detached state

When the HEAD is detached (not attached to any branch), the commit operation can proceed normally, but no named branches are updated. (You can think of this as updating an anonymous branch.)

Once you then switch to another branch, such as master, the commit node will (probably) never be referenced again and will be discarded. Note that nothing references 2eECB after this command.

However, if you want to preserve this state, you can create a new branch with the command git checkout -b name.

Reset

The reset command points the current branch to another location and optionally changes the working directory and index. Also used to copy files from the history repository to the index without moving the working directory.

If no option is given, the current branch points to that commit. If you use –hard, the working directory is updated, and if you use –soft, it stays the same.

If the version number of the commit point is not given, HEAD is used by default. This way, the branch pointer stays the same, but the index is rolled back to the last commit, as is the working directory if you use –hard.

If you give the file name (or the -p option), it works just as well as checkout with the file name, except that the index is updated.

Merge

The merge command merges branches. Before merging, the index must be the same as the current commit. If the other branch is the currently committed grandparent, the merge command does nothing. Another case is if the current commit is a grandparent of another branch, which results in a fast-forward merge. Pointing simply moves and generates a new commit.

Otherwise it’s a real merger. The default is a three-way merge of the current commit (ED489 shown below) with another commit (33104) and their common grandparent (B325C). The result is to save the current directory and index, and then make a new commit with the parent node 33104.

Cherry Pick

The cherry-pick command “copies” a commit node and makes an identical new commit at the current branch.

Rebase

Derivatives are an alternative to merge commands. Merge merges two parent branches for a single commit. The commit history is not linear. Derivatives reenact the history of another branch on the current branch, and the commit history is linear. In essence, this is linearized and automatic cherry-pick

All of the above commands are performed on the topic branch, not the master branch, and are repeated on the master branch, pointing the branch to the new node. Note that the old commit is not referenced and will be recycled.

To limit rollback scope, use the –onto option. The following command replays the last few commits of the current branch since 169A6, namely 2C33A, on the master branch.

Git rebase — Interactive also makes it easier to do complex operations such as discard, rearrange, modify, and merge commit. Git-rebase (1)

Technical specifications

The file contents are not actually stored in the index (.git/index) or commit object, but are stored separately in the database (.git/ Objects) as bloBs and verified with sha-1 values. Index files list associated BLOB files and other data with identification codes. For commits, it is stored as a tree, again identified by the hash value of. Trees correspond to folders in the working directory, and the trees or BLOb objects contained in the tree correspond to subdirectories and files. Each commit stores the identifier of its upper tree.

If you submit with a detached HEAD, then the last lift is referenced by the reflog for HEAD. But it fails after a while and is eventually recovered, similar to git commit –amend or git rebase.


Copyright © 2010, Mark Lodato. Chinese translation © 2012, wych.

This work is adoptedCreative CC Name Identifiers – Non-commercial – Same way share 3.0 US License termsAuthorization.