Git Basic Notes
Git Configuration
/etc/gitconfig
.~/.gitconfig
或~/.config/git/config
.repo/.git/config
.
Basic Configuration
git config --global user.name "sabertazimi"
git config --global user.email sabertazimi@gmail.com
git config --global core.autocrlf false
git config --global core.editor vim
git config --global credential.helper store
git config --global color.ui true
git config --global init.defaultBranch main
git config --global push.default simple
git config --global merge.conflictstyle diff3
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global alias.s "status"
git config --global alias.c "commit --verbose"
git config --global alias.a "add"
git config --global alias.rs "restore --staged"
git config --global alias.st "stash"
git config --global alias.pr "pull --rebase"
git config --global alias.rpo "remote prune origin"
git config --global commit.template $HOME/.gitmsg.md
git config --global commit.gpgsign true
git config --global gpg.program gpg
git config --global user.signingkey <pub-keyID>
# after 1s, git auto correct wrong command
git config --global help.autocorrect 10
Proxy Configuration
# GitHub proxy.
git config --global url."https:/hub.fastgit.org/".insteadOf "https://github.com/"
# Socks5 proxy.
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
# Post buffer configuration.
git config --global http.postbuffer 524288000
git config --global https.postbuffer 1048576000
DNS Pollution
To fix SSL_ERROR_SYSCALL in connection to github.com:443
:
- Ensure git configuration correct.
- Ensure socks5 proxy stay working.
- Change DNS server configuration (
8.8.8.8
).
List and Help
git config --list
git --help
man git-
git help
git help config
Git Ignore File
文件 .gitignore
的格式规范如下:
- 所有空行或者以 # 开头的行都会被 Git 忽略.
- 可以使用标准的 glob 模式 (简化正则表达式) 匹配.
- 匹配模式可以以
/
开头防止递归. - 匹配模式可以以
/
结尾指定目录. - 要跟踪指定模式以外的文件或目录, 可以在模式前加上惊叹号
!
取反. - GitHub gitignore style.
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subDir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
Add
- 交互式的选择 add 特定部分
git add -p
Remove
完全删除文件
git rm filename
--cached: 保留磁盘文件(仅从 git 库移除文件)
git rm --cached filename
Move
git mv old_path new_path
Clean
Remove untracked files from the working tree:
# Remove untracked files:
git clean -f
# Remove untracked files and folders:
git clean -fd
# Dry run:
git clean -n
Commit
- -a: 跳过暂存阶段(git add)
- -v: 显示详细 diff 信息
git commit -a -v
重新提交
git commit --amend -a -v
Commit Style Guide
- Conventional Commits Specification
- Commit Linter
- Commitizen: Conventional Commits CLI Tool
- Commitizen Conventional Changelog
- Standard Version: Automate Versioning and CHANGELOG Generation
npm i -D standard-version
npx commitizen init cz-conventional-changelog --save-dev --save-exact
<type>(<scope>): <subject>
(emptyLine)
<body>
(emptyLine)
<footer>
Message Subject
no more than 50 characters
Commit Type
- feat: 新增了一个功能 (MINOR Version).
- fix: 修复了一个 bug (PATCH Version).
- docs: 只是更改文档.
- style: 不影响代码含义的变化 (空白、格式化、缺少分号等).
- refactor: 代码重构, 既不修复错误也不添加功能.
- perf: 改进性能的代码更改.
- test: 添加确实测试或更正现有的测试.
- build: 影响构建系统或外部依赖关系的更改 (示例范围: gulp, broccoli, NPM).
- ci: 更改持续集成文件和脚本 (示例范围: Travis, Circle, BrowserStack, SauceLabs).
- chore: 其他不修改 src 或 test 文件 e.g
chore(release)
. - revert: commit 回退.
Scope Values
- init
- runner
- watcher
- config
- web-server
- proxy
- empty
Message Body
- uses the imperative, present tense: “change” not “changed” nor “changes”
- includes motivation for the change and contrasts with previous behavior
Message Footer
- referencing issues e.g. close #666, #888
- BREAKING CHANGE (
<type>!
) (MAJOR Version) e.g.port-runner
command line option has changed torunner-port
, so that it is consistent with the configuration file syntax. To migrate your project, change all the commands, where you use--port-runner
to--runner-port
.
Git Commit Tool
npm i -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
git cz # replace for `git commit`
yarn add -D @commitlint/config-conventional @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
yarn add -D husky
yarn husky install
yarn husky add .husky/commit-msg 'yarn commitlint --edit "$1"'
npx husky-init
npx husky add .husky/pre-commit "lint-staged"
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{md,mdx}": ["prettier --write"]
}
}
Git Commit Emoji
Commit type | Emoji |
---|---|
Initial commit | 🎉 :tada: |
Version tag | 🔖 :bookmark: |
New feature | ✨ :sparkles: |
Bugfix | 🐛 :bug: |
Metadata | 📇 :card_index: |
Documentation | 📚 :books: |
Documenting source code | 💡 :bulb: |
Performance | 🐎 :racehorse: |
Cosmetic | 💄 :lipstick: |
Tests | 🚨 :rotating_light: |
Adding a test | ✅ :white_check_mark: |
Make a test pass | ✔️ :heavy_check_mark: |
General update | ⚡ :zap: |
Improve format/structure | 🎨 :art: |
Refactor code | 🔨 :hammer: |
Removing code/files | 🔥 :fire: |
Continuous Integration | 💚 :green_heart: |
Security | 🔒 :lock: |
Upgrading dependencies | ⬆️ :arrow_up: |
Downgrading dependencies | ⬇️ :arrow_down: |
Lint | :shirt: :shirt: |
Translation | 👽 :alien: |
Text | :pencil: :pencil: |
Critical hotfix | 🚑 :ambulance: |
Deploying stuff | 🚀 :rocket: |
Fixing on MacOS |