Skip to main content

Git

Reset all commit times to the original commit date

$ git filter-branch --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE'

When rebasing commits, the date information is updated to the current date and time. There are two times stored in a commit however; commit date and author date.

The author date is the original date of the commit, so running a filter-branch across the repository will reset the date of every commit to the original author commit.

Reset all commit author information for a specific name/email

$ git filter-branch --env-filter '
  OLD_EMAIL="old@email.com"
  NEW_NAME="New Name"
  NEW_EMAIL="new@email.com"

  if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
  then
      export GIT_COMMITTER_NAME="$NEW_NAME"
      export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
  fi

  if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
  then
      export GIT_AUTHOR_NAME="$NEW_NAME"
      export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
  fi
  ' --tag-name-filter cat -- --branches --tags

If searching by name instead of email, swap $GIT_COMMITTER_EMAIL and $GIT_AUTHOR_EMAIL with $GIT_COMMITTER_NAME and $GIT_AUTHOR_NAME name respectively.

Format the output of logs

Aliases for Git can be set up to display customised outputs from git log, typically using the --pretty=format:'...' flag.

See my own Git configuration file for examples of log formatting.

The Pro Git book lists:

Split one existing commit into many commits

Run an interactive rebase at the specific commit:

$ git rebase -i <id>^

Mark the commit to split as edit in the interactive rebase's todo:

edit <id>

Once editing the commit, reset the HEAD to unstage all files from the commit being edited:

$ git reset HEAD~

With all files unstaged, select the files to commit with git add, commit them and repeat this process until the commit is split as desired. Then let the rebase continue:

$ git rebase --continue

Export a stash to a file

$ git stash show -p > stash.patch

To apply the patch:

$ git apply stash.patch