Git those things


Git that thing

I used to only know the basic operations of GIT, such as clone, remote, pull, add, commit, push, merge, reset, stage and so on. Here I passlearngitbranchingThis website system has learned about other operations of GIT. Through this article, you can learn:

  1. What is the common workflow of GIT?
  2. How does git rebase operate?
  3. How to operate git cherry pick?
  4. What is git revert?
  5. What about git relative references?
  6. How to use git tag
  7. Git branch – F and git branch XX XXX

1. Workflow

In general, GIT workflow is divided intoGit flow workflowandRebase workflow

Git flow workflowIt means that git maintains the branches of master, development, hotfix, feature and release: new functions are created by creating a new feature branch, then merged into the development branch, and finally published to the master branch; Hotfix creates a hotfix branch, merges it into the development branch, and finally publishes it to the master branch.

Rebase workflowThis means that only the master branch is maintained in GIT. All other branches need to rebase the master branch after completion, and submit after sorting out the submission information, so that the submission information of the master branch is not messy.

Note: there are two workflowsNot mutually exclusiveYes, some companies use both workflows together.

2、 git rebase

There are two usage scenarios for git rebase. One isInteractive collation submission; One isMake git timeline smoother

##Interactive collation submission
##The following command will open an interactive VIM, and then select to do some operations for each previous commit. The common operations are reword and squash
##Head ~ 3 is a relative reference to git, which we will talk about later
git rebase -i (HEAD~3)

##Make git timeline smoother
##The following command will add the submission of the current branch to the master as a new submission
git fetch
git rebase origin master
git rebase --continue
git rebase --abort

It should be noted that:

  1. Git rebase is for commit without push, so for commitDo not easily push to the remote warehouse
  2. After git rebase opens interactive VIM,Modify with I and exit with WQ
  3. Git rebase will change git’s historical submission, so it is only applicable toYou aloneOn the branch of
  4. Git pull — rebase is equivalent to git fetch and git rebase; Git pull is equivalent to git fetch and git merge
  5. Git rebase canWith second parametergit rebase xx xxxIndicates that the base change is performed on the XXX branch based on the XX Branch, and the switch is made to the XXX branch.
  6. After git rebase, you can merge, rebase or cherry pick your branch in the master branch. For detailsLook like

3、git cherry-pick

Git cherry pick can be used to select the submission node. The command is as follows:

Git cherry pick < submission number >
Note: sometimes git cherry pick can be used to replace some functions of GIT rebase; Moreover, GIT commit — amend can also be used to merge and modify the last submission information

4、git revert

We all know that using git reset can push back the commit that has not been pushed, but what if it has been pushed? Use git revert. This command will create a new submission, which will delete the contents of the previous submission. The command is as follows:

git revert HEAD
git push

5. Git relative reference

We often see, such as head、HEAD^For commands in the form of and head ~ 3, this is a relative reference. The meaning is as follows:

Head ^: indicates the previous node of the current head. If there are 2 ^ indicates the previous node, and so on
Head ~ 3: indicates how many nodes the current head moves upward. If no number is added, it indicates one node moves upward
be careful:

Git rebase – I (head ~ 3) means rebase the first three nodes.
Git checkout master ^ 2 indicates returning to the second parent node of the master (in the order of submission time). Note that ^ here is followed by a number, indicating the number of parent nodes. If the number is not followed, it indicates the parent node with the earliest submission time.

6、git tag

Git tag can be used to label. The subsequent submission number is optional. If not, it means that the current head is labeled.

Git tag V1 < submission number >

7. Git branch high order usage

We all know that git checkout – B XXX is the abbreviation of GIT branch XXX and git checkout XXX, so git branch has two similar high-level usages:

git branch
It means to create XX Branch at XXX node, but not switch to this branch

git branch xx xxx

Indicates that the existing XX Branch is forcibly moved to the XXX node and will not be switched to this branch

git branch -f xx xxx