git
It 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 add
What happened? What staging areas does git have?git pull
Andgit fetch
difference?git merge
Andgit rebase
difference?git reset
Andget restore
difference?
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.
takeWord
Document operation examples when you want todelete
In a certain paragraph, I’m afraid I want it in the futurerecovery
What if I can’t find it? You may save onecopy
。 Then modify it and save another onecopy
, this continues, and there will be severalcopy
。
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 tosmartgit
ofstage
-
git commit
Submit documents. Commit the changes in the staging area to the local branch. Each submission produces acommit-id
And 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 branchgit checkout -b <name>
Create and switch branches.git checkout -- <file>
Discard changes. (changes can also be discarded without adding)
amount tosmartgit
ofunstage
-
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
Work area, staging area, local warehouse, remote warehouse
The workspace is a project we clone clone, which iswork area
; When executedgit add
Changes will be added to a place, which isStaging area
;Local warehouse
, by.git
Directory management;Remote warehouse
, is the pull address. The relationship between them is shown in the following two figures:
HEAD
HEAD
yesgit
An internal pointer tocurrent version
。 This current version contains two conceptsbranch
andSubmit
。 HEAD
Which one are you responsible for pointing tobranch
, andbranch
Point to a specificSubmit
。
Default
。
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.
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.
Delete branch
。 After development, you can delete it as appropriate. To delete the dev branch is to delete the dev pointer
Animation demonstration
As follows:
Git pull and git fetch
git pull
Andgit fetch
Can pull the latest code from the remote warehouse. The difference is that the update method is different.
git fetch
git fetch
The 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_HEAD
File.
git pull
git pull
The 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 merge
You can also choosegit rebase
For example, I use smartgit:
Git merge and git rebase
git merge
Andgit rebase
Are used to merge two-thirds. The difference is that the generated log bifurcation lines are different.
git merge
The public branch will be merged with your current commit to form a new commit.
git rebase
It 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,
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:merge
perhapsrebase
。
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
As shown in the figure above, this creates a new merge commit in the feature branch, which links the history of the two branches.
usemerge
It’s a good way because it’s aNon destructive
Operation. Existing branches are not changed in any way. This avoidsrebase
Potential defects caused by operation.
The disadvantage is that an additional commit is generated.
Git rebase mode
git checkout feature
git rebase master
As shown in the figure above, this will change the wholefeature
Branch move tomaster
The top of the branch, which effectively integrates allmaster
Commit on branch. However, withmerge
Different submission methods,rebase
By creating a new for each commit in the original branchcommits
comerewrite
Project history.
The main benefit of rebase is a clearer project history.
Git rebase golden rule
git rebase
The golden rule isNever use it on a public branch
。
What is a public branch. My understanding is tomaster
dev
test
Such 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:
Git reset and get revert
git reset
Andget revert
Both 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:
git reset
git reset
Divided 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
git revert
git revert
Restore: by performing a restore operation on a specific submission, we will create a new submission containing the restored modifications
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)