git 简化命令_简化您的多帐户git设置

git 简化命令

Do you have a habit of changing hostnames when you clone git repositories to use the private key and username from the right account? You can make your life easier, save time, and free your mind for more impactful work by placing your local projects in per-account root folders and accordingly updating your git configurations. Read on to learn how.

克隆git存储库以使用来自正确帐户的私钥和用户名时,您是否习惯更改主机名? 通过将本地项目放置在每个帐户的根文件夹中并相应地更新git配置,可以使您的生活更轻松,节省时间并腾出精力进行更有意义的工作。 继续阅读以了解如何。

Imagine you have two accounts on GitHub (or GitLab, or Bitbucket, or any other repository hosting):

假设您在GitHub(或GitLab,Bitbucket或任何其他存储库托管)上有两个帐户:

  1. Account A — your work account, and

    帐户A-您的工作帐户,以及

  2. Account B — your personal account

    帐户B-您的个人帐户

These two accounts have their unique usernames and they use their unique private keys. When working with the code in these repositories locally you need a way to specify what username and what private key to use for each repository. You have two options to make both accounts work on the same machine.

这两个帐户具有唯一的用户名,并且使用唯一的私钥。 在本地使用这些存储库中的代码时,您需要一种方法来指定每个存储库使用的用户名和私钥。 您有两个选项可以使两个帐户在同一台​​计算机上工作。

快速而肮脏的方式 (A Quick and Dirty Way)

To let your local git know what names and private keys to use, you can create two ssh configurations in ~/.ssh/config, that will contain something like the following:

为了让您的本地git知道要使用的名称和私钥,您可以在~/.ssh/config创建两个ssh配置,其中将包含以下内容:

Host github-work
Hostname github.com
IdentityFile ~/.ssh/id_rsa_account_a
Host github-personal
Hostname github.com
IdentityFile ~/.ssh/id_rsa_account_b

Every time you clone a work or a personal repository you will need to manually change the remote host from github.com to github-work or to github-personal. Additionally, after cloning, you need to set up a related git username and git email to the one associated with the related account.

每次克隆工作或个人存储库时,都需要手动将远程主机从github.comgithub-workgithub-personal 。 此外,克隆后,您需要设置一个相关的git用户名和git电子邮件至与该相关帐户相关联的用户名。

It would work great but sometimes you might forget to change the hostname causing local git to use the default account, or you accidentally set user.email to your personal email on your work project.

它将很好用,但是有时您可能会忘记更改主机名,从而导致本地git使用默认帐户,或者您不小心将user.email设置为您的工作项目上的个人电子邮件。

All in all, apart from wasting your time on every clone, you sometimes make a mistake and realize it only after a push to remote causing inconvenience to the rest of your team. Gladly there is a better way.

总而言之,除了在每个克隆上浪费时间之外,您有时还会犯一个错误,并且只有在移至远程位置后才意识到这一点,这给您团队的其他成员带来不便。 很高兴有更好的方法。

更好的方法 (A Better Way)

By putting a structure around how you store your projects locally, you can supercharge your development processes and forget about changing clone URLs forever.

通过围绕本地存储项目的结构,您可以增加开发流程的工作量,而不必永远更改克隆URL。

步骤1:将项目分为两个子目录 (Step 1: Split projects into two sub-directories)

~/
workspaces/account_a/
project_panda/
project_tiger/
project_swan/account_b/
project_mice/
project_cricket/
project_elephant/

步骤2:修改git配置 (Step 2: Modify git configuration)

For your Account A, add the following configuration.

对于您的帐户A,添加以下配置。

Update ~/.gitconfig to conditionally include custom account configuration for project under ~/workspaces/account_a directory:

更新~/.gitconfig以有条件地在~/workspaces/account_a目录下包括项目的自定义帐户配置:

[includeIf "gitdir:~/workspaces/account_a/“]
path = ~/.gitconfig_account_a

Now create ~/.gitconfig_account_a file to set up your Account A username and email and point to the ssh configuration with the private key:

现在创建~/.gitconfig_account_a文件来设置您的帐户A用户名和电子邮件,并使用私钥指向ssh配置:

[core]
sshCommand="ssh -F ~/.ssh/config_account_a"
[user]
name = Account A Name
email = account.a.email@example.com

Finally, create ~/.ssh/config_account_a file to specify github username and private key:

最后,创建~/.ssh/config_account_a文件以指定github用户名和私钥:

Host github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa_account_a
IdentitiesOnly yes
User account_a_user_name

A similar configuration goes for Account B:

帐户B也有类似的配置:

In ~/.gitconfig:

~/.gitconfig

[includeIf "gitdir:~/workspaces/account_b/“]
path = ~/.gitconfig_account_b

In ~/.gitconfig_account_b:

~/.gitconfig_account_b

[core]
sshCommand="ssh -F ~/.ssh/config_account_b"
[user]
name = Account B Name
email = account.b.email@example.com

Lastly, create ~/.ssh/config_account_b:

最后,创建~/.ssh/config_account_b

Host github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa_account_b
IdentitiesOnly yes
User account_b_user_name

步骤3:更新遥控器 (Step 3: Update remotes)

If you previously used remotes with modified hostnames (for example, github-work and github-personal), you can now revert it to the original hostname github.com:

如果您以前使用的主机名已修改(例如github-workgithub-personal ),则可以将其还原为原始主机名github.com

git@github-work:awesome_you/project_panda.git -> git@github.com:awesome_you/project_panda.git

Once remotes are updated, try it out and see if it works.

遥控器更新后,请尝试一下,看看它是否有效。

步骤4:尝试一下 (Step 4: Try it out)

Now, every time you want to clone a repository for one of your accounts, go to a respective directory and use git commands as usual. Here is an example for a project in Account A:

现在,每次您想要为一个帐户克隆一个存储库时,都转到相应的目录并像往常一样使用git命令。 这是帐户A中项目的示例:

# cd ~/workspaces/account_a
# git clone git@github.com:awesome_you/project_panda.git
# cd project_panda
# git config --get user.emailaccount.a.email@example.com

And the following is an example for a project in Account B:

以下是帐户B中一个项目的示例:

# cd ~/workspaces/account_b
# git clone git@github.com:awesome_you/project_mice.git
# cd project_mice
# git config --get user.emailaccount.b.email@example.com

As you can see, projects have been cloned using related credentials for respective accounts without the need to modify clone URLs.

如您所见,已经使用相关凭证为各个帐户克隆了项目,而无需修改克隆URL。

Managing multiple git accounts for the same SCM hosting provider is tricky and requires some tweaks in the git configuration. While, undoubtedly, you can use modified hostnames for different accounts, the approach is prone to errors and accidental commits with a personal account to your work repo. To level up your development processes, split your projects in separate root directories locally, and forget about headaches caused by fixing the author on committed changes once and for all.

为同一个SCM托管提供商管理多个git帐户非常棘手,并且需要对git配置进行一些调整。 毫无疑问,尽管您可以为不同的帐户使用修改后的主机名,但是这种方法容易出错,并且个人帐户意外提交到您的工作仓库。 为了升级您的开发流程,请在本地将项目拆分到单独的根目录中,而不必担心因一劳永逸地将作者固定在提交的更改上而造成的麻烦。

I want to thank Arnelle for the following article that became an inspiration to write this post:

我要感谢Arnelle的以下文章,这是写这篇文章的灵感来源:

翻译自: https://medium.com/better-programming/simplifying-your-multi-account-git-setup-e8ac50798990

git 简化命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值