Git Authorship

Because the tech industry has decided that Git logs – and specifically GitHub accounts – are effectively part of a developer's resume, I've always just used my personal details as Git credentials. That way, when I push code to GitHub, it all neatly aligns, syncs to my activity heatmap, and I don't have to juggle a bunch of different credentials for every repository I'm working on. But what happens when you have a work account that needs Git access, but (logically) uses your work details?

In my case, I'm currently using a codebase hosted on Bitbucket, and because we have Atlassian work accounts for Jira, Confluence, etc. I've been given access using my work email address and name. Nothing about that set up prevents me from cloning the repository, making commits, or doing just about anything, but when I create a PR, the person creating the PR is not considered the same person as the one that made the code changes, which has caused a CI headache (and also looks a bit rubbish 😅).

Now plenty of people have advised me around running different SSH credentials, but I don't use SSH for Git. I've never seen the point, and none of the quick setup guides default to it. I have zero interest in this kind of technical configuration, so if it works, I don't question it.

There are also a couple of, frankly, bad options. For a one-off commit, you can change your author details on the commit message itself, but that's very inefficient if you're actively working on some code. Or you could just modify your Git credentials via the CLI, but that's hard to keep track of if you jump around codebases a fair bit, which I absolutely do.

However, there is a very simple, low-key fix[1]. Something that can be done on any repository you don't want to use your global Git credentials; a fix which only has to be set once per repository. The simple trick?

Edit your local Git config file!

Honestly, I just forget that this exists. Git sets itself up automatically, and .git folders are hidden on all major operating systems by default, so I never look at what it contains. But sure enough, in the root directory of every Git-powered codebase, you have a Git config file. Because of course you do. This is where things like the codebase origin are set and stored. And, like any good cascading system, your local Git config will override any settings that are configured both here and in the root config file, including your author details.

Just pop open ../<your_codebase>/.git/config in any text editor and add the following at the bottom (though note that the exact location is not important):

[user]
    name = <name>
    email = <email_address>

You don't have to worry about quotes or escaped text or anything like that, either. So say your name is Janet Smith, and your email address is janet.smith@business.org, this would be your config:

[user]
    name = Janet Smith
    email = janet.smith@business.org

How simple is that! 🎉

Oh, and if you want to check that this is taking effect correctly, you can test it locally. Just make any commit – it doesn't have to be a "valid" one; you can create a new branch, change anything, and stage/commit it – and then use the following Git command:

git show

That will print out the commit log for the latest commit. Right up at the top, you'll see Author details. It'll look something like this:

commit 395465mdsfd8sdf7sf6sdg (HEAD -> branch_name, origin/branch_name)
Author: Janet Smith <janet.smith@business.org>
Date: Fri Jan 26 20:02:00 2024 +0000
...

If you were to compare this against git show @~ (which is the second-to-last commit, or HEAD - 1), you should see that the credentials have changed (assuming you made the prior commit as well, of course 😉).

(P.S. To escape from the commit log, press the Q key on your keyboard and you'll be dropped back to the terminal.)

And that's it. Now every commit you make in this repository will have your codebase-specific credentials, whereas commits made elsewhere will default to your globally set details 👍

Explore Other Articles

Newer

A Missing Narrative

Ever spend weeks writing something, hit publish, and then feel completely unsatisfied. That's what just happened to me. So I figured I'd try to work out why.

Conversation

Want to take part?

Comments are powered by Webmentions; if you know what that means, do your thing 👍

Footnotes

  • <p>How to change your Git author details for a single codebase, and check that the changes have taken effect.</p>
  • Murray Champernowne.
Article permalink