前言
自用 git常用操作记录
配置代理
git config --global https.proxy 172.16.118.16:7890
git config --global http.proxy 172.16.118.16:7890
git config --global --unset https.proxy
git config --global --unset http.proxy
子仓相关
更新子仓
git submodule update --remote
更新子仓并指定为当前仓库的提交时所用的子仓commit
git submodule update --init --recursive --remote
添加子仓
git submodule add -b <分支名> <仓库URL> <目标路径>
删除子仓
git submodule deinit submodules/submodule-repo
移动子仓
git config -f .gitmodules submodule.<submodulename>.path <new_path>
git mv <old/location> <new/location>
清除修改
在未发生任何add或commit的情况下: 未跟踪文件 不管
git checkout .
强制清除所有新建的文件及文件夹, -d表示包括空文件夹
git clean -df
对于add的部分,先要撤销add:
git reset .
git reset --hard
对于不小心commit的
git reset HEAD^ 回到上一次提交
到指定commit版本
git reset --hard <commit_number>
stash
git stash 保存当前脏状态, 需要时可恢复
git stash list ,查看list
git stash drop stash@{no} 删除
git stash clear 一次性删除所有stash
如果git stash, 但删除了该分支, stash记录不会丢掉,仍在list里, 但可能无法访问
git stash pop 恢复最近一次状态
查询变更记录
查询某行代码最近一次commit中变更
VScode用GitLens插件更方便
git blame -L <line_number> <file_path>
修改/删除分支
修改分支名,远程修改
git branch -m oldname new_name
git push origin :oldname new_name
删除指定tag
git tag -d <tag-name>
git push --delete origin <tag-name>
批量删除本地分支
git branch | grep '筛选字符串' | xargs git branch -D
cherry-pick
git checkout 需要应用的分支
git cherry-pick <commitHash> <commitHash2> 在该分支应用commit的改动, 支持多个