The original link

If you’re working with multiple developers to maintain and manage a complex Git codebar, you might want to use tools like GitHub or BitBucket to delve into commit history and look for branching and merge issues.

The graphical interface provides a very user friendly interface to manage PR and view some simple history, but while the workflow SHTF You should join me on the command line to learn and master the git log when it is no longer possible to use git logs instead or some related tags to dig out the actual situation.

Git Repository is an example

To accompany this example, I have prepared a Git repository that I can clone and run:

$ git clone https://github.com/ianmiell/cookbook-openshift3-frozen
$ cd cookbook-openshift3-frozenCopy the code

git log

Git log is the vanlilla log you’re probably most familiar with:

$ git log

commit f40f8813d7fb1ab9f47aa19a27099c9e1836ed4f 
Author: Ian Miell <[email protected]>
Date: Sat Mar 24 12:00:23 2018 +0000

pip

commit 14df2f39d40c43f9b9915226bc8455c8b27e841b
Author: Ian Miell <[email protected]>
Date: Sat Mar 24 11:55:18 2018 +0000

ignore

commit 5d42c78c30e9caff953b42362de29748c1a2a350
Author: Ian Miell <[email protected]>
Date: Sat Mar 24 09:43:45 2018 +0000

latestCopy the code

The output shows 5+ rows for each submission, including the date, the author’s submission information, and the ID. Submissions are arranged in reverse chronological order, as we are usually only interested in the most recent submissions.

NOTE: It is worth mentioning that we can control whether the output to the console contains versions, aliases, and so on

– oneline

Most of the time I don’t care about authors or submission dates, so if we want to see more information on one screen, we can use the –oneline command to just display the id of the submission and the comment information for each submission

$git log --oneline ecab26a JENKINSFILE: Upgrade from 1.3 only 886111a default is master if not a multi-branch Jenkins build 9816651 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3 bf36cf5 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3Copy the code

– decorate

Looking at git log information, we sometimes want to know more about each commit. For example, which branch did the commit come from? Is there a label, and if so, how much?

At this point, the –decorate command gives us this information:

$ git log --oneline --decorate
ecab26a (HEAD -> master, origin/master, origin/HEAD) JENKINSFILE: Upgrade from 1.3 only
886111a JENKINSFILE: default is master if not a multi-branch Jenkins build
9816651 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3Copy the code

Git’s version update log is already set as a default, so we don’t need to hit extra commands to get information, which is great.

-all

$ git log --oneline --decorate --all ecab26a (HEAD -> master, origin/master, origin/HEAD) JENKINSFILE: Upgrade from 1.3 only 886111a JENKINSFILE: default is master if not a multi-branch Jenkins build 9816651 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3 [...]  a1eceaf DOCS: Known issue added to upgrade docs 774a816 (origin/first_etcd, first_etcd) first_etcd 7bbe328 first_etcd check 654f8e1 (origin/iptables_fix, iptables_fix) retry added to iptables to prevent race conditions with iptables updates e1ee997 Merge branch 'development'Copy the code

Can you see what’s added to the message? We can compare this to the oneline command. Yes, all branches are displayed…

– graph

The graph command also allows you to see git GUi-like output on the command line, making it easier to understand and master the information you need anytime, anywhere.

$ git log --oneline --decorate --all --graph * ecab26a (HEAD -> master, origin/master, origin/HEAD) JENKINSFILE: Upgrade from 1.3 only * 886111a JENKINSFILE: default is master if not a multi-branch Jenkins build * 9816651 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3 |\ | * bf36cf5 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3 | |\ | | * 313c03a JENKINSFILE: quick mode is INFO level only | | * 340a8f2 JENKINSFILES: divided up into separate jobs | | * 79e82bc JENKINSFILE: upgrades-specific Jenkinsfile added | * | dce4c71 Add logic for additional FW for master (When not a node) * | | d21351c  Update utils/atomic |/ / * | 3bd51ba Fix issue with ETCD * | b87091a Add missing FW for HTTPD |/ * a29df49 Missing (s) * 51dff3a Fix rubocopCopy the code

Don’t panic!

The above problems may be difficult for beginners to understand and accept, and there is no good tutorial to guide them. However, I will provide some tips to make it easier for beginners to read and understand git logs

The * symbol represents the corresponding submission and records the details of the submission (only the submission ID and the first comment line are listed here).

We can use the following example to help us see the effect of each branch content change on the branch line:

| * bf36cf5 Merge branch 'master' of github.com:IshentRas/cookbook-openshift3
| |\ 
| | * 313c03a JENKINSFILE: quick mode is INFO level onlyCopy the code

The line on the left can be thought of as a stable branch, and the two lines on the right represent the corresponding changes and the changes committed to each other (9816651 and D21351C).

The blue line takes you to a parent of the BF36CF5 merge (what is the commit identifier for the blue parent?). , the pink code is forwarded to another parent submission (313C03a).

The second line from the left shows a merge for BF36CF5, and the third shows another merge commit 313C03A

It’s worth taking the time to figure out the basics, and it will make it easier to understand what comes next

– simplify – by – decoration

If you’re looking for the entire history of your project and just want to know the key changes, this command can be very helpful. You need to follow this command if you want to use it in conjunction with the –decorate command.

It will remove all unmarked information from the submission, and of course, the latest submission is always there:

$ git log --oneline --decorate --all --graph --simplify-by-decoration * ecab26a (HEAD -> master, origin/master, origin/HEAD) JENKINSFILE: Upgrade from 1.3 only | * 774 a816 origin/first_etcd first_etcd | | * 654 / f8e1 origin/iptables_fix retry added to iptables to prevent race conditions with iptables updates |/ * 652b1ff (origin/new-logic-upgrade) Fix issue iwith kitchen and remove sensitive output * ed226f7 First commitCopy the code

File information

The information displayed using the –oneline command is a little sparse, so often –stat will give you more information about each change.

A number indicates the number of lines changed, a + sign indicates insertion, and then a – sign indicates deletion. Here is did not change a concept, if a line only was modified once, also be the meaning that adds.

$ git log --oneline --decorate --all --graph --stat * ecab26a (HEAD -> master, origin/master, origin/HEAD) JENKINSFILE: The Upgrade from 1.3 only | Jenkinsfile. Upgrades | 2 + - | 1 file changed, 1 insertion (+), 1 deletion(-) * 886111a JENKINSFILE: default is master if not a multi-branch Jenkins build | Jenkinsfile.full | 2 +- | Jenkinsfile.upgrades | 2 +- | 2 files changed, 2 insertions(+), 2 deletions(-)Copy the code

If you find –stat hard to remember, you can choose –name-only, but this will lose information about row count changes.

The use of re in submission

This command is also very convenient to use. The -g command allows you to search for all submissions and only return submissions and their files through regular expressions.

The following example j shows the relevant variation of searching text for chef-client via regular expression:

$ git log -G 'chef-client' --graph --oneline --stat ... * 22c2b1b Fix script for deploying origin | scripts/origin_deploy.sh | 65 ++++++++++++----------------------------------------------------- | 1 file changed, 12 insertions(+), 53 deletions(-) ... | * | 1a112bf - Move origin_deploy.sh in scripts folder - Enable HTTPD at startup | | | origin_deploy.sh | 148 ------------------------------------------------------------------------------------------------------------------------ ---------------------------- | | | scripts/origin_deploy.sh | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ | | | 2 files changed, 148 insertions(+), 148 deletions(-) ... | * | 9bb795d - Add MIT LICENCE model - Add script to auto deploy origin instance |/ / | | origin_deploy.sh | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | | 1 file changed, 93 insertions(+)Copy the code

If you’ve spent a lot of time in the past using Git log –patch to search for the output of related changes, this command is perfect.

There’s a weird command called –pickaxe-all that tells you how all files change in each commit, not just the ones matched by regular expressions: $ git log -G ‘chef-client’ –graph –oneline –stat –pickaxe-all

Having said that, go ahead and give it a try