Git — monthly work log

5 minutes read

While working as a software developer, you may come across a need to record creative work. Depending on country and laws, this might be even required for some specific cases of tax reduction. The process, although seems simple from the description, is unfortunately quite time-consuming and tiring. Especially when it is necessary to document the work from the whole month. Fortunately, using the Git version control system, with a few commands it is possible to create a list of files (patches) for all changes made by a person in a given period of time.

An example of such a command can be seen below:

git log --pretty="format:%H | Author: %an, %aD | Committer: %cn, %cD | %s" --author="Damian Terlecki" --after="2019-05-01" --before="2019-06-01" | tee /dev/tty | awk -F" " '{ print $1 }' | xargs -I@ bash -c "git show @ > @.txt"

A prerequisite for the correct evaluation of the above line is the presence of Unix tools (tee / xargs / bash) in the path. On the Windows you can optionally install them (which I also recommend) during Git install or use the Cygwin environment. I will explain individual parts of the command work.

  1. git log — displays information about commits;
  • --pretty="format:%H | Author: %an, %aD | Committer: %cn, %cD | %s" — we are interested mainly in author (%an), date (%aD) and commit title (%s), additionally we might be interested in information about who was the committer and when was the change applied (amend/rebase/patch/cherry-pick);
  • --author="Damian Terlecki" — filtering by author;
  • --after="2019-05-01" — filtering commits after provided date (note: commit date, local time);
  • --before="2019-06-01" — filtering commits before provided date which results here in a period of one month;
  1. tee /dev/tty — writes output of the previous command on the screen (terminal) and forwads the data to the next pipe;
  2. awk -F" " '{ print $1 }' — splits input by space and returns first value (hash);
  3. xargs -I@ bash -c "git show @ > @.txt" — executes git show for input param and saves it as text file with same name (-I@ defines a placeholder which will be replaced by the argument value).

A more complex version which extracts the project name from the remote repository:

git log --pretty="format:%H | Author: %an, %aD | Committer: %cn, %cD | %s" --author="Damian Terlecki" --after="2019-05-01" --before="2019-06-01" | tee /dev/tty | awk -F" " '{ print $1 }' | xargs -I@ bash -c "git remote get-url origin | sed -E 's#^.*/(.*)$#\1#' | sed 's#.git$##' | xargs -I! bash -c 'git show @ > !-@.txt'"
  1. git remote get-url origin | sed -E 's#^.*/(.*)$#\1#' | sed 's#.git$##' — extracts the repository name from the origin by default.

To use the script, replace values for --author, --before and --after parameters and execute it in the terminal. In return you should get a file representing changes for each commit. A neat piece of code, don't you agree? ... no? Well, there is probably an easier way to do this, nevertheless, this one also does the job. Maybe it will also be useful for you.

Tip 1: You can use git log --all or limit the results only to changes in remote repositories using git log --remotes.
Tip 2: All local copies of remote repositories can be updated by running git fetch --all.