git常用命令

git的一些常用命令

配置

  • 查看

    1
    2
    3
    git config -l #查看所有配置
    git config --global -l #查看全局配置
    git config --local -l #查看本地配置
  • 设置

    1
    2
    3
    4
    git config --global user.name "John Doe" #设置用户名
    git config --global user.email johndoe@example.com #设置用户信箱
    git config --global credential.helper store #设置权限保存,cache保留15分钟
    git config --global core.autocrlf true # true:提交时CRLF->LF,签出时LF->CRLF;input:提交时CRLF->LF,签出时不转换;false:不转换
  • 本地设置(.gitattributes)

    1
    2
    # Set the default behavior, in case people don't have core.autocrlf set.
    *.sh text eol=lf

初始化

  • 现有工程的初始化以及远程PUSH

    1. 进入项目目录,初始化本地仓库

      1
      git init
    2. 把所有文件添加到stage

      1
      git add .
    3. 提交到head

      1
      git commit -m "init project"
    4. 在远程新建仓库
      例:https://github.com/gikeihi/springboot.git

    5. 添加远程仓库地址

      1
      git remote add origin https://github.com/gikeihi/springboot.git
    6. PUSH到远程仓库

      1
      git push -u origin master​

在docker中使用git

有时候为了不污染环境,不想在服务器上安装git,这时候可以用docker里的git,可以选择的image有两个,一个是alpine/git,另外一个是bitnami/git,区别是alpine/git很小,但是它是直接执行git的,所以无法执行shell。
在使用git的时候,一般都是把私钥给git,实现静默交互,如果执行的命令比较简单,那么就可以使用alpine/git

1
docker run -it -v $(pwd)/deploy_key:/git/deploy_key alpine/git -c core.sshCommand="ssh -i /git/deploy_key -F /dev/null -oStrictHostKeyChecking=no" clone git@gitlab.com:eb-innovation/trtc/weshow-ent/weshow-ent-root.git

如果内部想执行多条命令的话,使用bitnami/git

1
2
3
4
5
6
docker run -it -v $(pwd)/deploy_key_weshow:/git/deploy_key_weshow bitnami/git sh -c '
export GIT_SSH_COMMAND="ssh -i /git/deploy_key_weshow -F /dev/null -oStrictHostKeyChecking=no"
git clone --single-branch --branch development git@gitlab.com:eb-innovation/trtc/weshow-ent/weshow-ent-root.git
cd weshow-ent-root
git pull
'