Git is an indispensable development tool for programmers to study and work, as well as a handy 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 -…
The four commands above copy files between a working directory, a staging directory (also called an index), and a 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 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 — files rollback to copy the last commit.
The convention is to use pictures in the following form.
Green 5-digit characters indicate the commit ID, one 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.
Diff has a number of ways to view changes between commits. Here are some examples.
When a Commit is committed, 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 will do 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 of how Git works is splitting a HEAD commit, which I’ll cover later.
The Checkout command is used to copy files from the history commit (or staging area) to the 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 you give a (local) branch instead of specifying a file name, the HEAD flag moves to that branch (i.e., we “switched” to that branch), and the contents of the staging area and working directory match 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.
When the HEAD is in a separate state (not attached to any branch), the commit works normally, but no named branches are updated. (You can think of this as updating an anonymous branch.)
Once you switch to another branch, such as Main, 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.
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 Git a file name (or the -p option), it works just as well as checkout with the file name, except that the index is updated.
The Merge command merges different branches together. 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. The point is simply moved and a new commit is generated.
Otherwise it’s a true merge. 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 Cherry – the Pick command “copies” a commit node and makes a new commit exactly the same 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 makes it easy to do complex operations such as discard, rearrange, modify, and merge commit.
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.