basic
生成公钥
1 2 3 4 5 $ ssh-keygen -t ed25519 -C "<your email>" # 不推荐 $ ssh-keygen -t rsa -C "<your email>" $ ssh-keygen -t rsa -C "<your another email>"
配置全局信息
1 2 $ git config --global user.name "sunzhenkai" $ git config --global user.email "zhenkai.sun@qq.com"
多公钥
适用
一台机器,同一Git仓库,多账号
一台机器,多Git仓库,多账号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # 生成公钥 $ ssh-keygen -t rsa -C "<your email>" $ ssh-keygen -t rsa -C "<your another email>" # 配置 config $ vim ~/.ssh/config Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa Host github.com-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_work # 指定账号添加对应公钥 # 使用, 克隆或添加remote时使用config定义的Host别名(github.com / github.com-work / 其他) $ git clone git@github.com:<user-name>/<repo-name>.git $ git remote add origin git@github.com:<user-name>/<repo-name>.git
restore
1 git restore --staged . # 恢复所有暂存
stash
用于暂存代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # 暂存 $ git stash save 'message' # 查看暂存 $ git stash list # 恢复并删除暂存 $ git stash pop stash@{?} # 只恢复,不删除暂存 $ git stash apply stash@{?} # 删除暂存 $ git stash drop stash@{?} # 删除所有暂存 $ git stash clear
submodule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # 添加子模块 $ git submodule add <git-address> <localpath> # 初始化/下载子模块代码 git submodule update --init --recursive # 递归clone $ git clone ... --recursive $ git clone --recurse-submodules ... # pull with submodules $ git pull --recurse-submodules # 更新子模块至最新提交 $ git submodule update --remote --merge # 重置 submodule commit id git submodule update --init
清理
1 2 3 4 $ rm -rf path/to/submodule$ rm -rf .git/modules/{module}$ vim .gitmodules $ vim .git/config
更换 submodule 地址
1 2 3 4 $ cd /path/to/submodule/dirctory$ git remote set-url origin {new-url} $ vim .gitmodules $ git submodule sync
初始化一个 submodule
1 git submodule update --init submoduleName
tags
1 2 3 4 5 $ git tag -s "tag_name" -m "comments" # push 到远端 $ git push origin <tag-name> $ git push --tags $ git push origin --tags
删除大文件
1 git filter-branch -f --prune-empty --index-filter "git rm -rf --cached --ignore-unmatch recommend/keywords.txt" --tag-name-filter cat -- --all
修改 commit 用户名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 设置用户名、邮箱 git config user.name "New User" git config user.email "newuser@gmail.com" git log git rebase -i <commit-id> # 这个 commit-id 需要修改的前一个 # 把需要修改的 commit 前的 'pick' 改为 'edit' , 保存并退出 # 重复下面两步 git commit --amend --reset-author --no-edit git rebase --continue # 强制提交 git push --force-with-lease
参考这里 。
查看某一行修改人
1 git blame <file> -L <start-line>,<end-line>
branch
1 2 # 删除分支 git push origin --delete <branch>
Git Tree
1 2 # 检出 /path/pattern 下文件在 tree-ish 下的状态 git checkout {tree-ish} /path/pattern # 主要后面的 path, 存放 checkout 出来的文件
检出并保存在其他路径
1 git archive f9ee8bb31f04f4e6a8c0d3e96fbb98deeb448d45 | tar -x -C /tmp/f9ee8bb31f04f4e6a8c0d3e96fbb98deeb448d45
从 remote 检出
1 git archive --remote=https://github.com/user/repo.git <tree-ish> | tar -x -C /path/to/target-dir
配置
1 2 3 4 5 # 打印配置 git config -l # 打印全局配置 git config --global -l # 当前用户的配置 git config --sysmte -l # 系统配置
作用域
1 2 3 默认仓库级别 --system 系统 --global 用户目录
配置项
1 2 git config user.name '...' git config user.email '...'
代理
1 2 3 4 5 6 7 # 设置 git config --global http.proxy http://user:password@domain:port git config --global https.proxy http://user:password@domain:port # 取消设置 git config --global --unset http.proxy git config --global --unset https.proxy
最佳实践
分支名称
分支分类
主分支,main / master
开发分支,dev
功能分支,feature / feat
修复分支,bugfix / fix
发布分支,release
紧急修复分支,hotfix
测试分支,test
文档分支,doc
命名示例
1 feature/JIRA-1929-support-oneid
问题排查
中文乱码
参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 git config --global core.quotepath false git config --global gui.encoding utf-8 git config --global i18n.commit.encoding utf-8 git config --global i18n.logoutputencoding utf-8 export LESSCHARSET=utf-8```shell git init git add file git commit -m 'comment' --author='sample@x.com' git commit --amend -m 'comment' ...
git-remote-http libcurl-httpd24.so.4 不存在
错误信息
1 /opt/rh/rh-git218/root/usr/libexec/git-core/git-remote-http: error while loading shared libraries: libcurl-httpd24.so.4: cannot open shared object file: No such file or directory
解决
1 2 3 4 5 6 7 8 find / -name 'libcurl-httpd24.so.4' /opt/rh/httpd24/root/usr/lib64/libcurl-httpd24.so.4 # 修改 LD_LIBRARY_PATH 环境变量 # vim ~/.bashrc export LD_LIBRARY_PATH=/opt/rh/httpd24/root/usr/lib64:$LD_LIBRARY_PATH source ~/.bashrc
shallow update not allowed
错误信息
1 ! [remote rejected] master -> master (shallow update not allowed)
解决
1 git filter-branch -- --all
fatal: ‘…’ does not have a commit checked out
错误信息
1 2 3 4 5 6 7 8 # case 1$ git submodule add {repo-url} fatal: '...' does not have a commit checked out # case 2$ git submodule add {repo-url} {dest-dir} fatal: You are on a branch yet to be born fatal: unable to checkout submodule '...'
原因
在仓库为空时,执行了 git submodule add
解决
1 2 3 4 rm -r .git/modules/{dest-dir} rm -r {dest-dir} # 重新添加 git submodule add ...