Git
- 改变 Commit 习惯
- vim - How do I make git use the editor of my choice for editing commit messages? - Stack Overflow
git config --global core.editor "vim"Alternatives
- https://learnxinyminutes.com/docs/zh-cn/git-cn/
- https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
Config
ssh: Username & email & key
git config --global user.name 'HX'
git config --global user.email '[email protected]'
ssh-keygen -t rsa -C '[email protected]'
# Check connect to github
ssh -Tv [email protected]
# Paste following to github setting
cat ~/.ssh/id_rsa.pubCase sensitive
git config core.ignorecase false
# https://stackoverflow.com/questions/71230581
# https://juejin.cn/post/7135422871735631902Commit
Init
git initClone
git clone ssh://[email protected]/repo.git
git clone -b main --single-branch ssh://[email protected]/repo.gitStatus
git status
git diff # Changes to tracked filesAdd
git add .Commit
git commit
git commit -m "feat(xxx): xxx"3 种 Git 规范
Add / Fix / ....首选动词,并且首字母大写,尽量简洁描述自己的内容,比较简陋;fix: xxx / feat: xxx / docs: xxx每次都需要选一个类似这样的 tag 打上去,方便后面检索;:tada: xxx / :books: xxx / bug: xxx利用 Github 实现的 Emoji 作为 tag,类似 2,并且可以在 Github 上显示更好?via: https://stackoverflow.com/questions/9742073/graphics-in-github-commit-messages
feat: add hat wobble
^--^ ^------------^
| |
| +-> Summary in present tense.
|
+-------> Type: chore, docs, feat, fix, refactor, style, or test.feat- 添加新特性、新功能( feature )
fix- 修复 bug
docs- 仅仅修改了文档
style- 仅仅修改了空格、格式缩进、逗号等等,不改变代码逻辑
refactor- 代码重构,没有加新功能或者修复 bug
perf- 性能测试
test- 测试用例
chore- 改变构建流程、或者增加依赖库、工具等
More via: https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13
History
git log
git log --oneline
git blame <file>Push & Pull
git pull <remote> <branch>
git push <remote> <branch>
git remote -v # List all currently configured remotes
git remote show <remote> # Show information about a remote
git remote add <shortname> <url> # Add new remote repository, named <remote>
# git submodule add <url>/<ssh>
git fetch <remote> # Download all changes from <remote>
git branch -dr <remote/branch> # Delete a branch on the remote
git push --tags # Publish your tagsgit fetch + git merge == git pull via https://www.jianshu.com/p/a5c4d2f99807

git fetch origin master
git log -p master..origin/master
git merge origin/master
git fetch origin master:tmp
git diff tmp
git merge tmp
# via: https://blog.csdn.net/hudashi/article/details/7664457Rollback
Warning
不要用
git reset --hard (versionHash), 修改会清空!
Add
git reset --mixed
# 保留修改, 退出暂存区, --mixed 为默认参数
git reset HEAD
# 撤销 add (绿字变红字)
git checkout -- (file)
# 撤销没 add 的修改 (红字变无)Commit
git reset --soft HEAD^
# 不删除工作空间改动代码, 撤销 commit, 不撤销 git add
git commit --amend
# commit 注释写错了, 只想改一下注释--hard
恢复到了上一次的 commit 状态,删除工作空间改动代码,撤销 commit,撤销 git add .
All things
git reflogMerge
jetbrains Gui tools
外部程序直接可以调用 IDEA 的 Git Merge 工具;
[diff]
guitool = intellij
[difftool "intellij"]
path = C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2023.3.2/bin/idea.bat
cmd = \"C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2023.3.2/bin/idea.bat\" diff \"$LOCAL\" \"$REMOTE\"
[merge]
guitool = intellij
[mergetool "intellij"]
path = C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2023.3.2/bin/idea.bat
cmd = \"C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2023.3.2/bin/idea.bat\" merge \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"并非银弹
via: https://www.xttblog.com/?p=5294 Untracked Files Prevent Checkout* Move or commit them before checkout
Command-line
git merge <branch>
git rebase <branch>
git rebase --abort
git rebase --continue
git mergetool
git add <resolved-file>
git rm <resolved-file>rebase check https://juejin.cn/post/7038093620628422669
Force reset merge other branch
git reset --hard dev/1197via: https://stackoverflow.com/a/59238409
Submodules
git submodule add https://github.com/yyy/xxx.git
git submodule status
git submodule deinit
git clone https://github.com/yyy/xxx.git --recursive #子模块也一起 clone, or using for
git submodule init && git submodule update # euqal following
git submodule update --init
git submodule update --init --recursive # 递归添加via: https://knightyun.github.io/2021/03/21/git-submodule
Remove it
git rm --cached path/to/submodule
rm -rf path/to/submodule然后注释 .gitmodules 相关配置即可
Brach
git branch -av
git checkout <branch> # Switch HEAD branch
git branch <new-branch> # Create a new branch based
git checkout --track <remote/branch> #Create a new tracking branch based on a remote branch
git branch -d <branch> # Delete a local branchTag
git tag <tag-name>
git log # list all tags
git tag -l "v1.8.5*" # list with regex
git tag -a v1.4 -m "my version 1.4" # annotated 附注标签
git push origin v1.5 # push sepecific
git push origin --tags # push alltag: 每一个正式发布的版本 merge 到 master 并且打一个 tag
用例:利用打 tag 自动部署一次应用
via: https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE
History remove
Install https://github.com/newren/git-filter-repo
pip3 install --trusted-host pypi.tuna.tsinghua.edu.cn -i https://pypi.tuna.tsinghua.edu.cn/simple filter-repoRemove folder
git filter-repo --path pdf --invert-pathsRemove file
git filter-repo --path readme.md --invert-paths --forceForce Push
# below is used to add a new remote:
git remote add "origin" [email protected]:xxx/xxx.git
# below is used to change the url of an existing remote repository:
git remote set-url "origin" [email protected]:User/UserRepo.git
# via: https://stackoverflow.com/questions/42830557
git push origin dev --forceNotes
revert Vs checkout vs reset
revert → commit level, no file level
safe operation for ‘public undos’ as it creates new history which can be shared remotely and doesn’t overwrite history remote team members may be dependent on.
checkout → commit level & file level
A checkout is an operation that moves the HEAD ref pointer to a specified commit
reset
A reset is an operation that takes a specified commit and resets the “three trees” to match the state of the repository at that specified commit.
three trees → working directory & stagged snapshot & commit History
three different modes??
checkout and reset are generally used for making local or private ‘undos’. They modify the history of a repository that can cause conflicts when pushing to remote shared repositories.
git reset --hard HEAD
git checkout HEAD <file> # Discard local changes in a specific file
git revert <commit> # Revert a commit (by producing a new commit with contrary changes)
git reset --hard <commit> # Reset your HEAD pointer to a previous commit …and discard all changes since then
git reset <commit> # and preserve all changes as unstaged changes
git reset --keep <commit> # and preserve uncommitted local changesvia: https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting
Remote add origin vs remote set-url origin
below is used to add a new remote:
git remote add "origin" [email protected]:User/UserRepo.gitbelow is used to change the url of an existing remote repository:
git remote set-url "origin" [email protected]:User/UserRepo.gitbelow will push your code to the master branch of the remote repository defined with "origin" and -u let you point your current local branch to the remote master branch:
git push -u origin mainDocumentation https://stackoverflow.com/questions/42830557/git-remote-add-origin-vs-remote-set-url-origin
Emojis supported by github 1
| 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: |
| Translation | 👽 :alien: |
| Text | 📝 :pencil: |
| Critical hotfix | 🚑 :ambulance: |
| Deploying stuff | 🚀 :rocket: |
| Fixing on MacOS | 🍎 :apple: |
| Fixing on Linux | 🐧 :penguin: |
| Fixing on Windows | 🏁 :checkered_flag: |
| Work in progress | 🚧 :construction: |
| Adding CI build system | 👷 :construction_worker: |
| Analytics or tracking code | 📈 :chart_with_upwards_trend: |
| Removing a dependency | ➖ :heavy_minus_sign: |
| Adding a dependency | ➕ :heavy_plus_sign: |
| Docker | 🐳 :whale: |
| Configuration files | 🔧 :wrench: |
| Package.json in JS | 📦 :package: |
| Merging branches | 🔀 :twisted_rightwards_arrows: |
| Bad code / need improv. | 💩 :hankey: |
| Reverting changes | ⏪ :rewind: |
| Breaking changes | 💥 :boom: |
| Code review changes | 👌 :ok_hand: |
| Accessibility | ♿ :wheelchair: |
| Move/rename repository | 🚚 :truck: |
| Other | Be creative |
Links
- https://github.com/liangzr/github-run
- https://github.com/sallar/github-contributions-canvas
- https://gist.github.com/joshbuchea/6f47e86d2510bce28f8e7f42ae84c716
- https://www.conventionalcommits.org/
- https://seesparkbox.com/foundry/semantic_commit_messages
- http://karma-runner.github.io/1.0/dev/git-commit-msg.html