How to manage your Git Identity for Work and for Personal Projects

Simply put, its the name and email that gets used in the Author field of your commits that show up in git log and on the git server.

Why do you need to manage your GIT Identities?

If you’re like me and enjoy using GitHub for personal projects and some private git server (most often a self-hosted GitLab for work, you often need to remember to set your GIT user and email.

otherwise you could end up in a situation like this

committed with wrong identity

Notice my two different git identities? Your boss might find this a little unprofessional…

The Solution

If your workplace lets you use your personal github profile, you’re the lucky one, You can just set your global identity and never think about it.

git config --global "John Doe"
git config --global ""

If however you have 2 or more identities you use on the daily, you’ve got 2 paths.

Opt. 1: Setting Global Identity

You could still set your global to your most used identity and then override per repository.

This is probably the easiest of the two. But IMHO leaves too much up to chance for accidentally forgetting.

Opt. 2: Setting per repository, every time

Simply put, don’t set your global identity, manually set it every time you create or clone a repository. If you forget, github will simply remind you set it.

Now of course, writing out those two lines can be annoying. So to solve this very first world problem of repetitive typing I’ve come up with this simple bash script. (sorry windows users)

# $1 is identity name
identity() {
  case $1 in
    work | google)
      git config "Work name"
      git config ""
      echo "You are now committing as"
      git config "personal name"
      git config ""
      echo "You are now committing as"

If you’re unfamiliar with writing bash/reading bash scripts, the important bits are

case1)  # Defines a case (similar to case "case1":)
        # code goes here
;;      # ends a case block (similar to break;)

When defining a case you can also add a pipe (|) for an OR case.

The code we’re running in each case is simply setting our git config and echoing a completion message.

I personally placed this function in my .bash_aliases, but so long as its sourced at some point when you open your console/terminal you can call it like this:

identity work
# You are now committing as
identity personal
# You are now committing as
identity google
# You are now committing as

This is changing your identity in the repository you are currently working cd’d into.

EMAIL: ved.acnaz@mas
PHONE: 3566 664 848 1+
Made with by Sam Zanca