Git is not only a tool for programmers to learn and work without, but also a useful document version management tool for non-programmers.

This article illustrates the most common commands in Git. If you understand a little bit about how Git works, this article will help you understand it better.

The original link: marklodato. Making. IO/visual – git -…

Basic usage

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

  • Git add Files puts the current file in the staging area.

  • Git commit Generate and commit a snapshot for the staging area.

  • Git reset — files to undo the last git add files. You can also undo all temporary area files with git reset.

  • Git checkout — files copies files from the staging area to the working directory, which is used 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 is equivalent to running git add to add all files in the current directory to the staging area. git commit.

  • Git Commit Files makes a commit that includes 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 — files rollback to copy the 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 main branch points to this commit, and the stable branch points to the grandparent 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 main. Before running the command, main points to ED489, and after committing, main 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 stable branch, the parent of the main branch. In this way, stable is no longer the grandparent of main. 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.

Detached HEAD If you specify neither a file name nor a branch name, but a tag, remote branch, SHA-1 value, or something like main~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 version 1.6.6.1 of Git, 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 main. 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 Main, the commit node is (probably) never referenced again and is 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 in the topic branch, not the main branch, and are repeated on the main 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 main branch.

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

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.