Skip to content

Instantly share code, notes, and snippets.

Created May 1, 2021
What would you like to do?
Log commands to a .history file for repos

A problem with repositories is sometimes you do not remember what commands you ran to bootstrap the application, or what generators were used. A history file similar to .bash_history would be handy for a per-project basis. The h command above provides a wrapper around this functionality. It should be portable across any system with bash.

Run a command and write out the command to the git project's .history file, this logs $@ to .history

$ h mix project_name
$ h mix deps.get
$ h mix ecto.create
$ cat .history
mix project_name
mix deps.get
mix ecto.create

Get the last command in the history file, useful for scripting

$ h
mix ecto.create

For example, to add a commit with a message of the last command with git commit -m "$(h)"

$ git add .history
$ git commit -m "$(h)"
[main 8a24723] ls -ahl
 1 file changed, 2 insertions(+)
$ git log -1
commit 8a247239342fb189f53c7680f4779ad0af8942e3 (HEAD -> main)
Author: Faheetah <>
Date:   Sat May 1 12:59:23 2021 -0500

    mix ecto.create
h() {
local history_file="$(git rev-parse --show-toplevel)/.history"
case $1 in
tail -1 $history_file
echo $@ >> $history_file
Copy link

Faheetah commented Jan 8, 2022

Also worth noting another good trick if this was the last command you entered in bash. git commit -m "$(echo !!)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment