git使用指南01

本文记录一下使用git时的常见操作。

1.git remote -v:查看当前项目远程地址。


2.echo $PATH | tr ":" "\n":在git bash终端中分行展示环境变量。

参考资料:How to split the contents of $PATH into distinct lines


3.在未将修改的本地文件推送到远程仓库的状态下,通过 git pull 拉取远程仓库的内容时会出现conflicts,如下图所示:

image-20220516212248506

此时可以通过如下步骤解决conflicts

  • git fetch origin:下载远程分支的所有变动,但不与本地分支合并

  • git pull origin hexo:下载远程hexo分支的所有变动并与本地分支合并。此时会出现如下情形

    image-20220516212734022

  • git status:查看当前分支状态。

    image-20220516212832926

  • git add ./:添加指定目录到暂存区,包括子目录

  • git commit -m "update":提交暂存区到仓库区

  • git pull origin hexo:发现Already up to date.

  • git status:发现nothing to commit, working tree clean

参考资料:How do I resolve merge conflicts in a Git repository?


4.在文件或者文件夹已经存在在仓库中时,将这些文件或者文件夹加入.gitignore文件后,git 并不会将这些文件或者文件夹删除。此时可以通过以下步骤使.gitignore中的改动生效:

  • git rm -rf --cached path_to_file:将对应路径的文件从仓库缓存中删除;
  • git add ./
  • git commim -m "add a commit"
  • git push origin main

参考资料:

  1. Gitignore not working
  2. Can you have multiple Gitignore files?

PS:可以为仓库中的每个文件夹创建一个.gitignore,但是并不建议,因为不方便查询和管理。


5.给 git 配置代理:

  • 配置 socks 协议代理:

    1. 设置代理:
git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'
  1. 查看代理:
git config --global --get http.proxy
git config --global --get https.proxy
  1. 取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
  • 配置 http 协议代理:

    1. 设置代理:
git config --global http.proxy 'http://127.0.0.1:1080' 
git config --global https.proxy 'https://127.0.0.1:1080'
  1. 查看代理:
git config --global --get http.proxy
git config --global --get https.proxy
  1. 取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
  1. 查看代理是否生效
curl https://www.google.com

PS:1080 为在 ShadowSocksR 或者 V2rayN 客户端中设置的代理的端口;Windows V2rayN 客户端似乎不支持 http 协议代理(注意在 git 更新 personal access token 时不支持 socks 协议)

参考文献:

  1. git设置、查看、取消代理

6.有时需要在.gitignore文件中添加仓库中所有的名为folder_name文件夹或文件,此时可以通过在.gitignore中添加如下内容实现:

*folder_name*

例如,要忽略所有名为datafilepth的文件夹或文件,可以在.gitignore中添加:

*datafile*
*pth*

PS:此种方式由于使用了极其宽松的正则表达式,凡是文件夹名或文件名中包含datafilepth的连续字符串都将被忽略。


7.同时进行多个项目的开发时,对git commmit -m "msg"中的msg没有过多要求的情况下,可以通过Windows .bat脚本对多个项目进行批量的git pullgit push

  • git pull

    @echo off
    echo "batch git pull"
    D:
    
    echo "moving to D:\Projects\AndroidProjects"
    cd D:\Projects\AndroidProjects
    echo git pull D:\Projects\AndroidProjects >>D:\Desktop\pull.txt
    git pull >>D:\Desktop\pull.txt
    echo "D:\Projects\AndroidProjects git pull finish"
    echo= >>D:\Desktop\pull.txt
    echo=
    
    ...
    
    pause

    上面演示了对AndroidProjects项目进行git pull操作,并将相关输出记录到pull.txt文件。最后的pause命令使得执行.bat脚本执行完毕后停留在cmd页面。

  • git push

    @echo off
    echo "batch git push"
    D:
    
    echo "moving to D:\Desktop\Tom89757.github.io"
    cd D:\Desktop\Tom89757.github.io
    echo git push D:\Desktop\Tom89757.github.io >>D:\Desktop\push.txt
    git add ./ >>D:\Desktop\push.txt
    git commit -m "update" >>D:\Desktop\push.txt
    git push origin hexo >>D:\Desktop\push.txt
    echo "D:\Desktop\Tom89757.github.io git push finish"
    echo= >>D:\Desktop\push.txt
    echo= >>D:\Desktop\push.txt
    echo=
    cd D:\Desktop\Tom89757.github.io\blog
    hexo d -g
    
    pause

    上面演示了对Tom89757.github.io项目进行git push操作,并将相关输出记录到push.txt文件。并在完成git push操作后,对blog中的内容进行生成和部署。

参考资料:

  1. Windows 下bat脚本git提交代码到github
  2. bat 批处理教程
  3. bat脚本echo命令输出空行的11种方法和效率

8.当git push较大文件(大于50M)时,会出现如下warning
此时可以通过git lfs来解决,其步骤如下:

  1. 安装git bash,运行git lfs install。注意对每个user account只运行一次
  2. 在每个你想要使用Git LFS的仓库,训责你想要用Git LFS管理的文件类型(或者直接编辑你的.gitattributes文件),可以在任何使用配置额外的扩展文件类型。例如:git lfs track ".pptx"。将.pptx文件类型添加到Git LFS管理的文件类型中。
  3. 上述操作会在当前仓库根目录下添加.gitattributes文件,其内容如下:
  4. 使用git add .gitattributes便可使Git LFS生效。

参考资料:

  1. Git Large File Storage

9.使用git bash进行javac编译时出现中文乱码:

解决方案:在选项里将字符集设为GBK,重启git bash:

参考资料:

  1. 在 git bash 里运行 java 命令,打印出的中文显示乱码
  2. 解决 Git Bash 在 windows 下中文乱码的问题(不针对此问题)

10.有时需要给git终端设置别名。给git设置别名分为两种: - 给git本身的命令设置别名,此时可以通过`git config --global alias.co checkout`设置全局别名,这样可以`git co`等同于`git checkout`,该配置会写入`~/.gitconfig`文件。故也可以直接编辑该文件来设置别名 - 给git终端运行的其他命令设置别名,如`javac -encoding utf8`简化为`javac`,此时可以编辑`~/.bashrc`文件(如果没有则创建),在里面写入`alias javac='javac -encoding utf8'`。设置后每次启动git终端窗口后该文件中的配置都会生效

参考资料:

  1. 2.7 Git 基础 - Git 别名
  2. 玩转 Git 别名

11.出现报错`error: RPC failed; curl 92 HTTP/2 stream 7 was not closed cleanly before end of the underlying stream`:


解决方案:

  1. 配置http版本:git config --global http.version HTTP/1.1
  2. 配置http.postBuffer:git config --global http.postBuffer 157286400

参考资料:

  1. git - error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) - Stack Overflow

12.在linux中使用git时,出现如下情况: ![](https://raw.githubusercontent.com/Tom89757/ImageHost/main/hexo/20230203180529.png) 解决方案:在Linux的git中配置personal token - `git config --global credential.helper cache`:在`.gitconfig`中添加配置,使得系统记住后续输入的token,避免重复输入 - `git clone https://github.com/Tom89757/dotfiles.git`:clone对应仓库,并输入对应用户名和token。由于上述配置,此次输入后后续无需再次输入。

参考资料:

  1. git - Message “Support for password authentication was removed. Please use a personal access token instead.” - Stack Overflow

13.clone仓库时出现:
Host key verification failed. fatal: The remote end hung up unexpectedly
解决方案:
git config --global user.name "你的github账户名"
git config --global user.email "你的github账户默认的邮箱地址"
ssh-keygen -t rsa -b 4096 -C "你的github账户默认的邮箱地址"
cat ~/.ssh/id_rsa.pub # 添加到git ssh
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts 
> 参考资料: > 1. [我的现代化Neovim配置 - 知乎](https://zhuanlan.zhihu.com/p/382092667) > 2. [ssh - Git error: "Host Key Verification Failed" when connecting to remote repository - Stack Overflow](https://stackoverflow.com/questions/13363553/git-error-host-key-verification-failed-when-connecting-to-remote-repository)
14.下载仓库中某个文件夹: 工具:[Download GitHub directory](https://download-directory.github.io/)

15.在运行git push origin main之后,在出现Total行之后卡住

问题:推送的objects较大
解决方案:参考下方参考资料

参考资料:

  1. bitbucket - git push hangs after Total line - Stack Overflow

16.查看分支的最早提交时间:
切换到对应分支例如w6-complete然后运行如下命令:

git log --reverse --pretty=format:"%cd" --date=iso w6-complete | tail -1

PS:上述命令只能查看w6-complete的最早提交时间,而不是分支的创建时间

参考资料:VSCode Copilot


17.`git log`显示commits时间并倒序输出到`commits.txt`:
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short | tac >commits.txt

18.git添加PATH: > 参考资料: > 1. [node.js - How come npm install doesn't work on git bash - Stack Overflow](https://stackoverflow.com/questions/35915832/how-come-npm-install-doesnt-work-on-git-bash) > 2. [node.js - Http-Server Command Not Found After Install in NPM - Stack Overflow](https://stackoverflow.com/questions/31694646/http-server-command-not-found-after-install-in-npm#:%7E:text=you%20should%20add%20the%20npm,%22%2C%20run%20the%20below%20command.&text=bashrc%20file%20in%20your%20home,commands%20on%20your%20git%20bash)

19.git配置pager:
GitHub Copilot: Git pager是指Git命令行工具中用于分页显示输出的程序。Git pager通常用于显示长时间运行的命令的输出,例如git log或git diff。当输出超过终端窗口的大小时,Git pager会自动将输出分页显示,以便您可以逐页查看输出。

默认情况下,Git pager使用less程序作为分页程序。您可以使用git config命令来配置Git pager的选项。例如,您可以使用以下命令将Git pager配置为使用more程序而不是less程序:

git config --global core.pager more

您还可以使用以下命令来禁用Git pager:

git config --global core.pager ''

这将使Git命令在终端窗口中直接输出所有内容,而不是分页显示。

参考资料:

  1. How do I prevent ‘git diff’ from using a pager? - Stack Overflow

20.git bash使用`git diff`,不显示任何东西。 问题:错误的使用方式 解决方案:按照下述流程使用:
git add file_name

git diff --cached file_name
> 参考资料: > 1. ['git status' shows changed files, but 'git diff' doesn't - Stack Overflow](https://stackoverflow.com/questions/14564946/git-status-shows-changed-files-but-git-diff-doesnt)
21.git显示警告:
warning: lF will be replaced by CRLF.
解决方案:
git config --global core.autocrlf false
> 参考资料: > 1. [Windows git "warning: LF will be replaced by CRLF", is that warning tail backward? - Stack Overflow](https://stackoverflow.com/questions/17628305/windows-git-warning-lf-will-be-replaced-by-crlf-is-that-warning-tail-backwar)
22.git设置符号链接(软链接 `ln -s`): - 打开Windows 10中的开发者模式("Developer Mode"),从而给`mklink`权限 - 使得git中symbol links生效:
git config --global core.symlinks true # 全局生效
git config core.symlinks true # 当前仓库生效
- 添加symbol link链接:
mklink C:\Users\26899\.bash_aliases D:\Desktop\dotfiles\git\.bash_aliases
- 添加文件夹链接:

mklink /d D:\Desktop\dotfiles\git\.oh-my-zsh C:\Users\26899\.oh-my-zsh
> 参考资料: > 1. [Symbolic link does not work in Git over Windows - Super User](https://superuser.com/questions/1713099/symbolic-link-does-not-work-in-git-over-windows) > 2. [Git symbolic links in Windows - Stack Overflow](https://stackoverflow.com/questions/5917249/git-symbolic-links-in-windows)
23.git bash配置定制(custom)的`git-prompt.sh`文件: 1. 找到`git-prompt.sh`文件,在Git安装目录下。 2. 将其复制到`~/.config/git/`目录下。 3. 即可编辑上述复制的`git-prompt.sh`文件对git prompt进行定制。 PS:同理,可以对使用oh-my-zsh的git prompt进行定制,其文件位于`/c/Users/26899/.oh-my-zsh/plugins/gitfast/git-prompt.sh`。 > 参考资料: > 1. [How to change the display name in Git bash prompt](https://www.brainstormcreative.co.uk/git-bash/how-to-change-the-display-name-in-git-bash/)
24.git bash中使用`tree`命令。在`.bash_aliases`中添加:
alias tree='cmd //c tree //a'
> 参考资料: > 1. [How to add the 'tree' command to git-bash in Windows? - Super User](https://superuser.com/questions/531592/how-to-add-the-tree-command-to-git-bash-in-windows)
25.git bash中使用`trash`替代`rm`,类似回收站。
npm install --global trash-cli
> 参考资料: > 1. [trash-cli - npm](https://www.npmjs.com/package/trash-cli)
26.使用`mklink`命令将`.vimrc`和`monokai.vim`等文件对`dotfiles`仓库中的文件进行软链接:
mklink C:\Users\A\.vimrc D:\Desktop\dotfiles\git\.vimrc
mklink C:\Users\A\.vim\colors\monokai.vim D:\Desktop\dotfiles\git\.vim\colors\monokai.vim

27.使用`mklink`将`.zshrc`,`.bash_aliases`,`.bash_path`,`.bashrc`,`.bash_profile`链接到`dotfiles`仓库中的文件。
mklink C:\Users\A\.zshrc D:\Desktop\dotfiles\git\.zshrc
mklink C:\Users\A\.bash_aliases D:\Desktop\dotfiles\git\.bash_aliases
mklink C:\Users\A\.bash_path D:\Desktop\dotfiles\git\.bash_path
mklink C:\Users\A\.bash_profile D:\Desktop\dotfiles\git\.bash_profile

28.将git配置文件链接到`dotfiles`仓库:
mklink C:\Users\A\.gitconfig D:\Desktop\dotfiles\git\.gitconfig
mklink D:\Develop\Git\etc\gitconfig D:\Desktop\dotfiles\git\gitconfig

29.配置`git-delta`,高亮工具,可以使得`git`, `diff`和`grep`等输出高亮。

参考资料:

  1. GitHub - dandavison/delta: A syntax-highlighting pager for git, diff, and grep output
  2. git-delta 终端代码 diff 也很酷!_哔哩哔哩_bilibili

30.git新建分支,不保留当前分支文件:
git switch --orphan <new-branch-name> 
> 参考资料: > 1. [git - Create empty branch on GitHub - Stack Overflow](https://stackoverflow.com/questions/34100048/create-empty-branch-on-github)