git
1. 安装
Debian: sudo apt install git
2. 配置
查看所有配置 git config --list
sh
git config --global user.name "axiomofchoice-hjt" # 用户名
git config --global user.email "Axiomofchoice@163.com" # 邮箱
git config --global core.quotepath false # 文件名显示中文
git config --global core.autocrlf false # 取消 CRLF
git config --global pull.rebase true # pull 默认 rebase
git config --global core.editor "code --wait" # 编辑器默认 vscode2.1. 命令别名
sh
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st "status -sb"
git config --global alias.up '!git add --all; git commit -m =; git push;'
git config --global alias.lg "log --oneline --graph"2.2. 代理
sh
git config --global http.proxy http://127.0.0.1:xxxx
git config --global https.proxy http://127.0.0.1:xxxx
git config --global --unset http.proxy
git config --global --unset https.proxy2.3. 配置文件
~/.gitconfig用户配置.git/config项目配置git config --global --edit编辑配置文件
2.4. ssh 配置
ssh-keygen -t rsa -C "1939696303@qq.com"~/.ssh/id_rsa.pub文件内容复制后到 github 添加 SSH 公钥ssh -T git@github.com验证
如果失败且 ~/.ssh/known_hosts 文件不存在,执行 ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
如果还失败,编辑 ~/.ssh/config 如下:
text
# 配置 github.com
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
# 配置其他网站
Host xxx.com
HostName xxx.com
IdentityFile ~/.ssh/id_xxx3. 项目配置
3.1. .gitignore
注释 # 必须是在行首,不能跟在内容后,下面的写法不合法
text
build # comment4. 克隆
直接克隆
git clone $urlgit clone $url $dir指定目录
从空项目中拉取
sh
mkdir $dir
cd $dir
git init
git remote add origin $url
git fetch
git checkout $branchdepth 克隆
sh
git clone $url --depth=1 # 只有 master 最新版
cd $dir
git fetch --depth=1 origin $branch # 拉取分支的最新版
git checkout $branch
git fetch --unshallow # 拉取当前分支的完整历史克隆单分支
sh
git clone $url --branch $branch --single-branchsparse checkout,可拉取指定目录
sh
mkdir $dir
cd $dir
git init
git remote add origin $url
git config core.sparsecheckout true
echo $sub_dir >> .git/info/sparse-checkout
git fetch
git checkout $branch5. 查看
git status状态git diff <file>查看差别git log日志,可指定文件 / commit--oneline精简日志--graph以图的形式--stat显示文件更改-p--patch查看 diff
git reflog操作日志
6. 提交
git add $path暂存更改git commit -m "$message"提交更改git reset HEAD $path取消暂存git checkout $path删除更改(会丢失)git reset --soft $target移动 HEAD 指针,不改变文件,将文件变化体现在暂存区git reset --hard $target强制移动 HEAD 指针,改变文件
目标 $target 可以是:
HEAD^上一个提交HEAD~10往前第 10 个提交- commit id
origin/mainorigin/main 分支
标签
git tag查看标签git tag $tagname打标签
7. 分支
git branch查看本地分支git branch $branch新建分支git checkout $branch切换分支git checkout -b $branch $remoteBranch新建分支、拉取远程代码、切换git branch -D $branch删除分支git remote prune origin删除远程已经删除的分支git fetch --all更新所有 remote 分支
rebase 后推送要用 git push --force-with-lease
8. tag
git checkout $tagname?git pull origin --tags $tagname当前分支拉取 tag
9. 更改提交
如果编辑器设为 vscode 且装了 gitlens 插件就可以图形化操作
编辑单个提交:
git rebase -i HEAD~10编辑文件,显示最后 10 个提交- 将需要修改的提交前 pick 改成 edit,保存退出
- 修改文件并
git add xxx git commit --amend- 修改提交消息,保存退出
- 可以重复多次 3, 4, 5
git rebase --continue
合并提交:第二步将被合并的提交前 pick 改成 squash
排序:第二步直接调整提交的顺序
10. 子模块
添加
sh
git submodule add https://github.com/fmtlib/fmt third_party/fmt更新
sh
git submodule sync
git submodule update --init --recursive删除
sh
git submodule deinit -f third_party/fmt
git rm --cached third_party/fmt
# 然后删除 .gitmodules 里的字段11. patch
- 打 patch:
git diff $COMMIT $COMMIT > $file - 应用 patch:
git apply $file git diff未缓存的改动git diff --cached缓存的改动git diff HEAD所有未提交的改动
12. 其他
git cherry-pick应用某一 commitgit revert反转某一 commitgit count-objects -vH计算仓库大小git blame按行查看谁最后修改git clean -fd清除 untrack 文件和目录
13. commit message 规范
text
type: Subject
body
footertype
- feat 新功能
- fix 修复错误
- docs 文档
- style 格式
- refactor 重构
- perf 性能优化
- test 测试
- chore 项目构建配置
14. 问题
git 太卡,用 git gc 优化性能。