简介
Git 是 Linux 之父设计的分布式版本控制软件,最出色的代码管理利器。 Git不只能用于代码的管理,一切配置文件、文本档案,甚至多媒体大文件,都很适合用Git来追踪变化,异步写作开发。 本文介绍 Git 的基本操作,并记录一些复杂需求的操作流程。
Discern
Github 是一个使用Git机制提供集中代码和文档追踪服务的平台,和Git没有官方联系。
安装
- Windows
- Windows版本在Github分发
- 2.46.2是最后支持Windows7的版本
- 使用setup安装时建议换行符方式选择
Checkout as is, commit as is
- Windows版本在Github分发
- Linux发行版仓库中一般带有 git
- 建议从发行版仓库安装
相关配套工具和插件
- git-lfs
- 集成在Windows版本中,Linux需额外从发行版仓库安装。
- 用于追踪大文件减少空间消耗
配置
配置文件位置
- 当前用户的配置在
.gitconfig - 每个仓库的配置文件在
.git/config - 每个文件夹中的配置文件
- .gitignore 设置追踪变更时忽视的路径
- .gitattribution 设置每个文件追踪属性如不记录差异
配置命令
git config [级别] [选项] 变量 值
- 级别有 –system –global –local –worktree –file 等
- 选项有 –list –unset 等
- 全局快速合并 git config –global pull.ff=only
- 设置基本信息
- git config user.name [用户名]
- git config user.email [电子邮件]
- 设置http/https协议的代理 git config http.[网址.]proxy “协议://IP地址:端口”
- git config http.proxy “socks5h://127.0.0.1:1080”
- git config http.https://github.com.proxy “socks5h://127.0.0.1:1080”
- git config –local remote.<远端名称>.proxy ""
- 设置SSH(代理用ssh方式设置)
- git config –local core.sshCommand “ssh -F path/to/config”
- SSH远程登入神器的初步使用
基本使用
创建本地仓库
|
|
克隆和拉取
- 子模块
- 直接一起克隆 git clone –recurse-submodules
- 克隆后在拉取
git clone 链接git submodule initgit submodule update--remote选项可以解决did not contain xxxxxxxxx类的问题
- 精简克隆
- 设置不包含文件变更暂不检出(仅概况)深度为1
git clone --filter=blob:none --no-checkout --depth=1 --sparse 链接 cd 仓库名git sparse-checkout set 想克隆的路径git sparse-checkout add 想克隆的路径- 手动维护想克隆的路径
.git/info/sparse-checkout git checkout- https://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository
- 设置不包含文件变更暂不检出(仅概况)深度为1
提交
|
|
分支
|
|
流程参考
PullRequest
通常实在网络平台上使用git用此操作进行贡献
- fork别人的仓库
- 做修改并提交
- 在自己fork的仓库新建PullRequest
- [可选]跟进上游进展 git remote add upstream https://[上游仓库地址].git
GitLFS
- 追踪
git lfs track 文件在仓库路径如Patch/test.bin - 添加配置
git add .gitattributes - 正常添加
it add 文件在仓库路径如Patch/test.bin - 正常提交
git commit -m "说明备注"
不小心提交敏感信息
- 本地撤销commit
git reset --soft HEAD^ - 创建新分支
git branch -b master - 重新提交
git push - 在github中Settings切换默认分支
- 在github中Code的分支菜单中
View all branches删除原分支 - 可以考虑再把新分支master重命名回去
1 2 3 4git branch -m master 原分支名 git fetch github git branch -u github/原分支名 原分支名 git remote set-head github -a
其他
Git相关的 GUI、WebUI、Server 等另开一篇博文介绍。