Some practical git tips


A few months ago, I wrote a form about git, called git cheat sheet. Now I want to share some useful tips. Please move on for more details. Another very good project about git tips is git tips.

Who moved my code

Who moved my code?

Whose bug is on my head?

When working in teams, such problems often occur. It can be used at this timegit blame <file>To locate the last modification of the code. However, there is a problem, which does not show the previous modification of the line code. For example, someone in the project team formats all the code,git blameIt’s not working. At this point, you can work with another useful commandgit log -p <file>In combination, you can view the change history and details of the file, and finally find out who should carry this pot.

git blame -L 10,12 package.json
git log -p -L 10,12:package.json

Fast switch merge branch

When you often work in branches a and B, you need to switch back and forth. The order should begit checkout ABut here’s a simpler command,git checkout -, indicating to cut to the latest branch. If you need to merge the contents of branch B, you can use thegit merge -

Digressioncd -It is also quite practical to enter the latest directory.

git checkout -
git merge -

Statistical items

Count the commit status of each member of the project. For example, you can check the commit number of your own project and the contribution of others to your project

git shortlog -sn
Git shortlog - Sn -- no merges does not contain a merge commit

Quick location submission

If your commit message is relatively standard, for example, it will be associated with the issue or the number of the current task or bug, then quickly locate it according to the commit message:git log --grep "Add"

If your commit message is not standardized, you can only remember which lines of code have been changed. At this time, you can also search for keywords according to the information submitted each time. Yesgit log -S "setTimeout"

At the same time, it can also help fast positioning according to the author and time.

Git log -- since = "0 am" ා view today's submissions
Git log -- author = "shfshanyu" ා view the submission of shfshanyu
Git log -- grep = "ා񖓿ා12" ා񖓿񖓿ා񖓿񖓿񖓿񖓿񖓿񖓿񖓿񖓿
Git log - s "setTimeout" ා view the submission of the switch off sub in the submitted content

Quick positioning string

How to find all the files of key words?

UseVS CodeGlobal search, usinggrepOr throughgrep -rn <keyword>To search globally.

However, they also search for ignored files, such as two famous folders in the front endnode_modulesandpublic(dist/build)。 Although grep can specify--excludeTo ignore the file, butgitIt’s more convenient.

At this time, you can usegit grep <keyword>To solve this problem, Ag can also solve this problem.

grep -rn <keyword>
grep -rn <keyword> --exclude config.js --exclude-dir node_modules
git grep <keyword>
ag <keyword>


Welcome to my official account.Mountain and moon row, recording my technology growth here, welcome to exchange

Some practical git tips