Git diff examples
The git diff command is useful for finding changes between two commits when using git for source control. there are many options for formatting the output also, so for one example say I am just interesting in getting a list of files that have changed from a given starting and ending commit it, such a task can be completed by using the git diff command with the name only option.
I am also a javaScript developer so this post will also be on a few quick examples that make use of the child process module in core nodejs to make git command calls that help get a list of files that have changed between tow commits in a repository. However I will be going over some examples that can just be used in the command line interface also of course.
1 - Git log and git diff –name-only for getting a list of files that changed
So one task that comes to mind is that I would like to have a way to just get a list of files that have changed from one commit to the next. I could use the git diff command with the –name-only command, but first I will want two commit ids before hand. So I can also use the git log command to get a list of commit ids for say the last twenty commits of a repository. While I am at it I can also use the format option of the git log command to format the output so that it will just spit out commit id values.
|
|
Now that I have a list of commit ids going back some twenty commits I can use these to find out what files have changed from the oldest commit up to the latest one.
|
|
Okay great now I have my list of files that have changed between one point in time, and another. I could then use this list of files, and commit ids to do cretin things with some additional scripting. Such as tabulating the total word count for each file in my _posts folder at the oldest commit by creating a json file for each post in the folder for starters. Then just update the json files for the posts in this list rather than the full collection of some six hundred posts and growing resulting in much faster way of creating a word count increase history.
2 - More on the PRETTY FORMATS for git log, before doing a git diff
Although this is a post on the git diff command I think that I should write at least a little more on what the options are for formatting the output of a git log command, to help with the process of doing a git diff command. The reason why I say that is because say I want to write a script that will log data for changes on a per day bases, back a certain number of commits.
|
|
I can then use this info to just get the files that where edited on Oct 31 with git diff like this:
|
|
The date format that is used for dates with the \%cD format option is RFC2822 style date strings. What is great about this format is not just that it is easy to manually read the output from git log, it is also a format that can be used to pass to the javaScript date constructor.
|
|
So then it is possible to write a nodejs script that would use the child process module to do a git log command, then parse the standard output into an array of objects that contain a commit hash id, and date object as properties. This array can then be used as a way to go about making one or more git diff calls to get the differences that have happened on a per day, rather than per commit basis.
3 - A nodejs powered project that makes use of git log, and git diff to get a list of files that changed
So now I though I would work out a little javaScript code that is a small collection of nodejs module that make use of git log, and git diff via the spawn child process module method on nodejs. This will just be two modules and an additional script that makes use of these modules. The first module will use git log to get a list of commit ids, and then the second module will use git diff to get a list of file names from that collection of commit ids, as well as a staring and ending index value.
3.1 - A git-commit-list module that uses git log
So first I want a module that I can just use to get an array of commit ids for a given current working directly that should be a git folder.
|
|
3.2 - A git-commit-list-diff module that uses git diff
Now that I have a way to get an array of commit ids, I can then have a module that will take that array of commit ids as an argument. This module will use the git diff command with ids from a commit id lost created with the other module to get a list of files that have changed between a starting and ending index value of ids in the commit id list.
|
|
3.3 - An index.js file that makes use of these modules
So now I just need a main index.js file that will make use of this.
|
|
4 - Conclusion
So the git diff command is one of many sub commands of git that will come up when working things out with a git repository. I can use it to just know what has changed in terms of content between the last commit, and the current state of files. Or I can go way back, and get a long list of files that have been altered from one point back in time up to another. I can then of course use the git diff command along with many others such as git log, and additional scripting to accomplish all kinds of tasks such as finding out increases in word count over time if it is a repository that contains markdown files of blog posts for example.