切换Git账号(脚本版)

出于某些特殊情况,小编需要反复切换Git账号,然而原生的Git不具备存储多套Git配置的能力,切换起来难免麻烦,如果有一个脚本能存储多套Git配置并灵活切换就好了。

可小编通过网络搜索,且没找到相应的脚本,所以只能自己造轮子了。

原理

Git可通过如下命令修改用户名和邮箱

  git config --global user.name "userName" 
  git config --global user.email "userEmail" 

基于以上命令,我们只需要再提供一个配置的存储能力增删查改能力即可满足需求。更多细节详见源码

安装方式

  1. 确保您的系统已安装git。

  2. 克隆本项目到您的本地目录

    git clone https://github.com/motribe/gitcm.git
    
  3. 进入项目目录并将gitcm.sh添加到系统环境变量

使用方式

$ gitcm.sh
Usage: gitcm.sh {
    
    options}

This is git config manage script.

你可以使用如下参数进行操作
-c                                           查看当前配置
-d {
    
    config_name}                             删除指定配置
-e {
    
    config_name} {
    
    key} {
    
    value}               编辑指定配置
-g {
    
    config_name}                             获取指定配置
-h                                           输出帮助信息
-l                                           输出配置列表
-i {
    
    config_name} {
    
    user_name} {
    
    user_email}    初始化新配置
-u {
    
    config_name}                             切换配置
-v                                           输出调试信息
-?                                           输出帮助信息

使用示例

以下是一些使用本脚本的示例:

  1. 新增git配置

    $ gitcm.sh -i "config1 example email@example.com"
    user.name=example
    user.email=email@example.com
    
  2. 获取git配置列表

    $ gitcm.sh -l
      config1
    
  3. 获取指定的git配置

    $ gitcm.sh -g config1
    user.name=example
    user.email=email@example.com
    
  4. 更新git配置

    $ gitcm.sh -e "config1 user.name example2"
    
  5. 切换git配置

    $ gitcm.sh -u config1
    
  6. 获取当前git使用的配置

    $ gitcm.sh -c
    user.name=example2
    user.email=email@example.com
    

心得

为了简化安装和便于使用,小工具尽量避免引入第三方包。

另外,小编是个后端开发,很少编写shell脚本,shell代码风格一般,如遇bug或有优化的地方,可以提建议,有空时会处理,求轻喷QWQ。

猜你喜欢

转载自blog.csdn.net/moresi/article/details/140828484