GIT使用笔记
安装
简单安装
1
2
yum install git -y # for centos
apt install git -y # for ubuntu
安装最新版本
有的 Linux 发行版默认安装的 git 版本较低,可能不支持一些新特性,这时可以手动安装最新版本,例如对于 CentOS 7 可以参考Install Latest Git ( Git 2.x ) on CentOS 7 | ComputingForGeeks
常用高级用法
本部分内容使用的版本:1.8.3.1
push
1
2
3
4
git push --set-upstream origin CSos-V3.2-fix14755
git push origin --delete abc
git push origin --tags
git push -f
cherry-pick
1
2
3
4
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a..adsfsas
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a^..adsfasdf
git cherry-pick -m 1 <commit> # -m 的使用说明请参见 git cherry-pick --help
show
1
2
git show efe5822
git show 07bd0aa:isos/platform/mcp/fpdetect.c >fpdetect_07bd0aa.c
log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git log /home/wangzhiqiang/SecOS-main/isos/platform/scripts/Makefile.common
git log CSos-V3.1
git log c626201..2458bf0
git log c626201^..2458bf0
git log --graph c626201
git log --author=wangzhiqiang
git log --format='xxx'
git log --minimal
git log --name-only
git log --name-status c626201
git log --oneline
git log --stat
git log --summary
git log --pretty=xxx
git log --patch # -p/-u
git log --decorate # show tags
git log --decorate --all --oneline --graph # from Newbie: git log to show tags as well?
branch
1
2
git branch --contains efe5822
git branch -D abc
clean
1
2
sudo git clean -nd .
sudo git clean -fd .
fetch
1
git fetch origin CSos-V3.1
rev-parse
1
git rev-parse --abbrev-ref HEAD
diff
1
2
git diff --name-status c626201 2458bf0
git log --name-only
add
1
2
3
4
git add a.c
git add .
git add -A .
git add -u .
commit
1
2
3
git commit a.c
git commit .
git commit --amend --author="Author Name <email@address.com>" --no-edit # 修正上一次的commit message
reset
1
2
3
git reset HEAD .
git reset <commit>
git reset --hard origin/master # 重置到远程分支最新版本
blame
1
git blame ./ipv4/appidentify/dpi/dpi_tls.c
whatchanged
git whatchanged -p v2.6.12.. include/scsi drivers/scsi
Show as patches the commits since version v2.6.12 that changed any file in the include/scsi or drivers/scsi subdirectories
git whatchanged –since=”2 weeks ago” – gitk
Show the changes during the last two weeks to the file gitk. The “–” is necessary to avoid confusion with the branch named gitk
——引用自man git-whatchanged
rebase
1
2
3
4
git checkout experiment
git rebase master
git checkout master
git merge experiment
详情参见以下链接:
tag
1
git tag <tag name> <tag name>^{} -f -a # 修改某个tag的message
describe
1
2
git describe --tags --abbrev=0 #获取latest tag
git describe --abbrev=0 # To get the most recent annotated tag
archive
1
git archive --format zip --output /full/path/to/zipfile.zip master
最佳实践
这里的“最佳实践”是指一些比较好的使用习惯
commit message
可以使用commitizen工具中的cz check
命令来检查 commit message 合规性,另可结合pre-commit-hooks实现自动检查(每当git commit
时就检查)
参见以下链接:
自动处理不合规文件
可使用一些工具自动在git commit
时判断此次操作是否有风险,如提交了大文件、搞乱了源代码文件的中文编码、混合了\n
和\r\n
、缺乏EOL、每行末尾有冗余的空白字符等
可结合使用pre-commit和pre-commit-hooks来实现上述需求
版本号
推荐使用semver
详情参见语义化版本 2.0.0 | Semantic Versioning
另可参见Software versioning - Wikipedia了解版本号这一概念
此外,建议结合git tag实现对版本号的跟踪,参见Git - Tagging以学习git tag的使用方法
可使用cz bump
自动更新版本号,详情参见Commitizen
CHANGELOG
可通过规范化的 commit message 自动生成。如使用commitzen
工具中的cz ch
命令即可实现
详情参见Commitizen
遇到过的问题
git show
查看指定提交的指定文件的内容?
1
git show REVISION:/path/to/file
详情参见How to view a file at a specific commit in git? - SysTutorials
git stash
How can I git stash a specific file?
参见How can I git stash a specific file? - Stack Overflow
git log
sort by date?
参见Can you order git log by commit timestamp? - Stack Overflow
How to configure ‘git log’ to show ‘commit date’?
git log - How to configure ‘git log’ to show ‘commit date’ - Stack Overflow
Why git AuthorDate is different from CommitDate?
Why git AuthorDate is different from CommitDate? - Stack Overflow
删除远程分支?
git push origin –delete abc
git format-patch
How can I generate a Git patch for a specific commit? - Stack Overflow
git commit
修订记录
修订时间 | 修订人 | 版本 | 说明 |
---|---|---|---|
2021-02-03 | wsxq2 | 1.0 | 初稿 |