Skip to content

Instantly share code, notes, and snippets.

@Faheetah
Created May 1, 2021 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Faheetah/6bdc23808e83db0050b30a7e1ccfb97b to your computer and use it in GitHub Desktop.
Save Faheetah/6bdc23808e83db0050b30a7e1ccfb97b to your computer and use it in GitHub Desktop.
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 phx.new project_name
$ h mix deps.get
$ h mix ecto.create
$ cat .history
mix phx.new 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 <Faheetah@users.noreply.github.com>
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
$@
;;
esac
}
@Faheetah
Copy link
Author

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