Git 常用命令集合,按功能分类整理
git help -g
抛弃本地所有的修改,回到远程仓库的状态:
git fetch --all && git reset --hard origin/master
把所有的改动都重新放回工作区,并清空所有的 commit:
git update-ref -d HEAD
git diff
展示任意两个 commit 之间的文件变动:
git diff <commit-id> <commit-id>
git diff --cached
git diff HEAD
git checkout -
如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit:
git commit --amend
git commit --amend --author='Author Name <[email protected]>'
git config --global alias.<handle> <command>
# 示例:将 git status 简化为 git st
git config --global alias.st status
关闭 track 指定文件的改动:
git update-index --assume-unchanged path/to/file
恢复 track 指定文件的改动:
git update-index --no-assume-unchanged path/to/file
git config core.fileMode false
git branch -vv
关联之后可以直接使用 git push,不需要指定远程仓库:
git branch -u origin/mybranch
或者在 push 时加上 -u 参数:
git push origin/mybranch -u
git branch -r
git branch -a
git remote show origin
git checkout -b <branch-name>
git checkout -b <branch-name> origin/<branch-name>
git branch -d <local-branchname>
git push origin --delete <remote-branchname>
或者:
git push origin :<remote-branchname>
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
git branch -m <new-branch-name>
git remote prune origin
最新的放在最上面:
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
相当于保存修改,但是重写 commit 历史:
git checkout --orphan <branch-name>
git clone -b <branch-name> --single-branch https://github.com/user/repo.git
减少 clone 时间:
git clone --depth=1 https://github.com/user/repo.git
git checkout <branch-name> && git cherry-pick <commit-id>
git tag
展示当前分支的最近的 tag:
git describe --tags --abbrev=0
git tag -ln
默认 tag 是打在最近的一次 commit 上:
git tag <version-number>
指定 commit 打 tag:
git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id>
推送单个标签:
git push origin <local-version-number>
一次性推送所有标签:
git push origin --tags
git tag -d <tag-name>
需要先删除本地标签,再执行:
git push origin :refs/tags/<tag-name>
git checkout -b branch_name tag_name
放弃某个文件的修改:
git checkout <file-name>
放弃所有修改:
git checkout .
# 得到 deleting_commit
git rev-list -n 1 HEAD -- <file_path>
# 回到删除文件 deleting_commit 之前的状态
git checkout <deleting_commit>^ -- <file_path>
git revert <commit-id>
和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit
# 默认就是 -mixed 参数
git reset <commit-id>
# 回退至上个版本,重置 HEAD 到另外一个 commit,重置暂存区,工作区不会被更改
git reset –mixed HEAD^
# 回退至三个版本之前,只回退 commit 信息,暂存区和工作区保持一致
git reset –soft HEAD~3
# 彻底回退到指定 commit-id 的状态,暂存区和工作区也会变为指定版本的内容
git reset –hard <commit-id>
git reset <file-name>
git push -f <remote-name> <branch-name>
git stash
git stash -u
git stash list
git stash apply <stash@{n}>
git stash pop
git stash clear
git checkout <stash@{n}> -- <file-path>
git remote
git remote add origin <remote-url>
git remote set-url origin <URL>
git fetch origin pull/<id>/head:<branch-name>
git log
git log --pretty=oneline --graph --decorate --all
git log --all --grep='<given-text>'
git log --show-signature
git log Branch1 ^Branch2
git blame <file-name>
每次更新了 HEAD 的 git 命令都会被记录下来(不限分支),可以 reset 到任何一次更新了 HEAD 的操作之后:
git reflog
git whatchanged --since='2 weeks ago'
git diff --word-diff
git show <branch-name>:<file-name>
可以用来删除新建的文件。注意:删除的文件无法找回,不会影响 tracked 文件的改动:
git clean <file-name> -f
git clean <directory-name> -df
.gitignore 文件中记录的文件git clean -X -f
git ls-files -t
git ls-files --others
git ls-files --others -i --exclude-standard
git status --ignored
# 当前目录
git config --local --list
# 全局
git config --global --list
git config --global --unset <entry-name>
git bundle create <file> <branch-name>
git clone repo.bundle <repo-dir> -b <branch-name>
git rebase --autostash
git config --global https.proxy 'http://127.0.0.1:8001'
git config --global http.proxy 'http://127.0.0.1:8001'
git config --global socks.proxy "127.0.0.1:1080"
# 编辑 ~/.ssh/config
Host gitlab.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
| 类型 | 说明 |
|---|---|
feat | 新特性 |
fix | 修改问题 |
refactor | 代码重构 |
docs | 文档修改 |
style | 代码格式修改(非 CSS) |
test | 测试用例修改 |
chore | 其他修改(构建流程、依赖管理等) |
| 字段 | 说明 |
|---|---|
scope | commit 影响的范围,如:route, component, utils, build |
subject | commit 的概述 |
body | commit 具体修改内容,可以分为多行 |
footer | 一些备注,通常是 BREAKING CHANGE 或修复的 bug 的链接 |
全局安装:
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
安装后使用 git cz 代替 git commit。
| 概念 | 说明 |
|---|---|
| 工作区 | 改动(增删文件和内容) |
| 暂存区 | git add 后的改动存放位置 |
| 本地仓库 | git commit 后的版本存放位置 |
| 远程仓库 | git push 后的存放位置(GitHub 等) |
| commit-id | git log 最上面那行 commit xxxxxx 后的字符串 |
git clean 删除的文件无法找回git reset --hard 会丢失 commit 历史,请谨慎使用