跳到主要内容

Git

获取远程分支并切换到该分支​

git checkout -b branchName origin/branchName
// 切换不成功,先拉下代码

Review pull request​

git  co -b review/xxx
# 添加别人的 repo 源
git remote add [源简称] https://github.com/[username]/[repo]
git pull [源简称] [分支]

Review 后,删除源

git remote remove [源简称]
git remote -v

跳过 git hooks 的 lint 检查​

git commit --no-verify -am 'xxx'

不改代码,触发重新运行 git action​

git commit --amend --no-edit                             
git push --force-with-lease

SubModules​

Git Submodules

拿最新的 SubModules 代码​

git submodule update

更新 SubModules 代码​

git submodule sync

Sync fork repo latest code​

git remote add [源简称] https://github.com/原仓库地址.git
git fetch [源简称]
git merge upstream/main

一般 源简称 叫 upstream。

获取远程分支​

git fetch origin branchName:branchName

删除本地分支​

git branch -D branchName

强制回滚到某个版本​

git reset --hard commitId
git ph -f

暂存和恢复​

git stash # 暂存
git stash pop # 恢复

用 rebase 的方式合并分支​

以 master 为基,把 master 合并到 feature/a 为例。和 merge 的命令类似:

git checkout master
git pl
git checkout **feature/a**
git pl
git rebase master
git push --force-with-lease
# 自己开发的分支,可以这么做。rebase 改了提交顺序。本地分支和远程分支不一样了。
# 否则,git pull 来合并吧。

将多次 Commit 合并成一次​

# 1 rebase
git rebase -i commitId # 要 合并 的前一次。
# git rebase -i HEAD~3 # 最后个数字是合并几次。

# 2 编辑内容
# 2.1 将第一个 commitId 前的 pick 改成 r
# 2.2 将其他的 commitId 前的 pick 改成 f

## 3 修改提交信息
# 2 结束后,会出现编辑器来改提交信息。

## 4 强推
git push --force-with-lease # rebase 改了提交顺序。本地分支和远程分支不一样了。

合并分支时,将多次 Commit 合并成一次​

git merge --squash 分支名

其他分支合并策略: 图解4种git合并分支方法。

合并其他分支的某次提交​

git cherry-pick commitID

提交信息前缀​

  • feat: new feature for the user, not a new feature for build script
  • fix: bug fix for the user, not a fix to a build script
  • docs: changes to the documentation
  • style: formatting, missing semi colons, etc; no production code change
  • refactor: refactoring production code, eg. renaming a variable
  • perf: add code for performance test
  • test: adding missing tests, refactoring tests; no production code change
  • chore: updating grunt tasks etc; no production code change

配置别名​

git config --global alias.st status
git config --global alias.pl 'pull --all --rebase' # 用 rebase 来合并代码
git config --global alias.aa 'add -A'
git config --global alias.ci commit
git config --global alias.ca 'commit -am'
git config --global alias.caa 'commit -am "just add"'
git config --global alias.ph push
git config --global alias.co checkout
git config --global alias.br branch

所有配置​

git config --list

改了 Git 密码后的处理​

Mac 会默认会从密钥链里拿。git config 里会有一句

credential.helper=osxkeychain

密码改后, 要删除旧的凭证:Keychain access > 搜索 github.com(或其他代码托管网站) > "互联网密码“> 删除。再做一次,会要求输用户名和密码,更新到新的凭证。

具体见 这里。

SSH Key 位置​

mac:

open ~/.ssh

PR​

文档​