git command operation diagram, very detailed and comprehensive

Article Directory

  • What is Git?
  • The theoretical basis of Git
  • In daily development, the basic common commands of Git
  • Git advanced branch processing
  • Git advanced handling conflicts
  • Git advanced cancellation and rollback
  • Advanced Git tags
  • Some other classic Git commands

What is Git

When recalling what Git is, let’s review these concepts first~

What is version control?

Baidu Encyclopedia defines it as sauce purple~

Version control refers to the management of changes in various program codes, configuration files, and documentation during the software development process. It is one of the core ideas of software configuration management.

In those years, our graduation thesis was actually a true portrayal of version changes... Brainstorm, version control is the management of these thesis changes~

What is a centralized version control system?

So, what is a centralized version control system? To put it bluntly, there is a centralized server that saves all the revision history versions of all files, and the co-developers connect to this server through the client, from the server Update synchronously or upload your own edits.

What is a distributed version control system?

The distributed version control system is to synchronize all version information from the remote warehouse to each local user. Hehe, here are three points:

  • Users can view all the historical version information locally, but occasionally need to update it remotely, because other users may have file modifications submitted to the remote.
  • Users can submit locally even if they are offline, and only need to be connected to the remote server if they are pushed to a remote server.
  • Each user has saved the historical version, so as long as there is no problem with one user device, the data can be restored~

What is Git?

Git is a free and open source distributed version control system that can effectively and quickly handle version management from very small to very large projects.

The theoretical basis of Git

  • The four major work areas of Git
  • Git workflow
  • The four states of Git files
  • A picture explaining how Git works

The four major work areas of Git

First review several working areas of Git:

  • Workspace : Files and directories that you see locally on your computer, under Git version control, constitute a workspace.
  • Index/Stage : Temporary storage area, generally stored in the .git directory, that is, .git/index, which is also called the update area to be submitted, used to temporarily store your uncommitted changes. For example, if you execute git add, these changes will be added to this area.
  • Repository : Local warehouse, you execute the git clone address, which is to clone the remote warehouse to the local warehouse. It is a local repository, where HEAD points to the latest version put into the repository . When you execute git commit, the file changes will come to the local warehouse~
  • Remote : A remote warehouse is a warehouse similar to github, code cloud and other websites, which can be understood as a warehouse for remote data exchange~

Git workflow

The last section introduced the four major work areas of Git. In this section, I will introduce the workflow of Git. Combine the operation commands of git with several work areas. I think it’s easier to understand, haha, look at the picture:


The forward workflow of git is generally like this:

  • Pull back the file code from the remote warehouse;
  • In the working directory, add, delete, modify and check files;
  • Put the changed files into the temporary storage area;
  • Submit the files in the temporary storage area to the local warehouse;
  • Push the files in the local warehouse to the remote warehouse;

The four states of Git files

According to whether a file has been added to version control, the file status can be divided into: Tracked (tracked) and Untracked (untracked), and tracked (tracked) includes three working states: Unmodified, Modified, and Staged

  • Untracked : The file has not been added to the git repository and has not participated in version control, that is, it is not tracked. The file at this time, through the git add state, can be changed to the staged state
  • Unmodified : The file has been added to the git library, but it has not been modified yet, which means that the content of the file snapshot in the version library is completely consistent with the folder. If an Unmodified file is modified, it will become Modified. If you use git remove to remove the repository, it will become an Untracked file.
  • Modified : When the file is modified, it enters the modified state. This state of the file can enter the staged state through the stage command
  • staged : Temporary state. Execute git commit to synchronize the changes to the library. At this time, the files in the library and the local files become consistent again, and the files are in the Unmodified state.

A picture explaining how Git works

In daily development, the basic common commands of Git

  • git clone
  • git checkout -b dev
  • git add
  • git commit
  • git log
  • git diff
  • git status
  • git pull/git fetch
  • git push

This picture is just a simulation of the approximate process used by git basic commands~

git clone

When we want to develop, the first step is to clone the remote repository to the local

git clone url  克隆远程版本库

 
  
  
  • 1

git checkout -b dev

After cloning, to develop new requirements, we need to create a new development branch, such as a new development branch dev

Create a branch:

git checkout -b dev   创建开发分支dev,并切换到该分支下

 
  
  
  • 1

git add

The format of git add:

git add .	添加当前目录的所有文件到暂存区
git add [dir]	添加指定目录到暂存区,包括子目录
git add [file1]	添加指定文件到暂存区

 
  
  
  • 1
  • 2
  • 3

After we have the development branch dev, we can start development. Assuming we have finished developing HelloWorld.java, we can add it to the temporary storage area. The command is as follows

git add Hello.java  把HelloWorld.java文件添加到暂存区去

 
  
  
  • 1

git commit

The format of git commit:

git commit -m [message] 提交暂存区到仓库区,message为说明信息
git commit [file1] -m [message] 提交暂存区的指定文件到本地仓库
git commit --amend -m [message] 使用一次新的commit,替代上一次提交

 
  
  
  • 1
  • 2
  • 3

After adding the HelloWorld.java file to the temporary storage area, we can then submit it to the local warehouse~

git commit -m 'helloworld开发'

 
  
  
  • 1

git status

git status, which means to view the status of the workspace, use the command format:

git status  查看当前工作区暂存区变动
git status -s  查看当前工作区暂存区变动,概要信息
git status  --show-stash 查询工作区中是否有stash(暂存的文件)

 
  
  
  • 1
  • 2
  • 3

When you forget whether the code file has been added to the temporary storage area or whether it has been submitted to the local warehouse, you can use git status to check it~

git log

git log, this command should be used more, which means to view the commit history/commit log~

git log  查看提交历史
git log --oneline 以精简模式显示查看提交历史
git log -p <file> 查看指定文件的提交历史
git blame <file> 一列表方式查看指定文件的提交历史

 
  
  
  • 1
  • 2
  • 3
  • 4

Hee hee, take a look at the commit history on the dev branch. If you want to roll back the code, use it often. Meow commit history

git diff

git diff 显示暂存区和工作区的差异
git diff filepath   filepath路径文件中,工作区与暂存区的比较差异
git diff HEAD filepath 工作区与HEAD ( 当前工作分支)的比较差异
git diff branchName filepath 当前分支的文件与branchName分支的文件的比较差异
git diff commitId filepath 与某一次提交的比较差异

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

If you want to compare what you have changed, you can use git diff to compare the file modification differences.

git pull/git fetch

git pull  拉取远程仓库所有分支更新并合并到本地分支。
git pull origin master 将远程master分支合并到当前本地master分支
git pull origin master:master 将远程master分支合并到当前本地master分支,冒号后面表示本地分支

git fetch --all pulls the latest code of all
remotes git fetch origin master pulls the latest remote master branch code

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

We usually use git pull to pull the latest code to see, resolve the conflict, and then push the code to the remote warehouse.

Some partners may wonder whether to use git pull or git fetch. In fact,
git pull = git fetch+ git merge. In the case of pull, pull the remote branch and merge it with the local branch. Fetch only pulls the remote branch. You can choose how to merge it yourself.

git push

git push can push local branches and tags to remote warehouses, or delete remote branches.

git push origin master 将本地分支的更新全部推送到远程仓库master分支。
git push origin -d <branchname>   删除远程branchname分支
git push --tags 推送所有标签

 
  
  
  • 1
  • 2
  • 3

If we finish developing in dev, or want to push the files to a remote warehouse for other partners to see, we can use git push origin dev~

Git advanced branch processing

Git generally has multiple branches, such as development branches, regression test branches and trunk branches, so the commands for Git branch processing also need to be familiar~

  • git branch
  • git checkout
  • go merge

git branch

There are many uses for git branch, such as creating new branches, viewing branches, deleting branches, etc.

New branch:

git checkout -b dev2  新建一个分支,并且切换到新的分支dev2
git branch dev2 新建一个分支,但是仍停留在原来分支

 
  
  
  • 1
  • 2

View branch:

git branch    查看本地所有的分支
git branch -r  查看所有远程的分支
git branch -a  查看所有远程分支和本地分支

 
  
  
  • 1
  • 2
  • 3

Delete branch:

git branch -D <branchname>  删除本地branchname分支

 
  
  
  • 1

git checkout

Switch branches:

git checkout master 切换到master分支

 
  
  
  • 1

go merge

Before we release the development and testing of the development branch dev, we generally need to merge the development branch dev code into the master, so git merge is also a must-have command for programmers.

git merge master  在当前分支上合并master分支过来
git merge --no-ff origin/dev  在当前分支上合并远程分支dev
git merge --abort 终止本次merge,并回到merge前的状态

 
  
  
  • 1
  • 2
  • 3

For example, after you have developed the requirements, you need to merge the code to the main master branch for the release, as follows:

Git advanced handling conflicts

Git version control is still done by multiple people, and multiple branches coexist, so conflicts will inevitably occur~

Git merges branches, conflicts occur

When merging branches in the same file, if the same line is modified by multiple branches or different people, conflicts will occur during the merge.

For example, we are now in the dev branch and modify the HelloWorld.java file. Assuming that the third line is modified, and commit is submitted to the local warehouse, the modification content is as follows:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello,捡田螺的小男孩!");
    }
}

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

We switch back to the master branch and also modify the contents of the same location in HelloWorld.java as follows:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello,jay!!");
    }
}

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

Then, we submit this change of the master branch, and merge the dev branch, there will be conflicts, as shown in the figure:

Git conflict resolution

The Git conflict resolution steps are as follows:

  • View the content of conflicting files
  • Determine which parts of conflicting content to keep, modify the file
  • Resubmit, done

1. View the content of the conflicting file

After git merge prompts the conflict, we switch to the corresponding file to see the content of the conflict, as follows:

2. Determine which parts of the conflicting content to keep and modify the file

  • Git uses <<<<<<<, =======, >>>>>>> to mark the content of different branches,
  • <<<<<<<HEAD refers to the content modified on the main branch, >>>>>>> dev refers to the content modified on the dev branch

So, we decide which branch content to keep, or both branches to keep, and then modify the file conflict content~

3. After modifying the content of the conflict file, we resubmit it and the conflict is done

Git advanced cancellation and rollback

Git's undo and rollback are frequently used in daily work. For example, if we want to undo a modified file to the previous version, or want to undo a redundant submission, we must use git undo and rollback operations.

Which commands are used to undo or roll back the code in each work area of ​​Git, as shown in the following figure:

Regarding the undo and rollback of Git, generally the following core commands

  • git checkout
  • git reset
  • git revert

git checkout

If the file is still in the workspace and has not been added to the staging area, you can use git checkout to undo

git checkout [file]  丢弃某个文件file
git checkout .  丢弃所有文件

 
  
  
  • 1
  • 2

The following demo uses git checkout-test.txt to undo the modification of the temporary storage area test.txt

git reset

Understanding of git reset

The function of git reset is to modify the position of HEAD, that is, to change the position pointed to by HEAD to a previously existing version.

In order to better understand git reset, let’s review the version management of Git and the understanding of HEAD

All Git commits will be connected into a timeline, this is the branch. If the current branch is the master, the HEAD pointer generally points to the current branch, as follows:

Suppose you execute git reset and roll back to version two, and version three is gone, as follows:

Use of git reset

Several usage modes of Git Reset

git reset HEAD --file
回退暂存区里的某个文件,回退到当前版本工作区状态
git reset –-soft 目标版本号 可以把版本库上的提交回退到暂存区,修改记录保留
git reset –-mixed 目标版本号 可以把版本库上的提交回退到工作区,修改记录保留
git reset –-hard  可以把版本库上的提交彻底回退,修改的记录全部revert。

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

Let’s take a look at a millet demo first, the code git add to the temporary storage area, without commit submission , just the following sauce purple rollback as follows:

git reset HEAD file 取消暂存
git checkout file 撤销修改

 
  
  
  • 1
  • 2

Let's look at another millet, the code has been git commit, but it has not been pushed yet:

git log  获取到想要回退的commit_id
git reset --hard commit_id  想回到过去,回到过去的commit_id

 
  
  
  • 1
  • 2

If the code has been pushed to the remote warehouse, you can also use reset to roll back (here you can do it yourself)~

git log
git reset --hard commit_id
git push origin HEAD --force

 
  
  
  • 1
  • 2
  • 3

git revert

Unlike git reset, revert copies the historical version you want to roll back to and adds it to the forefront of the current branch.

Before

revert: After revert:

Of course, if the code has been pushed to the remote, you can also consider revert and rollback.

git log  得到你需要回退一次提交的commit id
git revert -n <commit_id>  撤销指定的版本,撤销也会作为一次提交进行保存

 
  
  
  • 1
  • 2

Advanced Git tags

Tagging means to mark the released version with a version number. If there is a problem with the release, pull the version out, fix the bug, and go back again.

git tag  列出所有tag
git tag [tag] 新建一个tag在当前commit
git tag [tag] [commit] 新建一个tag在指定commit
git tag -d [tag] 删除本地tag
git push origin [tag] 推送tag到远程
git show [tag] 查看tag
git checkout -b [branch] [tag] 新建一个分支,指向某个tag

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Some other classic Git commands

git rebase

Rebase, also known as rebase, is another option for merging.

Suppose there are two branches master and test

      D---E test
      /
 A---B---C---F--- master

 
  
  
  • 1
  • 2
  • 3

The result of executing git merge test

       D--------E
      /          \
 A---B---C---F----G---   test, master

 
  
  
  • 1
  • 2
  • 3

Execute git rebase test and get the result

A---B---D---E---C‘---F‘---   test, master

 
  
  
  • 1

The benefits of rebase are: to obtain a more elegant submission tree, you can see each submission linearly, and no more submission nodes are added. So many times, I see that some partners pull the code with this command: git pull --rebase

git stash

The stash command can be used to temporarily save and restore changes

git stash  把当前的工作隐藏起来 等以后恢复现场后继续工作
git stash list 显示保存的工作进度列表
git stash pop stash@{num} 恢复工作进度到工作区
git stash show :显示做了哪些改动
git stash drop stash@{num} :删除一条保存的工作进度
git stash clear 删除所有缓存的stash。

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

go reflog

Show the last few commits of the current branch

git blame filepath

git blame records the change history and the person who changed a file, you can view the person who is behind it, haha

git remote

git remote   查看关联的远程仓库的名称
git remote add url   添加一个远程仓库
git remote show [remote] 显示某个远程仓库的信息

 
  
  
  • 1
  • 2
  • 3

Reference and thanks

Thank you for your article:

Guess you like

Origin blog.csdn.net/yang520java/article/details/107565943