Today we will introduce git Two of them are very, very useful tools ,git show and reflog.
Although these two orders are not necessarily known, they will , But if you use it skillfully, it can greatly help us to look at the problems in the code warehouse , And save us when we're wrong . It can be understood as emergency skills , Not normally , The kind that can save your life if you do .
First of all, let's talk about git show, It solves a very common problem in our daily development scenarios , Is to look back at a submitted content . We go through git log I can only read commit When you enter the prompt . But some developers don't follow the rules , Or the hint is very simple , It's hard for us to get To the content of it , Still need to be manual checkout Look at the content in the past , This is very inconvenient .
And with git show After the tool , We can go straight through git show command See the changes at the code level .
git show Receive a parameter , Specific commit id, such as ：
git show 40a91193d
that git It will automatically display this commit All the changes under ：
It's easy for everyone to understand , But there's a small problem , It's how we know the submission we're going to see id Well ？ So we We need to find out commit id, can git show, Otherwise, it's blind show It takes a lot of time .
lookup commit id, We introduced , I'd like to briefly mention . In general, there are two parameters that are very useful , The first is --stat Parameters , It can list every one of them commit Changes specific to the file level ：
The second parameter is pretty=oneline, It can be git Is compressed into a single line , It is convenient for us to quickly find the notes when submitting .
We introduced , stay git Inside of the implementation , A branch is just a pointer to a node . So we can also use git show + Branch name To see the changes in a branch , The changes found in this way are from the last commit node of this branch .
such as ：
git show test
What it shows is test The last committed change in this branch , If we want to see test What about the change of the penultimate node of the branch ？ This requires us to use Ancestors quote 了 . We've also introduced , We have two ways to represent the ancestor of a node . The first way is to use ^ This symbol , it Represents the parent node of a node . This symbol is repeatable , For example, we have to choose the third node from the bottom , I can write this as test^^.
The second way is to use ~, It and ^ It means the same thing , Same as parent node , But it can be back A number is nested with a number . for instance , such as test^^ It is equivalent to test~2.
So for example, we want to see test Commit of the last node of the branch , We just need to do this ：
git show test~3
The second feature we're going to introduce today is called reflog, This is a very, very useful feature , Saved my life many times .
reflog yes reference log Abbreviation , It means reference log , it Will record HEAD The history of the node and the branch it refers to . We can use git reflog Command to view the reference log ：
A brief explanation of this displayed log , Its leftmost column shows commit id. Its second column represents the branch , Which branch does the submission belong to . The third column shows the relative position , That is, it is relative to the present HEAD The position of the pointer . The last column is information , it The record is HEAD When the pointer moves .
When we use reset Or is it checkout Wait for the order , Will leave a record here . When we want to go back , We can go through reflog Quickly find the right place .
This often appears , For example, we checkout Went to a past node and looked at some code , Or use it reset Past nodes . When you've checked out and want to go back , You'll find you can't go back . Use git log Will only show the log before , because git log In order to HEAD Node output forward . So at this time, we can only check reflog, Find us checkout Over here commit id, And then through checkout take HEAD The pointer moves over .
in other words reflog Give you a chance to regret , That's what we said before , As long as it's submitted code , In general, it won't be lost , Because we can still go through reflog Search for .
Today's article is here , What about? , Have you learned ？ If you still like today's content , One, please Three companies support Well ~（ give the thumbs-up 、 Focus on 、 forward ）