Git Cheat Sheet, git Cheat Sheet, git Cheat Sheet, Git Cheat Sheet, Git Cheat Sheet

Who’s been tampering with my code

Who touched my code?

Who’s got a bug on me?

This problem often arises when working in a team. Git Blame

can be used to locate the last change to the code. There is a problem, however, that this does not tell you about previous changes to this line of code. For example, if someone on the project team formatted all the code, git Blame would 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 -.

As an aside, CD – stands for access to the latest directory, which is also quite useful.

git checkout -
git merge -
Copy the code

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 the commit in the commit information for the replacement keyword
git log -S "setTimeout"           # View the submission of the switch machine in the submission content
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

Welcome to pay attention to my public number shanyuexixing, here records my technical growth, welcome to exchange