Git的奇技淫巧:see_no_evil:
Git的奇技淫巧
By 521xueweihan
Git的奇技淫巧
By 521xueweihan
Git 的奇技淫巧:see_no_evil:
Git 常用命令集合,Fork 于tips项目
署名-非商业性使用-禁止演绎 4.0 国际
Git 是一个 “分布式版本管理工具”,简单的理解版本管理工具:大家在写东西的时候都用过 “回撤” 这个功能,但是回撤只能回撤几步,假如想要找回我三天之前的修改,光用 “回撤” 是找不回来的。而 “版本管理工具” 能记录每次的修改,只要提交到版本仓库,你就可以找到之前任何时刻的状态(文本状态)。
下面的内容就是列举了常用的 Git 命令和一些小技巧,可以通过 "页面内查找" 的方式进行快速查询:Ctrl/Command+f
。
如果之前未使用过 Git,可以学习 Git 小白教程入门
git version 2.7.4 (Apple Git-66)
下测试通过git add 改动的文件名
,此次改动就放到了 ‘暂存区’git commit 此次修改的描述
,此次改动就放到了 ’本地仓库’,每个 commit,我叫它为一个 ‘版本’。git push 远程仓库
,此次改动就放到了 ‘远程仓库’(GitHub 等)git log
,最上面那行 commit xxxxxx
,后面的字符串就是 commit-id.gitignore
文件中记录的文件sh
git help -g
The command output as below:
```
The common Git guides are:
attributes Defining attributes per path
cli Git command-line interface and conventions
core-tutorial A Git core tutorial for developers
cvs-migration Git for CVS users
diffcore Tweaking diff output
everyday A useful minimum set of commands for Everyday Git
glossary A Git Glossary
hooks Hooks used by Git
ignore Specifies intentionally untracked files to ignore
modules Defining submodule properties
namespaces Git namespaces
repository-layout Git Repository Layout
revisions Specifying revisions and ranges for Git
tutorial A tutorial introduction to Git
tutorial-2 A tutorial introduction to Git: part two
workflows An overview of recommended workflows with Git
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help ' or 'git help ' to read about a specific subcommand or concept.
```
抛弃本地所有的修改,回到远程仓库的状态。sh
git fetch --all && git reset --hard origin/master
也就是把所有的改动都重新放回工作区,并清空所有的 commit,这样就可以重新提交第一个 commit 了
sh
git update-ref -d HEAD
展示工作区的冲突文件列表sh
git diff --name-only --diff-filter=U
输出工作区和暂存区的 different (不同)。
sh
git diff
还可以展示本地仓库中任意两个 commit 之间的文件变动:sh
git diff <commit-id> <commit-id>
输出暂存区和本地最近的版本 (commit) 的 different (不同)。sh
git diff --cached
输出工作区、暂存区 和本地最近的版本 (commit) 的 different (不同)。
sh
git diff HEAD
sh
git checkout -
sh
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
sh
git branch -vv
关联之后,git branch -vv
就可以展示关联的远程分支名了,同时推送到远程仓库直接:git push
,不需要指定远程仓库了。sh
git branch -u origin/mybranch
或者在 push 时加上 -u
参数sh
git push origin/mybranch -u
-r 参数相当于:remotesh
git branch -r
-a 参数相当于:allsh
git branch -a
sh
git remote show origin
sh
git remote prune origin
sh
git checkout -b <branch-name>
sh
git checkout -b <branch-name> origin/<branch-name>
sh
git branch -d <local-branchname>
sh
git push origin --delete <remote-branchname>
或者
sh
git push origin :<remote-branchname>
sh
git branch -m <new-branch-name>
sh
git tag
展示当前分支的最近的 tag
sh
git describe --tags --abbrev=0
sh
git tag -ln
sh
git tag <version-number>
默认 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:sh
$ git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id>
首先要保证本地创建好了标签才可以推送标签到远程仓库:
sh
git push origin <local-version-number>
一次性推送所有标签,同步到远程仓库:
sh
git push origin --tags
sh
git tag -d <tag-name>
sh
git push origin --delete tag <tagname>
一般上线之前都会打 tag,就是为了防止上线后出现问题,方便快速回退到上一版本。下面的命令是回到某一标签下的状态:sh
git checkout -b branch_name tag_name
sh
git checkout <file-name>
放弃所有修改:sh
git checkout .
```sh
git rev-list -n 1 HEAD -- #得到 deleting_commit
git checkout ^ -- #回到删除文件 deleting_commit 之前的状态
```
sh
git revert <commit-id>
和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit
```sh
git reset #默认就是-mixed参数。
git reset --mixed HEAD^ #回退至上个版本,它将重置HEAD到另外一个commit,并且重置暂存区以便和HEAD相匹配,但是也到此为止。工作区不会被更改。
git reset --soft HEAD~3 #回退至三个版本之前,只回退了commit的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接commit即可
git reset --hard #彻底回退到指定commit-id的状态,暂存区和工作区也会变为指定commit-id版本的内容
```
如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit
sh
git commit --amend
sh
git log
blame 的意思为‘责怪’,你懂的。
sh
git blame <file-name>
每次更新了 HEAD 的 git 命令比如 commit、amend、cherry-pick、reset、revert 等都会被记录下来(不限分支),就像 shell 的 history 一样。
这样你可以 reset 到任何一次更新了 HEAD 的操作之后,而不仅仅是回到当前分支下的某个 commit 之后的状态。
sh
git reflog
sh
git commit --amend --author='Author Name <[email protected]>'
sh
git remote set-url origin <URL>
sh
git remote add origin <remote-url>
sh
git remote
sh
git whatchanged --since='2 weeks ago'
这个过程需要 cherry-pick
命令,参考
sh
git checkout <branch-name> && git cherry-pick <commit-id>
简化命令
```sh
git config --global alias.
比如:git status 改成 git st,这样可以简化命令
git config --global alias.st status
```
详解可以参考廖雪峰老师的 git 教程sh
git stash
untracked 文件:新建的文件sh
git stash -u
sh
git stash list
sh
git stash apply <stash@{n}>
sh
git stash pop
sh
git stash clear
sh
git checkout <stash@{n}> -- <file-path>
sh
git ls-files -t
sh
git ls-files --others
sh
git ls-files --others -i --exclude-standard
可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的 untracked 文件。clean
命令,注意两点:
1. clean 后,删除的文件无法找回
2. 不会影响 tracked 的文件的改动,只会删除 untracked 的文件
sh
git clean <file-name> -f
可以用来删除新建的目录,注意:这个命令也可以用来删除 untracked 的文件。详情见上一条
sh
git clean <directory-name> -df
sh
git log --pretty=oneline --graph --decorate --all
sh
git bundle create <file> <branch-name>
新建一个分支,分支内容就是上面 git bundle create
命令导出的内容
sh
git clone repo.bundle <repo-dir> -b <branch-name>
sh
git rebase --autostash
sh
git fetch origin pull/<id>/head:<branch-name>
sh
git diff --word-diff
sh
git clean -X -f
注意: config 分为:当前目录(local)和全局(golbal)的 config,默认为当前目录的 config
sh
git config --local --list (当前目录)
git config --global --list (全局)
sh
git status --ignored
sh
git log Branch1 ^Branch2
sh
git log --show-signature
sh
git config --global --unset <entry-name>
相当于保存修改,但是重写 commit 历史
sh
git checkout --orphan <branch-name>
sh
git show <branch-name>:<file-name>
sh
git clone -b <branch-name> --single-branch https://github.com/user/repo.git
只会 clone 最近一次提交,将减少 clone 时间
sh
git clone --depth=1 https://github.com/user/repo.git
关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
sh
git update-index --assume-unchanged path/to/file
恢复 track 指定文件的改动
sh
git update-index --no-assume-unchanged path/to/file
不再将文件的权限变化视作改动
sh
git config core.fileMode false
最新的放在最上面
sh
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
通过 grep 查找,given-text:所需要查找的字段
sh
git log --all --grep='<given-text>'
不添加参数,默认是 -mixed
sh
git reset <file-name>
sh
git push -f <remote-name> <branch-name>
sh
git config --global https.proxy 'http://127.0.0.1:8001' # 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口
git config --global http.proxy 'http://127.0.0.1:8001'
git config --global socks.proxy "127.0.0.1:1080"
```sh
$ cat ~/.ssh/config
Host gitlab.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p # 直接使用 shadowsocks 提供的 socks5 代理端口
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
```
使用Angular团队提交规范
主要有以下组成
常用的修改项
可以使用cz-cli工具代替 git commit
全局安装
```shell
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc``
git cz
全局安装后使用代替
git commit`就可以了,如下图
本作品采用 署名-非商业性使用-禁止演绎 4.0 国际 进行许可。
返回顶部