“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

A few months ago, I wrote a git Cheat Sheet. Here are some useful tips. Git-tips is a great project about git tips.

Who’s been tampering with my code

Who touched my code?

And whose bug is pointing at me?

When working as a team, I believe that such problems have become commonplace and common. Git Blame

to locate the last change to the code.

There is a problem, however, that this can only view the body commit of this line of code, not locate the commit history of this line of code. For example, if someone formats the entire code in a project collaboration, Git Blame will lose its effect. Git log -p

can be used in conjunction with git log -p

to view the history and details of the changes to the file.

Git blame -l 10,12 package.json gitlog10, 12 - p - L: package. JsonCopy the code

Quick toggle merge branches

When you often work on both A and B branches, you cut back and forth. The command should be git checkout A, but there is A simpler command called git checkout -, which means to cut to the nearest branch. Git merge – if you need to merge the contents of the B branch, you can use Git merge -.

git checkout -
git merge -
Copy the code

And – often represents the most recent, such as CD – represents access to the most recent directory, is also quite useful.

Statistics project

For example, you can look at the number of commits for your own project and the number of contributions others have made to your project

git shortlog -sn
git shortlog -sn --no-merges      # Merge COMMIT not included
Copy the code

Quick location submission

Git log –grep “Add” if your commit message is canonical, such as the issuse or current task or bug number associated with it.

Git log -s “setTimeout” : Git log -s “setTimeout” : Git log -s “setTimeout”

At the same time, can also be based on the author, time to assist fast positioning.

git log --since="0 am"         # View today's submission
git log --author="shfshanyue"     # View the shfshanyue submission
git log --grep="# 12"              Find commits that contain keywords in the commit information
git log -S "setTimeout"           # view submissions that contain keywords in the submissions
Copy the code

Quick position string

How do I find all files that replace keywords?

A global search can be performed using VS Code, grep, or grep -rn

.

However, they also search for ignored files, such as the two well-known front-end folders node_modules and public(dist/ Build). While grep can specify –exclude to ignore files, git is more convenient.

You can use git grep

to solve the problem, and ag can also solve the problem.

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