覆盖在学习和工作中的常用 Git 命令:配置 / 仓库 / 查看 / 暂存 / 提交 / 回退 / 克隆 / 拉取 / 推送 / 合并 / 冲突 / 分支 / 标签 / 工具
目录 Table of Contents
配置
账号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
git config --global user.name "${user_name}" git config --global user.email "${user_email}"
git config --local user.name "${user_name}" git config --local user.email "${user_email}"
git commit --amend --author="${user_name} <${user_email}>" git commit --amend --reset-author
|
密钥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
ssh-keygen -t rsa_github ssh-keygen -t rsa_gitlab
vim ~/.ssh/config
Host github.com HostName github.com User git IdentityFile ~/.ssh/rsa_github Host gitlab.com HostName gitlab.com User git IdentityFile ~/.ssh/rsa_gitlab
:wq
|
换行
1 2 3 4 5 6 7 8
| git config --global core.autocrlf true
git config --global core.autocrlf input
git config --global core.autocrlf false
|
仓库
查看
新建
1 2
| git remote add ${origin_name} git@host:/path/to/registry.git
|
删除
1
| git remote rm ${origin_name}
|
查询
当前状态
差异分析
工作区 vs 暂存区
1
| git diff (${branch_name}) (${file})
|
暂存区 vs 版本库
1
| git diff --cached (${commit_id}) (${file})
|
工作区 vs 版本库
1
| git diff ${commit_id} (${file})
|
版本库 vs 版本库
1
| git diff ${commit_id} ${commit_id} (${file})
|
提交记录
1 2 3
| git log git log --oneline git log --graph
|
操作记录
暂存
查看
保存
恢复
1 2
| git stash pop git stash pop stash@{${id}}
|
删除
1 2
| git stash clear git stash drop stash@{${id}}
|
提交
暂存区
1 2 3
| git add -A or git add --all git add /path/to/file git add -u
|
版本库
1 2 3
| git commit -m "${commit_message}" git commit git commit --amend
|
回退
工作区
1 2
| git reset --hard (${commit_id}) (${file})
|
暂存区
1 2
| git reset (--mixed) (${commit_id}) (${file})
|
版本库
1 2 3 4 5 6 7
| git reset --soft (${commit_id}) (${file})
git revert (${commit_id}) git revert -n ${old_commit}^..${new_commit}
|
克隆
1 2 3 4 5
| git clone git@host:/path/to/registry.git
git clone -b ${branch_name} git@host:/path/to/registry.git
|
拉取
1 2 3 4 5 6
| git pull origin ${remote_branch} git checkout -b ${local_branch} ${remote_branch}
git fetch origin ${remote_branch} git pull origin ${remote_branch} git pull origin --rebase ${remote_branch}
|
推送
1 2
| git push origin ${remote_branch} git push origin ${local_branch}:${remote_branch}
|
合并
Merge
1 2 3 4
| git merge ${branch_name} git merge ${branch_name} --no-commit git merge --continue git merge --abort
|
Squash
1 2 3 4
| git merge --squash ${branch_name} git rebase -i ${startpoint} (${endpoint})
|
Rebase
1 2 3
| git rebase ${branch_name} git rebase --continue git rebase --abort
|
Cherry-Pick
1 2 3
| git cherry-pick ${commit_id} git cherry-pick --continue git cherry-pick --abort
|
冲突
1 2 3 4
| git status vim /path/to/file git add /path/to/file git commit -m "${commit_message}"
|
分支
查看
1 2 3
| git branch -a git branch -v git branch -r
|
更新
新建
1 2
| git checkout -b ${local_branch} ${remote_branch} git push origin ${local_branch}:${remote_branch}
|
改名
1 2 3 4 5 6
| git branch -m ${old_branch} ${new_branch}
git push origin --delete ${old_remote_branch} git push origin ${new_local_branch}:${new_remote_branch}
|
切换
1
| git checkout ${branch_name}
|
关联
1
| git branch --set-upstream-to=${remote_branch} ${local_branch}
|
删除
1 2 3
| git branch -d ${local_branch} git branch -D ${local_branch} git push origin --delete ${remote_branch}
|
标签
查看
1 2
| git tag git show ${tag_name}
|
新建
1 2 3
| git tag ${tag_name} (${commit_id}) git push origin --tags git push origin ${tag_name}
|
删除
1 2
| git tag -d ${tag_name} git push origin --delete ${tag_name}
|
工具
提交规范
1 2 3 4 5
| npm install -g commitizen cz-conventional-changelog
git cz
|
变更日志
1 2 3 4 5
| npm install -g conventional-changelog-cli
conventional-changelog -p angular -i CHANGELOG.md -s -r 0
|
代码评审
子级模块