Hidden features of Git

Mehmet Ali Baykara
2 min readApr 10, 2024

Since Git is an essential tool for software engineers, we should utilize it to its fullest extent. I’ll list some features you may or may not know about, which can make your daily work with Git easier.

https://git-scm.com/images/logos/downloads/Git-Logo-2Color.png
  1. Git aliases!

yes, we love bash aliases why not git aliases allows to define anything, see following samples:

$ git config --global alias.co checkout

Now you have globally defined an alias for the checkout command. No need to always type git checkout -b feature/login-fix, instead simply use git co -b feature/login-fix.

Or, for whatever reason, if you want to make an empty commit:

$ git config --global alias.empty commit --allow-empty -m "empty commit"

Now hit the git empty

Your aliases are already autocompleted.

2. Sign your commits with your SSH keys instead of PGB keys

Personally, I am not nerdy enough to configure and use signing my commits regularly if it’s not forced :) But using SSH keys for signing commits makes the configuration much easier. Why not use it?

git config gpg.format ssh #or with --global flags set the configuration global
git config user.signingkey ~/.ssh/key.pub
git commit -S -m "My signed commit"

If you don’t want to use -S flag then

git config --global commit.gpgsign true

3. Configure Git with different users and SSH keys and more!

Sometimes you want to commit with your personal account to your GitHub account, but you have set your work account globally, and you commit with the wrong Git user, or vice versa.

Git reads its configuration from different directories, which are:

  1. /etc/gitconfig → system-wide configuration
  2. ~/.gitconfig → global configuration
  3. /all-repos/my-backend/.git/config → local configuration

Precedence is 1 -> 2 -> 3, so the local config will override the global one and so on.

Let’s use conditional include to use configuration based on which directory we are in. This feature is called conditional include. Just set it up as follows:

Assuming you work with Git under two main directories as follows:

$ pwd
$ /users/mbaykara
$ ls -l
$ drwx------ 4 mbaykara staff 128 Aug 30 2023 work
drwx------+ 4 mbaykara staff 128 Sep 8 2023 private

As you may have guessed, I want to use the Git account with my work email while I work under /users/mbaykara/work directory and with my private email while working in /users/mbaykara/private.

In your global .gitconfig file, which is located at /users/mbaykara/.gitconfig, adjust the following lines:

[includeIf “gitdir:~/work/”]
path = ~/work/.gitconfig
[includeIf “gitdir:~/private”]
path = ~/private/.gitconfig

Now your Git client will apply the respective configuration accordingly. You don’t need to remember to change your Git user instantly while you’re working with Git.

For more features and git hacks look at the excellent book by Scott Chacon the Pro Git.

Sources:
1. Pro Git by Scott Chacon, Ben Straub
2. https://git-scm.com/doc

--

--