Live data from Hacker News

Show HN: Switch Git Users CLI

github.com

51–60 of 61 posts

Re: Show HN: Switch Git Users CLI

#51
post #13

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. .name " " git config --global user. .email " " git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git conf…

Good idea as it makes the trees "self describing".

But: does this edit a file that can appear in .gitignore or are you uploading this to affect all your collaborators?

Re: Show HN: Switch Git Users CLI

#52
post #45
post #44

Earlier quoted context omitted.

> git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :' Why `; :` at the end?

I'm not entirely sure, but `:` means "true" in bash, and if I omit it, something like this happens: $ git config alias.foo '! echo "$1";' $ git foo bar bar echo "$1";: bar: command not found Whereas if I end with `; :` then it works as expected: $ git config alias.foo '! echo "$1"; :' $ git foo bar bar It seems to execute the last argument (`bar`) as a command without the `:` at the end, and I don't have a `bar` comm…

Seems it's to allow implicit and explicit use of arguments passed to alias. Git does the following to the alias string[1]:

  argv_array_pushf(out, "%s \"$@\"", argv[0]);
So, you can have aliases like `!grep foobar` to automatically accept arguments or aliases like yours that use arguments explicitly.

I've done aliases like `!bash -c 'foo $1' sh` before, but on seeing yours, I see that it was unnecessary to re-wrap with bash.

[1] https://github.com/git/git/blob/e31aba42fb12bdeb0f850829e008...

Re: Show HN: Switch Git Users CLI

#53
post #19

Earlier quoted context omitted.

I find it much easier to use direnv and set GIT_AUTHOR_EMAIL in each of ~/work/.envrc and ~/personal/.envrc No need to reconfigure every repo this way.

Git has conditional includes based on path[1]. My configuration is like: On ~/.gitconfig [user] name = personal email = personal@example.com [includeIF "gitdir:~/workspace/"] path = ~/work.gitconfig Then on ~/work.gitconfig [user] name = workname email = work@example.com [1] https://git-scm.com/docs/git-config#_conditional_includes

Just a note of caution, when this feature was added tools based on libgit2 did not understand it. I just checked now, libgit2 eventually added support (though it took 5 months since I filed the ticket). But if you're using any tools that have a 3-year-old version of libgit2 they might not understand it.

Re: Show HN: Switch Git Users CLI

#54
post #52
post #45

Earlier quoted context omitted.

I'm not entirely sure, but `:` means "true" in bash, and if I omit it, something like this happens: $ git config alias.foo '! echo "$1";' $ git foo bar bar echo "$1";: bar: command not found Whereas if I end with `; :` then it works as expected: $ git config alias.foo '! echo "$1"; :' $ git foo bar bar It seems to execute the last argument (`bar`) as a command without the `:` at the end, and I don't have a `bar` comm…

Seems it's to allow implicit and explicit use of arguments passed to alias. Git does the following to the alias string[1]: argv_array_pushf(out, "%s \"$@\"", argv[0]); So, you can have aliases like `!grep foobar` to automatically accept arguments or aliases like yours that use arguments explicitly. I've done aliases like `!bash -c 'foo $1' sh` before, but on seeing yours, I see that it was unnecessary to re-wrap with…

Interesting! Thanks for digging up the code.

Re: Show HN: Switch Git Users CLI

#55
post #52
post #45

Earlier quoted context omitted.

I'm not entirely sure, but `:` means "true" in bash, and if I omit it, something like this happens: $ git config alias.foo '! echo "$1";' $ git foo bar bar echo "$1";: bar: command not found Whereas if I end with `; :` then it works as expected: $ git config alias.foo '! echo "$1"; :' $ git foo bar bar It seems to execute the last argument (`bar`) as a command without the `:` at the end, and I don't have a `bar` comm…

Seems it's to allow implicit and explicit use of arguments passed to alias. Git does the following to the alias string[1]: argv_array_pushf(out, "%s \"$@\"", argv[0]); So, you can have aliases like `!grep foobar` to automatically accept arguments or aliases like yours that use arguments explicitly. I've done aliases like `!bash -c 'foo $1' sh` before, but on seeing yours, I see that it was unnecessary to re-wrap with…

Huh I had no idea $1/$2/etc worked as-is in an alias. The advice I learned years ago for dealing with any alias that needs to do something custom with parameters is to write it like

  git config alias.foo '!f() { actual command goes here }; f'
as that will pass all the args to the shell function. But if git is already setting it up so the args work then suffixing the alias with ";:" seems simpler.

Re: Show HN: Switch Git Users CLI

#56
post #52

Earlier quoted context omitted.

Seems it's to allow implicit and explicit use of arguments passed to alias. Git does the following to the alias string[1]: argv_array_pushf(out, "%s \"$@\"", argv[0]); So, you can have aliases like `!grep foobar` to automatically accept arguments or aliases like yours that use arguments explicitly. I've done aliases like `!bash -c 'foo $1' sh` before, but on seeing yours, I see that it was unnecessary to re-wrap with…

Huh I had no idea $1/$2/etc worked as-is in an alias. The advice I learned years ago for dealing with any alias that needs to do something custom with parameters is to write it like git config alias.foo '!f() { actual command goes here }; f' as that will pass all the args to the shell function. But if git is already setting it up so the args work then suffixing the alias with ";:" seems simpler.

> The advice I learned years ago

Being able to use $1/$2/etc directly might be a relatively new development.

EDIT: Or maybe not. This might have been doable since 2010:

https://github.com/git/git/commit/8dba1e634af1d973a47fca616a...

Re: Show HN: Switch Git Users CLI

#58
post #51
post #13

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. .name " " git config --global user. .email " " git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git conf…

Good idea as it makes the trees "self describing". But: does this edit a file that can appear in .gitignore or are you uploading this to affect all your collaborators?

It writes to `.git/config` in the local repo. (.git/ is ignored by default, obviously.)

Re: Show HN: Switch Git Users CLI

#59

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

I find that creating a separate user account on my system for each client works much better for this kind of thing than trying to configure every application with multiple identities.

More isolation, less accidents. I use this way too.

Re: Show HN: Switch Git Users CLI

#60
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
Post reply on HN