Some practical git tips
Article address: https://juejin.im/post/5cfe63f4f265da1bd522c437
A few months ago, I wrote a table about git, called Git Cheat Sheet . Now I am sharing some useful tips. For more content, please move here. There is also a very good project git-tips about git tips .
Who moved my code
Who moved my code?
Whose bug pointed to me?
When working in a team, such problems often arise. This time can be used git blame <file>
to locate the last modification of the code. However, there is a problem, which does not reveal the previous modification of this line of code. For example, if someone in the project team formatted all the code, git blame
it would be useless. At this point, it can be combined with another useful command git log -p <file>
to view the change history and details of the file, and finally find out who should carry the pot.
git blame -L 10,12 package.json
git log -p -L 10,12:package.json
复制代码
git view commit submission record details
git log
git show 79fb389692338ae50e67c690f825fe8c0e04057d
Quickly switch merge branches
When you often work in the A and B branches, you need to switch back and forth. At this time the command should be git checkout A
, but there is a simpler command, git checkout -
which means to switch to the nearest branch. If you need to merge the contents of the B branch, you can use it git merge -
.
Off topic, it means cd -
entering the recent catalog, which is also quite practical.
git checkout -
git merge -
复制代码
Statistics project
Count the commit status of each member of the project, for example, you can check the number of commits of your own project and the number of contributions of others to your project
git shortlog -sn
git shortlog -sn --no-merges # 不包含 merge commit
复制代码
Quickly locate and submit
If you commit message fairly standard, such as would be associated with the current task or bug issuse or number, then quickly locate According commit message: git log --grep "Add"
.
If your commit message is not standardized, just remember which lines of code have been changed. At this time, you can also search for keywords based on the information submitted each time, yes git log -S "setTimeout"
.
At the same time, you can also assist in quick positioning based on the author and time.
git log --since="0 am"     # 查看今日的提交
git log --author="shfshanyue" # 查看 shfshanyue 的提交
git log --grep="#12" # 查找提交信息中包换关键字的提交
git log -S "setTimeout" # 查看提交内容中包换关机子的提交
复制代码
Quickly locate a string
How to find all files that include replacement keywords?
Use VS Code
it to search globally, or use grep
it grep -rn <keyword>
to search globally.
However, they will also search for ignored files, such as the two famous folders node_modules
and the front end public(dist/build)
. Although grep can be specified --exclude
to ignore files, it git
is more convenient.
At this time, you can use git grep <keyword>
to solve this problem, and ag can also solve this problem.
grep -rn <keyword>
grep -rn <keyword> --exclude config.js --exclude-dir node_modules
git grep <keyword>
ag <keyword>
复制代码