1

Simply put, these seven points:

  • Use git rebase to make commits more readable
  • Use git reflog + git reset to jump to any commit
  • Use git cherry-pick to get the specified commit
  • Change commits with git commit --amend
  • Use git revert to roll back a commit
  • Staging files with git stash
  • Configure git alias to improve work efficiency

Use git rebase to make commits more readable

Basic usage of rebase

Rebase is translated into rebase, which is similar to merge and is used to merge the modifications of a branch into the current branch

As shown in the figure below, the changes in the commit history after rebase

rebase

If you don't understand the benefits of a single branch, you can look at this question on Zhihu: How does the history of Git commits be so refreshing?

You Yuxi, the author of Vue, said: use rebase more often

Specific usage:

  • Create a feature branch based on the master branch
  • Develop feature points on feature branches
  • A commit was also submitted on master
  • Execute git rebase master on the feature branch, which means that each change of the feature is applied one by one using the last commit of the master branch as the base point

git rebase vs git merge

There are two types of merge branches, namely rebase and merge

Merge translates to merge, ie git merge branchname , that is, merge branch code, this method will save each commit, when you use gitk to view, you will find several colored lines

The other is rebase, which removes a series of commits, "duplicates" them, and puts them down one by one in another place

So the advantage of rebase is clear, it can create a clearer commit record

But merge will keep the history time of all your commits, when there are more developers, the history will become messy

Interactive mode for rebase

In development, many invalid commits are usually generated on a branch. In this case, using the interactive mode of rebase can compress multiple commits into one commit and get a clean commit history

 # 先看提交
git log 
# f9f6f3b commit 3
# 2feb45f commit 2
# 07a3cb6 commit 1
# 我们要修改 2 的话,rebase 到它的下一个 commit,这里是 1
git rebase 07a3cb6 -i
# 然后在打开的对话框里面修改,之后还要一个 rebase continue
git rebase -i <base-commit>
# 或者是 git rebase -i HEAD~2 对最近的两次 commit 进行合并
Some people also call it the regret medicine function, that is, no matter what commit you write, you can modify it at the end, and no matter what you submit, you can merge it, and it is highly DIY.

Use git reflog + git reset to jump to any commit

In other words, it is called a time machine, that is, by finding all operation records of all branches (including deleted commit records and reset operations), jump to the specified commit through reset HEAD

 git reflog
#afa2f45 HEAD@{10}: checkout: moving from 今天 to 明天
#4abcda5 HEAD@{11}: commit: 打通1800处仙窍
#de42069 HEAD@{12}: commit: 真言轮经大成
git reset HEAD@{10}
# 或者 git reset --hard afa2f45

In this way, I went back to the afa2f45 commit. Anyone who is familiar with the "law of time" and "time machine" knows that this is going back to the past.

Use git cherry-pick to get the specified commit

Means "picking" commits. Unlike merge, which merges all commits of a branch, it takes a single commit of a branch and attaches it to the current branch as a new commit.

This requires a story background to be easy to understand

Zhang San develops functions on the branch, submits one commit for each function point, and submits six function points in total (feature1~feature6 respectively), and then returns to the first commit point, that is, he uses git reset --hard feature1 Jump to the first commit, and develop a new feature on this basis, that is, feature7, then what if you merge feature7 into feature6?

 git reflog
# git reflog 查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)
# 找到 feature7 的 commit 4c97ff3
# 回到 feature6 的 commit cd52afc
git reset --hard cd52afc
# 使用 cherry-pick 拿到 feature7 的代码
git cherry-pick 4c97ff3

For details, see this little tadpole biography of the little tadpole: git time shuttle - the side face of the goddess to experience one or two

Simply put, each of your commits is a record that can be merged anywhere. Therefore, when developing function points or fixing bugs, try to achieve one function point and one commit, so as to facilitate the selection of code when errors occur.

Change commits with git commit --amend

amend means to amend

 # 继续改动你的文件
git add . 
git commit --amend --no-edit
# 你这次的改动会被添加进最近一次的 commit 中

Merged into last commit

git commit --amend : pop up to let you modify the content

git commit --amend --no-edit : keep the last commit content

PS: If your code has been pushed, use it with caution, because the commit history will be modified.

Use git revert to roll back a commit

A rollback operation is mentioned above: git reset --hard xxx , which can go back to a certain commit, in addition to that, there is another one that can revoke a certain commit

 # 先找到你想撤销的那个 commit hash值
git log
git revert <commit-id>

This approach will create a new commit message to undo the previous changes.

And git reset will directly submit the record back to the specified commit.

So in terms of personal development or personal feature branches, you can use git reset to roll back the code, but on an integrated branch with multi-person collaboration, git revert is more suitable. This way, the history of the commits is not wiped and can be safely withdrawn

Staging files with git stash

As the name implies, it is to temporarily store local changes.

First understand the four working areas of git

Four working areas

Git工作区

  • Workspace: files and directories seen by the local computer
  • Index/Stage (temporary storage area): Generally stored in the .git directory, when you git add 改动文件 , the changed files are placed in the "temporary storage area"
  • Respository (local repository): When you git clone 地址 , clone the remote repository to the local repository. 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 go to the local repository
  • Remote (remote warehouse): similar to Github, Gitlab, code cloud, etc. on the code hosting platform

A common scenario is when you are still developing a feature point, and suddenly there is an online bug that needs to be fixed urgently. This time, you can submit it to the local repository via git commit, and then pass git commit --amend and continue to modify it on the original commit. content. But there is another way here, that is to store the code in the temporary storage area, and then take it out from the temporary storage area after the bug is fixed.

The basic commands are as follows:

 git stash # 将本地的改动暂存
git stash save "message" # 执行存储时,添加备注
git stash pop # 应用最近一次暂存,并删除暂存记录
git stash apply #恢复最近的存储,但不会把存储从存储列表中删除,某人使用第一个存储,即 stash@{0},如果要使用其他,git stash apply stash@{$num}
git stash list # 查看 stash 了哪些存储
git stash clear #删除所有缓存的 stash
git ls-files --stage #查看 index 暂存区

Configure git alias to improve work efficiency

Mainly to simplify the command, its basic usage is git config --global alias.<简化的字符> 原始命令

Such as the following example:

 git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

Of course, another way is to set in the .gitconfig file

 [alias]
st = status -sb
co = checkout
br = branch
mg = merge
ci = commit
ds = diff --staged
dt = difftool
mt = mergetool
last = log -1 HEAD
latest = for-each-ref --sort=-committerdate --format=\"%(committername)@%(refname:short) [%(committerdate:short)] %(contents)\"
ls = log --pretty=format:\"%C(yellow)%h %C(blue)%ad %C(red)%d %C(reset)%s %C(green)[%cn]\" --decorate --date=short
hist = log --pretty=format:\"%C(yellow)%h %C(red)%d %C(reset)%s %C(green)[%an] %C(blue)%ad\" --topo-order --graph --date=short
type = cat-file -t
dump = cat-file -p
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Refer to the configuration of Zhengcaiyun

besides

There are also some uncommon but useful commands

  • gitk : Open the graphical tool of git
  • gitjk : undo what you just did in git
  • git help -g : Display help information
  • cat .git/HEAD : View branch file
  • git fetch --all && git reset --hard origin/master : Return to the state of the remote warehouse

    • Abandon all local changes and return to the state of the remote warehouse
  • git push -f origin master : forcibly overwrite the remote master

References


山头人汉波
394 声望555 粉丝