Skip to content

git 常用命令备忘

Published: at 09:50

git tag 常用笔记

查看 tag

新建 tag

推送 tag

删除 tag

参考链接

alias

Terminal window
git config --global alias.lg "log --graph --pretty=tformat:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --decorate=full"

“git lg” alias for pretty git log

gitignore

以下回答来自 GPT4

在 Git 中使用 .gitignore 文件排除特定文件或文件夹后,如果需要重新包含某些被排除的内容,可以通过以下步骤实现,以特定路径的 x64 文件夹为例

1 编辑 .gitignore 文件: 如果你只想排除大部分 x64 文件夹,但保留特定的 x64 文件夹,可以在 .gitignore 文件中进行更精细的配置。例如,假设你有一个 x64 文件夹在 path/to/ 目录下,你可以这样修改 .gitignore 文件:

# 排除所有 x64 文件夹
**/x64/
# 但排除特定 x64 文件夹
!path/to/x64/

2 使用 git add -f 命令:

如果你已经提交了 .gitignore 文件,并且想要强制添加被忽略的文件或文件夹,可以使用 git add -f 命令。例如:

Terminal window
git add -f path/to/x64

3 使用 git check-ignore 命令: 如果你不确定某个文件或文件夹是否被忽略,可以使用 git check-ignore 命令进行检查:

Terminal window
git check-ignore -v path/to/x64

其他命令

使用解决冲突的方式合并,将避免简单的自动合并

Terminal window
git merge origin/dev --strategy=resolve

清理本地已经合并到 dev 的分支

Terminal window
git branch --merged | grep -v 'dev' | xargs -n 1 git branch -d

分支清理

Git 之删除本地无用分支_dearfulan 的博客 - CSDN 博客_git 删除本地无效分支

Terminal window
git fetch -p
git remote prune origin

查看两个分支的最近共同祖先

Terminal window
git merge-base [-a|--all] <commit> <commit>…

包含某个提交的分支或TAG

Terminal window
git branch --contains <commit>
git branch -r --contains <commit>
git tag --contains <commit>

统计 git 提交数

Terminal window
git rev-list --count HEAD

git 列出 HEAD 的 提交号

Terminal window
git rev-parse HEAD
# 如果只是查看,则直接使用 show 命令就可以
git show HEAD

git 别名

Terminal window
git config --global alias.co checkout
git config --global alias.lg log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

换行符问题

Git 多平台换行符问题 (LF or CRLF)

提交检出均不转换

Terminal window
git config --global core.autocrlf false

允许提交包含混合换行符的文件

Terminal window
git config --global core.safecrlf false

查看提交日志

Terminal window
git reflog show --color --abbrev-commit --all --pretty=oneline --date=short --after="2020-05-22" | grep 'commit' | grep -v 'refs'

空提交

Terminal window
git commit -m "version + 2" --allow-empty

其他

Git LFS 操作指南


原文链接: https://blog.jgrass.cc/posts/git-command-note/

本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。