Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pimp My Git – Manage Different Git Identities

DZone's Guide to

Pimp My Git – Manage Different Git Identities

Git users- this tutorial runs through two methods of managing Git identities when cloning new repositories for separate projects.

· DevOps Zone
Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

I usually work on different Git projects that need different Git identities. My work flow for new repositories was

  1. Clone new repository.
  2. Go to cloned repository.
  3. If it is necessary to change the Git identity, call a shell script that runs `git config user.name “Sandra Parsick”; git config user.email sparsick@web.de`

I was never happy with this solution, but it works. Fortunately, a tweet of @BenediktRitter and one of @wosc suggest two alternatives to my method.

Image title

The first method is based on the Git feature “Conditional Includes” (required Git Version at least 2.13). The idea is that you define a default Git identity and separate Git identities per specific directory. That means every repository that is cloned beneath one of the specific directory, has automatically its specified Git identities.

The second method is based on a Python script, that is inspired by the Mercurial extension hg-persona. The idea is that you can individually set a Git identity per Git repository. It is a one command replacement for the git config user.*command serie.

In the next two sections, I’d like to summarize how to set up and how to use these two methods. I have tested it on a Debian-based system. Let’s start with the first one.

Summarize Git Identity for Several Git Repositories

As above described, this method is based on the Git feature “Conditional Includes.” Therefore, ensure you have installed your Git client in at least version 2.13. Assume, we want to have two Git identities, one for Github and one for work. Therefore, create two .gitconfig files in your home folder.

touch ~/.gitconfig_github
touch ~/.gitconfig_work

Then add the specific Git identity in respective .gitconfig files.

~/.gitconfig_github
 
[user]
   name = YourNameForGithub
   email = name@forgithub.com
~/.gitconfig_work
 
[user]
   name = YourNameForWork
   email = name@forwork.com

The next step is to add these two .gitconfig files to our global one and to specify when to use them.

~/.gitconfig
 
[user]
   name = defaultName
   email = default@email.com
 
[includeIf "gitdir:~/workspace_work/"]
   path = .gitconfig_work
 
[includeIf "gitdir:~/workspace_github/"]
   path = .gitconfig_github

Now, every repository, that is cloned beneath ~/workspace_work/, has automatically the Git identity for Work (.gitconfig_work) and every repository, that is cloned beneath ~/workspace_github/, has automatically the Git identity for Github (.gitconfig_github). Otherwise, the default Git identity is used.

Setting Git Identity Individually Per Git Repository

For the second method, you have to install ws.git-persona from PyPI.

sudo apt-get install pip # if PyPI isn't install
pip install ws.git-persona

Then, open your global ~/.gitconfig and add your personas. In our cases, we add two personas, one for Github and one for work.

~/.gitconfig
 
[persona]
  github = YourNameForGithub <name@forgithub.com>
  work = YourNameForWork <name@forwork.com>

In the next step, we want to switch our Git identity in a Git repository. This is now possible with the command git-persona. In the following example, we switch to the identity for Github and then to the identity for work.

> git-persona -n github
Setting user.name="YourNameForGithub", user.email="name@forgithub.com"
> git config user.name
YourNameForGithub
> git config user.email
name@forgithub.com
> git-persona -n work
Setting user.name="YourNameForWork", user.email="name@forwork.com"
> git config user.email
name@forwork.com
> git config user.name
YourNameForWork

If you have other methods to manage different Git identities, let me know it and write a comment.

Links

  1. Blog post about Git feature “Conditional Includes.”
  2. Github repository of git-personas.

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.

Topics:
devops ,git ,github

Published at DZone with permission of Sandra Parsick, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}