ยท All Snippets

Git Config and Snippets

Setup user name and email on a per repository basis:

git config "dre"
git config ""

Git Commit Time & Timezone

With a team in multiple timezones we’re all starting to think in UTC. Commits have two timestamps, the “author date” and the “commit date”. Both can be changed by setting the TZ environment variable.

TZ=UTC git commit

Multiple SSH Keys

See how to setup and use multiple SSH keys in my other post.

Commit in the past

There are easy ways to specify time for specific commits and add git commits in the past. Remember that merging branches with such commits can retroactively change the timeline of a git repository.

git commit --date="yesterday" -m "commit msg"
git commit --date="3 day ago" -m "commit msg"
git commit --date="2006-01-02 15:04:05" -m "message"

Format and apply git patch files

Creating git patches in order to exchange diff files of changes made to a git repository, between two branches or unstaged.

git format-patch <branch> <options>

Example, we’re making changes on a feature branch and committing there.

git branch patch-branch
git checkout patch-branch
git commit -am "committing changes"

We can then create patch files from commits

git format-patch master

Another example, we want to create a patch set for the last 10 commits from HEAD:

git format-patch -10 HEAD

Someone else can now apply this patch set, ideally on a branch and not master.

git am <patch_file>
git apply <patch_file>

Or bulk apply 10 patch sets:

git am -3k 00*.patch

Github CLI

Signing into multiple GitHub accounts while working on the same day on different projects can be tedious. The GitHub CLI allows for some more convenience:

brew install gh
gh auth login
# go through the browser auth process

before doing anything, check that you’re on the correct account

gh api user --jq '"You are @\(.login) (\(.name))."'

and now we can add a repository without opening a browser

gh repo create --source . --public --remote

Github Token

In order to go get private GitHub repositories, create a new personal access token at

export GITHUB_TOKEN=gh...
git config --global url."https://${GITHUB_TOKEN}".insteadOf ""


go get -u -f

Change Git History Email & Author

In situations where we are open sourcing a company internal repository, we often have the options to wipe the history and publish with a single “initial commit” or rewrite authors with publicly acceptable names and emails. Branch out for the rewrite:

git branch rewrite
git checkout rewrite

set the correct author in this repository

git config "dre"
git config ""

change everything after commit d18590053de58631da12cb2c6c11210fd1f548c8

git rebase -r d18590053de58631da12cb2c6c11210fd1f548c8 --exec 'git commit --amend --no-edit --reset-author'

OR change all including root

git rebase -r --root --exec 'git commit --amend --no-edit --reset-author'

then push the rewrite branch to a new master target

git push backup rewrite:master

Last modified on Sunday, Aug 14, 2022.
Go back