Who moved my cheese, find out content change history with git

In the daily development process, a piece of code content is added and deleted are sparse and common things. This also causes us to encounter two problems in our daily development work.

  • Who added the content of this code, has it always existed or has been modified many times?
  • A piece of code was deleted, who deleted it?

Here we illustrate with a concrete example.

There is a hosts.txt file with the content

 1 2 
import lib/a import lib/b

Now we have a situation like this

  • The earliest host.txt contains the introduction of lib/c
  • But in a certain version lib/c was removed by someone
  • We want to determine who and which commit removed this lib/c

We can use this script to achieve this, the script is very simple as follows.

 1 2 3 4 5 6 7 
// 将下面的内容保存成whoMovedMyCheese.sh 文件#!/bin/bash git log -S$1 $2  // 使用方法whoMovedMyCheese.sh keyword file_path

Call the script to view

 1 2 3 4 5 6 7 8 9 10 11 12 13 
whoMovedMyCheese.sh lib/c hosts commit a4b5ac190f9d152dbdb6555862617ba93f ( HEAD -> master ) Author: hahaha <[email protected]> Date: Mon May 16 07:59:13 2022 +0800  feat: remove lib/c commit 89405dbe29ff79412f701e529791a10387e Author: hahaha <[email protected]> Date: Mon May 16 07:58:53 2022 +0800  new file: host.txt

Yes, through the above script, you can easily check the history of adding and deleting a piece of content. Is it very convenient and fast and effective.

Say goodbye to Git blame

Existing content tracking history

  • git blame can only view the most recent modification,
  • The method in this paper can check the modification history of a content. For example, information such as adding-deleting-adding a piece of content can be found.

Remove content tracking

  • git blame cannot view information about non-existing code snippets
  • This method can view the historical information of the deleted content

Git is a good thing, make good use of it, especially the terminal git command, you will easily be efficient, fast and accurate in dealing with problems.

droidyue_gzh_green_png.png

This article is reprinted from https://droidyue.com/blog/2022/06/13/a-little-git-log-case-can-do-big-help/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.