Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Last active October 31, 2023 05:10
Show Gist options
  • Save tangoabcdelta/aee5c46e023afc7fcaffb2b950cfc12d to your computer and use it in GitHub Desktop.
Save tangoabcdelta/aee5c46e023afc7fcaffb2b950cfc12d to your computer and use it in GitHub Desktop.
Adding `git hist` alias that shows you the history of a directory or a file

To create a git hist alias in your Bash profile that:

  1. prints the history of the whole directory if no arguments are specified, and
  2. if a file or directory is specified, it shows the history of that,

you can add the following lines to your ~/.bash_profile or ~/.bashrc file:

# define a Bash function called git hist
git hist() {
  # checks if there are no command-line arguments
  if [ $# -eq 0 ]; then
    git log --oneline --decorate --graph --all
    # if there are no arguments, it shows the history of the whole directory
  else
    git log --oneline --decorate --graph "$@"
    # if there are arguments, it shows the history of the specified file or directory.
  fi
}

After adding these lines to your Bash profile, save the file, and either restart your terminal or run source ~/.bash_profile (or source ~/.bashrc) to apply the changes.

Now, you can use git hist to view the history of the whole directory. And git hist <file> or git hist <directory> to view the history of a specific file or directory.

To include the author's name in the git log output, you can use the --pretty option to specify a custom format.

git log --oneline --decorate --graph --all --pretty=format:"%h %d %s (%an)"

This will display the commit hash, branch/tag decorations, commit message, author's name for each commit in the Git log.

  • %h: Short commit hash.
  • %d: Decorations (branch and tag names).
  • %s: Commit message.
  • (%an): Author's name in parentheses.

To find author date and time, you can pass in additional formatting parameters:

git log --oneline --decorate --graph --all --pretty=format:"%h %d %s (%an) %ad"

The param %ad is used to include the author date in the format and the date and time will be in the format defined by your Git configuration.

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