I think Git is simple


I think Git is simple

gitIt is an open source distributed version control system, which can effectively and quickly deal with project version management from very small to very large. We may often use it, but do you really know git? Let’s start with a few questionsgit addWhat happened? What staging areas does git have?git pullAndgit fetchdifference?git mergeAndgit rebasedifference?git resetAndget restoredifference?

Although I’ve been using git, I don’t know the above problems, so I’ll summarize them. Original in personal blog

What is a version control system

At the beginning, Git is a distributed version control system, so what is a version control system? You can refer to the example given by Liao Xuefeng for word document operation.

takeWordDocument operation examples when you want todeleteIn a certain paragraph, I’m afraid I want it in the futurerecoveryWhat if I can’t find it? You may save onecopy。 Then modify it and save another onecopy, this continues, and there will be severalcopy

I think Git is simple

After a week, if you want to find a paragraph, you may need to find it one by one from these copies. Isn’t it very troublesome.

This is also the case when you write word alone. If your colleagues edit this word with you, in order to ensure document synchronization, you may need to keep sending and transmitting to each other (through USB flash disk, QQ wechat, etc.). In short, it’s cumbersome.

If there is such a software that can record every modification and edit with many people, is it very convenient. Git is to solve the above problems, so that we can view the content modified each time, and synchronize the modifications of others.

basic operation

  • git clone
    Generally speaking, the first step of remote operation through Git is to clone a version library from the remote host through this command
  • git fecth
    Retrieve all updates of a remote host locally
  • git pull
    Retrieve the update of a branch of the remote host and merge it with the specified branch of the local host
    The smartgit pull button has a drop-down option, which can be selected:
    Merge fetched remote changes
    Rebase local branch onto fetched changes
  • git add
    Add file. In detail, file differences in the workspace should be submitted to the staging area.
    amount tosmartgitofstage
  • git commit
    Submit documents. Commit the changes in the staging area to the local branch. Each submission produces acommit-idAnd will bring your username, email and other information.
  • git push
    Push updates from local branches to remote hosts
  • git marge
    Merge parts, multiple lines
  • git rebase <name>
    Merge a branch to the current branch, change the base derivative, and a line
  • git branch <name>
    Create branch
    Git branch – d < name > delete branch
  • git checkout
    git checkout <name>Switch branch
    git checkout -b <name>Create and switch branches.
    git checkout -- <file>Discard changes. (changes can also be discarded without adding)
    amount tosmartgitofunstage
  • git reset HEAD <file>
    Cancel adding staging area
  • git switch <name>
    Switch branches, commands provided by the new version.
    git switch -c <name>Create and switch

Borrow a picture

I think Git is simple

Work area, staging area, local warehouse, remote warehouse

The workspace is a project we clone clone, which iswork area; When executedgit addChanges will be added to a place, which isStaging areaLocal warehouse, by.gitDirectory management;Remote warehouse, is the pull address. The relationship between them is shown in the following two figures:

I think Git is simple

I think Git is simple


HEADyesgitAn internal pointer tocurrent version。 This current version contains two conceptsbranchandSubmitHEADWhich one are you responsible for pointing tobranch, andbranchPoint to a specificSubmit


I think Git is simple

Create new branch。 Git creates a branch very quickly, because there is no change in the files in the workspace except adding a dev pointer and changing the direction of head. From now on, the modification and submission of the workspace are aimed at the dev branch. For example, after a new submission, the dev pointer moves forward one step, while the master pointer remains unchanged.

I think Git is simple

Merge branch。 First switch to the master, and then merge dev into the current branch. This is to directly point the master to the current commit of dev. Git is lazy. This submission method is because git tries to adoptfast-forward(- FF) this type of merge does not create a new commit.

If there is no modification on the master, this is the case at this time. If it is modified on the master, it will be usedNo-fast-foward(- no FF), a merge submission will be created.

I think Git is simple

Delete branch。 After development, you can delete it as appropriate. To delete the dev branch is to delete the dev pointer

I think Git is simple

Animation demonstrationAs follows:

I think Git is simple

Git pull and git fetch

git pullAndgit fetchCan pull the latest code from the remote warehouse. The difference is that the update method is different.

git fetch

git fetchThe method is to pull the latest content of the remote host locally. After checking, the user decides whether to merge it into the working local branch. The latest update record will be saved in.git/FETCH_HEADFile.

git pull

git pullThe latest content of the remote host is pulled down and merged directly, which may cause conflicts and need to be solved manually.git pull = git fetch + git merge

Note, not necessarilygit mergeYou can also choosegit rebaseFor example, I use smartgit:

I think Git is simple

Git merge and git rebase

git mergeAndgit rebaseAre used to merge two-thirds. The difference is that the generated log bifurcation lines are different.

git mergeThe public branch will be merged with your current commit to form a new commit.

git rebaseIt will put the commit of your current branch at the end of the public branch, so it is called variable base. It’s like you pull this branch out of the public branch again.

Suppose you have the following branch records,

I think Git is simple

Suppose the new submission on the master branch is related to the feature you are developing. To merge the new submission into your feature branch, you have two options:mergeperhapsrebase

Git merge mode

git checkout feature
git merge master

#Or a command. The premise is that you have to be on the feature branch
git merge feature master

I think Git is simple

As shown in the figure above, this creates a new merge commit in the feature branch, which links the history of the two branches.

usemergeIt’s a good way because it’s aNon destructiveOperation. Existing branches are not changed in any way. This avoidsrebasePotential defects caused by operation.

The disadvantage is that an additional commit is generated.

Git rebase mode

git checkout feature
git rebase master

I think Git is simple

As shown in the figure above, this will change the wholefeatureBranch move tomasterThe top of the branch, which effectively integrates allmasterCommit on branch. However, withmergeDifferent submission methods,rebaseBy creating a new for each commit in the original branchcommitscomerewriteProject history.

The main benefit of rebase is a clearer project history.

Git rebase golden rule

git rebaseThe golden rule isNever use it on a public branch

What is a public branch. My understanding is tomaster dev testSuch a branch, or a branch used by more than two people, is called a public branch.

What happens when you rebase the master branch to the feature branch:

I think Git is simple

Git reset and get revert

git resetAndget revertBoth can be used for version fallback. The difference is that one can be hard reset without retaining records; One is overwrite submission, which keeps records.

Let’s assume that we have such a branch:

I think Git is simple

git reset

git resetDivided intoSoft reset(GIT reset — soft < commit ID >) andHard reset(git reset –head <commit id>)。 The default is soft reset. Generally, we use hard reset when rolling back code

git reset --hard <commit id>
git push -f

I think Git is simple

I think Git is simple

git revert

git revertRestore: by performing a restore operation on a specific submission, we will create a new submission containing the restored modifications

I think Git is simple

Git exercise

Find a good tool to understand the basic concepts and operations of GIT, GIT online. This website allows you to understand git through tasks. If you don’t want to do questions, you can directly add the parameter nodemo git online sandbox mode to verify online.

If you want to intuitively understand how the GIT command works through the way of moving graph, please refer to the CS visualization: useful git command, which I reprinted

Reference link

Git tutorial

Learn git in an hour

git rebase VS git merge? A more elegant git merge is worth having

Two methods of GIT restoring the previous version reset and revert (picture and text explanation)

Recommended Today

Summary of DOM event flow in JS

1、 Event capture 1. Concept Event capture: from the document to the node that triggered the event,Top downTo trigger the event. 2. Illustration Event capture 2、 Event bubbling 1. Concept Event bubbling: from the node that triggered the event to the document, yesBottom upTo trigger the event. 2. Illustration Event Bubbling 3、 DOM event flow […]