1   三维模型等二进制文件的版本控制 git和svn适合管理文本类的代码文件,perforce更适合管理像游戏资产3D模型、psd图像文件等的大型文件。
Perforce Helix Core官网下载:点击跳转
2   linux和windows之间的文件换行符 windows中的换行符(dos)是\r\n,而linux中的换行符(unix)是\n。若不转换,git可能会视作为被更改的文件,从而造成大量无意义的commit。此外,windows中bat脚本文件要求换行符为\r\n,否则cmd解释器无法正确解析,导致无法正确执行脚本。
单文件转换:
\r\n转\n:dos2unix test.c
\n转\r\n:unix2dos test.c
批量转换:
\r\n转\n:find . -type f -exec dos2unix {} \;
\n转\r\n:find . -type f -exec unix2dos {} \;
详细命令解释:
1 2 3 4 5 6 7 8 9 10 find .: Find anything in this directory, including its subdirectories, and anything in those subdirectories as well (recursion) -type f: Only return 'regular file' names. Exclude folder names from the results. -exec: Execute the following command for every result. Everything beyond this point should be treated as part of that command until the ; character is found. dos2unix: dos2unix will be executed with the following options... -k: Keep the date stamp of the output file the same as the input file -s: Skip binary files (images, archives, etc.). This option is included by default, but I use it anyway in case that default were different on some systems (e.g. OS X v. Debian v. CentOS v. Ubuntu v. ...). -o: Write the changes directly to the file, rather than creating a new file with the data in the new format. {}: This tells find to insert the filename it has found as a parameter of the dos2unix call. ';': Tell find that the params for dos2unix have ended. Anything beyond this point will again be treated as a parameter of find. 反斜杠\是用于转移分号; '{}' is a placeholder which indicates where in the command you want the filename(s) to be inserted, and '+' terminates the said command. You can also run dos2unix once for each filename (by changing '+' with ';'), but since dos2unix accepts an arbitrary number of input arguments, it’s better to use it (as it avoids spawning many processes).
2.1   换行符问题导致git同步错误 若出现本地仓库文件和远程仓库文件出现crlf和lf不相同的问题,会导致git代码同步报错。
解决方法:使用editorconfig文件规定文档。
3   Git配置 查看设置:
3.1   git常规配置 1 2 git config --global user.name "yourname" git config --global user.email "youremail@example.com"
3.2   编辑git特定的config项 打开%USERPROFILE%\.gitconfig
文件,编辑特定的设置项即可。
3.3   代理设置 1 2 3 4 5 6 7 8 git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0.0.1:1080 git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'socks5://127.0.0.1:1080' git config --global --unset http.proxy git config --global --unset https.proxy
3.4   关于git的重命名操作 git默认对大小写不敏感,一般可通过两种方式避免git未识别大小写操作。
方式1:中间文件方法
1 2 3 git mv myfolder tmp git mv tmp MyFolder
方式2:设置git配置
1 git config core.ignorecase false
3.5   package.json
安装git仓库依赖出现报错fatal: Could not read from remote repository.
问题现象:hexo项目中,package.json
中有git仓库链接的依赖,但是尝试安装依赖时出现报错
1 2 3 4 5 6 7 8 9 10 error Command failed. Exit code: 128 Command: git Arguments: ls-remote --tags --heads git://github.com/Mister-Kin/live2d-widget.git Directory: /root/BlogSource Output: fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
问题原因:github的安全机制升级,不再支持端口9418上未经身份验证的git协议。fatal: remote error: The unauthenticated git protocol on port 9418 is no longer support
解决方法:
配置git替换git:
链接为https:
:1 2 [url "https://github.com/" ] insteadOf = git://github.com/
1 git config --global url."https://github.com/" .insteadOf git://github.com/
将package.json
中依赖的git仓库链接git://github.com/
替换为https://github.com/
。 一般来说,操作第二个步骤即可。但如果引用的git仓库链接的依赖,他们本身也有依赖于git://
协议的依赖时,只操作第二步就仍无法解决问题。需要同时配置第一个步骤的操作。
4   pull(拉取) 4.1   github从远程仓库拉取 4.2   github从上游拉取未并入主干的拉取请求(pull request) 1 2 3 4 5 6 7 8 # 添加远程上游,仓库地址填写需要拉取的上游 git remote add upstream https://github.com/USER/repository.git # ID 填pull-request id,BRANCHNAME填准备拉取到本地的某个分支名 git pull upstream pull/ID/head:BRANCHNAME # 切换到分支目录 git checkout BRANCHNAME # push到自己fork之后的上游 git push -u origin BRANCHNAME
5   clone(克隆) 5.1   常规克隆 git clone http链接(可以不包含.git后缀)
5.2   wget从Github仓库下载特定的文件夹 1 2 3 wget -O - https://github.com/Mister-Kin/PublicResources/archive/refs/heads/main.tar.gz | tar -xz -C path --strip=2 PublicResources-main/test
5.3   克隆特定分支 1 2 3 git clone --recursive -b v1.0 url git clone --recursive -b branch—name url
5.4   克隆远程仓库中的指定文件或者文件夹 以克隆blender手册翻译的简中为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 mkdir localecd localegit init git remote add -f origin https://projects.blender.org/blender/blender-manual-translations.git echo "zh-hans" >> .git/info/sparse-checkoutgit sparse-checkout set 'zh-hans' git config core.sparsecheckout true git pull origin main
6   commit(提交) 6.1   commit信息的规范化 规范编写的commit message可通过auto changelog发布release:
7   release(发布) 7.1   常规发布 github release create -a ../learngit/README.md -m "release my first program" v1.0.1
7.2   release note的规范化 Breaking Changes Changes New Features Features Bug Fixes Improvements Documentation(可缩写 Docs) 注:支持……版本,英文用 Support for …… 表示
8   添加子仓库 1 2 3 4 git submodule add https://github.com/Mister-Kin/actions.git git config --global submodule.recurse true
9   重写历史 9.1   删除所有commit 1 2 3 4 5 6 rm -rf .gitgit init git remote add origin url(仓库地址) git add . git commit -m 'commit message' git push --force origin master
注意如果使用这种方式,强制推送到远程github仓库的话,实际查看github网站的信息,貌似并没有缩小,可能数据更新有延迟。
还有另外一种方式,在github网站上删除该reop,并重新建立同名repo再推送。
9.2   从所有commit中清除特定文件 1 2 3 4 5 6 git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD git rm -rf --ignore-unmatch .git/refs/original git reflog expire --expire=now --all git fsck --full --unreachable git repack -A -d git gc --aggressive --prune=now
10   分支管理 10.1   常规操作 10.2   多人协作的工作流 多人协作注意版本控制,分支把控好,不能污染上游,开发者每个人单独分支。
10.3   常规分支管理项目 dev分支常规开发,hotfix分支临时修复
1 2 3 4 5 6 7 8 9 10 git checkout -b dev git checkout -b hotfix git checkout master git merge hotfix git branch -d hotfix
11   切换分支时有未提交的文件 应用场景:在dev分支进行开发编写,突然发现master分支有bug,想checkout分支(切换分支),但此时dev分支上有未提交的文件,并且也并不想现在提交。
解决方案:
在dev分支上git stash,储藏当前工作区 checkout到master分支进行bug修复(新建bugfix分支时,完成后合并,删除) checkout到dev分支,git stash list git stash pop,恢复工作区(该方式恢复的同时也把stash内容删除)。(另一种方式是git stash apply(恢复)后git stash drop(删除)) 12   远程仓库 12.1   重命名分支并推送到远程仓库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 git branch -m <old_name> <new_name> git push <remote> --delete <old_name> git push <remote> :<old_name> git branch --unset-upstream <new_name> git push <remote> <new_name> git push <remote> -u <new_name>
12.2   删除已经推送到远程仓库中的包含在.gitignore中的文件 .gitignore文件中添加新的文件过滤,但是该文件在之前已经提交到远程仓库分支里,我们需要将远程分支代码中的这个文件移除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 git pull git rm -r --cached . git add . git commit -m "filter new files" git push
12.3   unable to access github: OpenSSL SSL_read: Connection was reset, errno 10054 这是由于Http协议错误,刷新缓存即可解决
使用终端命令窗口运行命令:
1 2 sudo killall -HUP mDNSRespondersudo dscacheutil -flushcache
12.4   更改已提交到远程仓库的commit消息(修改错别字或添加信息) git rebase -i HEAD~n(N为最近n个提交) git会生成一个文件在编辑器打开,在需要修改的commit那一行中,将pick修改为reword,保存关闭 之后会弹出一个文件,在编辑器修改后保存关闭 之后强制push到远程仓库 12.5   RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8) 因为git文件过大造成的错误,默认缓冲区是1M大小。
解决方法:
1 git config --global http.postBuffer 524288000
12.6   已经提交Pull Request的如何二次修改覆盖 本地git reset撤销提交,然后重新完成二次修改 push -f强行覆盖掉你远程的commit 上游仓库的PR会自动更新相关内容(例如xx强制push commit) 有其他的新内容commit,push普通推送也能自动追加到上游仓库的PR中。 13   Github LFS Github LFS是收费产品,免费账户只有1G的存储空间和带宽流量(本人已弃用)。
仓库一旦使用LFS存储,无法直接通过wget或者curl工具下载原文件,只会下载到一个文本指针文件,指向LFS的存储云地址。
13.1   为Git启用LFS Windows Git LFS大文件管理:windows上使用Git for windows客户端程序时,LFS启用命令:git lfs install
。
ubuntu安装LFS包:sudo apt install git-lfs
。
13.2   LFS基本使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 git lfs track test.txt git lfs track *.txt git lfs track "*.txt" git lfs untrack *.txt git clone git pull origin main git lfs clone git lfs pull origin main git lfs uninstall git add --renormalize . git push origin master
14   CI/CD 重复繁琐的工作交予自动CI/CD完成。
15   参考文献 [1] How do I rename both a Git local and remote branch name?[EB/OL]. https://stackoverflow.com/questions/30590083/how‑do‑i‑rename‑both‑a‑git‑local‑and‑remote‑branch‑name . [2] Git 中 submodule 的使用[EB/OL]. https://zhuanlan.zhihu.com/p/614114699 . [3] Why is git submodule not updated automatically on git checkout?[EB/OL]. https://stackoverflow.com/questions/1899792/why‑is‑git‑submodule‑not‑updated‑automatically‑on‑git‑checkout . [4] git lfs安装及使用方法[EB/OL]. https://blog.csdn.net/michaelshare/article/details/83183806 . [5] Git克隆远程仓库中的指定文件或者文件夹[EB/OL]. https://blog.csdn.net/fsfsdgsdg/article/details/127177631 . [6] How to run dos2unix on all files with all extensions in a directory and its sun-directories?[EB/OL]. https://stackoverflow.com/questions/67254229/how-to-run-dos2unix-on-all-files-with-all-extensions-in-a-directory-and-its-sun . [7] How to pull a pull request from upstream in github[EB/OL]. https://stackoverflow.com/questions/54033842/how-to-pull-a-pull-request-from-upstream-in-github . [8] 详解 Git 大文件存储(Git LFS)[EB/OL]. https://zhuanlan.zhihu.com/p/146683392 . [9] Git Sparse Checkout使用指南[EB/OL]. https://blog.csdn.net/shelutai/article/details/123116973 . [10] git-sparse-checkout[EB/OL]. https://www.git-scm.com/docs/git-sparse-checkout . [11] Simple steps to uninstall Git LFS from your repository[EB/OL]. https://github.com/git-lfs/git-lfs/issues/3026 . [12] zh-cn 与 zh-hans 是什么关系、有什么区别?[EB/OL]. https://www.zhihu.com/question/21980689 . [13] github 提交pull request操作后, 如果想对代码再次进行修改,该如何操作?[EB/OL]. https://segmentfault.com/q/1010000012129041 . [14] git filter-branch 命令修改删除提示记录,删除误提交的大文件.减小.git的大小[EB/OL]. https://www.zhaokeli.com/article/8332.html .