from Hacker News

Show HN: Switch Git Users CLI

by geongeorgek on 11/12/20, 12:59 PM with 57 comments

  • by varl on 11/12/20, 2:35 PM

    I found this user management strategy somewhere, and it's been working great for me:

      git config --global --unset user.name
      git config --global --unset user.email
      git config --global --unset user.signingkey
    
      git config --global user.useConfigOnly true
    
      git config --global user.<id>.name "<name>"
      git config --global user.<id>.email "<email>"
    
      git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
    
    So given that I have created two users, e.g. personal and work I run:

      git identity work
    
    in repos that need the work name/e-mail, and

      git identity personal
    
    in the ones that are private.
  • by pavlo on 11/12/20, 1:55 PM

    This is a great looking tool.

    What I have been doing by hand for some time is putting code for different customers in different directories and having a conditional in `~/.gitconfig` to determine what config applies there:

        [includeIf "gitdir:~/projects-private/**"]
          path = ./.gitconfig-private
    
        [includeIf "gitdir:~/projects-client/**"]
          path = ./.gitconfig-work
    
    Then in .gitconfig-private or .gitconfig-work I have all the usual gitconfig settings that apply, for example the [user] section...

    Switching to the right directory thus automatically changes the settings.

  • by Foxboron on 11/12/20, 1:50 PM

    A 1000 line YARN lock file for something that would be roughly 20 lines of bash is amazing and terrifying at the same time.
  • by oefrha on 11/12/20, 2:03 PM

    Without using include in gitconfig:

      #!/usr/bin/env zsh
      case $1 in
      foo)
          git config user.name 'John Doe'
          git config user.email john@example.com
          git config user.signingKey 0xFFFFFFFF
          ;;
      bar)
          git config user.name 'Jane Doe'
          git config user.email jane@example.com
          git config user.signingKey 0xEEEEEEEE
          ;;
      baz)
          git config user.name 'My Dog'
          git config user.email dog@example.com
          git config user.signingKey 0xDDDDDDDD
          ;;
      *)
          echo "usage: git-user foo|bar|baz" >&2
          exit 1
          ;;
      esac
    
    Done.

    Btw probably want to add signingKey support.

  • by archseer on 11/12/20, 1:54 PM

  • by mr-karan on 11/12/20, 4:59 PM

    I currently use [karn](https://github.com/prydonius/karn) to manage multiple `git` identities. Works pretty nice.
  • by gurjeet on 11/14/20, 6:59 AM

    I rely on Git's `user.useConfigOnly` option to force myself to set my email per repository. This is what my `user` section in ~/.gitconfig looks like.

      [user]
        name = Gurjeet Singh
        # Tell Git to _not_ guess my name and email based on `whoami` and `hostname`
        useConfigOnly = true
    
    With this in place, whenever I try to commit for the first time in a repository, Git prompts me

      *** Please tell me who you are.*
    
    I then add an email address to the repo-local config based on whether it's work or personal project.

      git config user.email me@example.com
  • by miguelmota on 11/12/20, 5:37 PM

    This package installed 47.4MiB of node_modules. Yikes.
  • by enriquto on 11/12/20, 2:24 PM

    What's the point of writing this simple tool in js? I'd like to use it but I don't have npm everywhere (and I don't want to grab a huge tree of dependencies just for editing a couple of lines on a text file...)
  • by 29athrowaway on 11/12/20, 7:29 PM

    You can have a per-project git config, which to me is a more convenient solution.

    For my work repo I configure the repo to use my work identity. For my personal repos I use my personal identity.

    I do not need to switch identities when using the same repo.

    Doing this, you only setup your identity once per repo not every time, which is safer/less error prone. i.e.: you wont leak your work email on github by accident.

  • by KuiN on 11/12/20, 3:58 PM

    Have seen more than a few attempts to solve this problem. Here's my version[0] and there's many more on GitHub. Wonder if there's an opportunity to unify our efforts & get something into git contrib?

    [0] - https://github.com/bobbo/git-profile

  • by SparkyMcUnicorn on 11/12/20, 3:36 PM

    This line makes it not very useful, because I have to re-add accounts for each project.

        const store = require("data-store")({ path: process.cwd() + "/data.json" });
    
    I think a `git-user-data.json` file in a centralized location makes far more sense.
  • by geongeorgek on 11/12/20, 1:01 PM

    Author here! I'm a freelancer working for multiple agencies at the moment. And One of the requirements was that I use the agency email for all commits.

    So I made this as a sugar for git config user.email whatever@gmail.com

  • by zachrip on 11/12/20, 2:03 PM

    Looks neat, but I agree with others about using conditional includes instead. Perhaps this tool could build on that?
  • by Icyphox on 11/12/20, 2:06 PM

    Could've been written in a 30–40 line shell script, heh.