LoginSignup
3
5

More than 5 years have passed since last update.

git cloneする時にローカルユーザーを同時に指定できるサブコマンド作ったよ!

Posted at

はじめに

「あっ、コミットユーザー間違えた・・・」

gitでこのような経験をした人はいるだろうか。
本家のGithubにしかPushしない場合は遭遇することは少ないだろうが、例えば会社ではGithubEnterpriseを使っている場合などは、それぞれの環境で異なるアカウント名、メールアドレスを使っている、といったケースが多いだろう。
最近だと、フリーランスで働く人も多くなっている現状、会社毎に異なるアカウント名/アドレスが配布され、それぞれ異なるアカウント名、アドレスで作業をする、といったケースもあるのではないだろうか。

このような時、GitではlocalコマンドでUser名とメールアドレスを個別に指定できる機能がある。

$ git config --local user.name <UserName>
$ git config --local user.email <E-Mail>

が、この指定はgit cloneとは別コマンドで指定するため、うっかり忘れが発生してしまう。加えて、地味にコマンドが長いので打つのがメンドクサイ。

そこでgit cloneと同時にlocalのユーザー情報を指定できるコマンド、cloneasコマンドを作成した。

使い方

インストール

まずはGithubからシェルを取ってくる
https://github.com/kiuchikeisuke/git-cloneas

次に内包されているgit-cloneasファイルを、任意のパスが通ったフォルダに配置する。これでコマンドのインストールは完了

ユーザー情報を設定する

global.gitconfigファイルを開き、任意のユーザー情報を記載する

.gitconfig
[SomeUser1]
   name = some-user1
   email = some.user1@some.com
[SomeUser2]
   name = some-user2
   email = some.user2@some.com
...

git cloneasする

コマンドの仕様は以下のとおり

$ git cloneas <User> <git-repo>

例えば今回のgit-cloneasのレポジトリに、SomeUser1の設定を追加したい場合は、以下のようになる

$ git cloneas SomeUser1 git@github.com:kiuchikeisuke/git-cloneas.git
Cloning into 'git-cloneas'...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), 4.52 KiB | 0 bytes/s, done.
set local user.name some-user1
set local user.email some.user1@some.com

これで、Cloneすると同時にlocalのuser情報がセットされるので、うっかり忘れがなくなる。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5