Git 是一个强大的分布式版本管理工具,它就像是你的时间机器,能够帮你找回过去的修改。回撤功能只能回退几步,但是版本管理工具可以记录每一次的修改,只要提交到版本仓库,你就可以轻松找回任何时刻的修改状态 。
下面是一份关于 Git 命令和技巧的速查表,帮助你快速掌握并运用 Git,你可以使用页面内查找功能(Ctrl/Command+f)来快速查找你需要的命令。在开始之前,请注意以下几点:
在工作环境中使用命令之前,请务必先在测试环境中测试命令的效果,以免造成不可挽回的后果。否则到时候别拿着砍刀找我哦!
所有的命令在 git version 2.7.4 (Apple Git-66) 下测试通过。
统一一些概念:
工作区:你的修改(增删文件和内容)发生在这里。
暂存区:使用命令 git add 文件名,将修改放入暂存区。
本地仓库:使用命令 git commit -m “修改描述”,将修改放入本地仓库。每一次 commit,就是一个版本。
远程仓库:使用命令 git push,将修改推送到远程仓库(如 GitHub)。
commit-id:使用命令 git log,你可以在最上面的 commit xxxxxx 行找到 commit-id。
现在,让我们开始探索你需要的 Git 命令速查表
展示帮助信息
The command output as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 <command>' or 'git help <concept>' to read about a specific subcommand or concept.
回到远程仓库的状态 抛弃本地所有的修改,回到远程仓库的状态。
重设第一个 commit 也就是把所有的改动都重新放回工作区,并清空所有的 commit ,这样就可以重新提交第一个 commit 了
查看冲突文件列表 展示工作区的冲突文件列表
展示工作区和暂存区的不同 输出工作区 和暂存区 的 different (不同)。
还可以展示本地仓库中任意两个 commit 之间的文件变动:
1 git diff <commit-id > <commit-id >
展示暂存区和最近版本的不同 输出暂存区 和本地最近的版本 (commit) 的 different (不同)。
展示暂存区、工作区和最近版本的不同 输出工作区 、暂存区 和本地最近的版本 (commit) 的 different (不同)。
快速切换到上一个分支
删除已经合并到 master 的分支 1 git branch --merged master | grep -v '^\*\| master ' | xargs -n 1 git branch -d
展示本地分支关联远程仓库的情况
关联远程分支 关联之后,git branch -vv 就可以展示关联的远程分支名了,同时推送到远程仓库直接:git push ,不需要指定远程仓库了。
1 git branch -u origin/mybranch
或者在 push 时加上 -u 参数
1 git push origin /mybranch -u
列出所有远程分支 -r 参数相当于:remote
列出本地和远程分支 -a 参数相当于:all
查看远程分支和本地分支的对应关系
远程删除了分支本地也想删除
创建并切换到本地分支 1 git checkout -b <branch-name>
从远程分支中创建并切换到本地分支 1 git checkout -b <branch-name > origin/<branch-name >
删除本地分支 1 git branch -d <local -branchname>
删除远程分支 1 git push origin --delete <remote-branchname>
或者
1 git push origin :<remote-branchname>
重命名本地分支 1 git branch -m <new -branch-name >
查看标签
展示当前分支的最近的 tag
查看标签详细信息
本地创建标签 1 git tag <version-number >
默认 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:
1 $ git tag -a <version -number > -m "v1.0 发布(描述)" <commit-id >
推送标签到远程仓库 首先要保证本地创建好了标签才可以推送标签到远程仓库:
1 git push origin <local -version -number >
一次性推送所有标签,同步到远程仓库:
删除本地标签
删除远程标签 1 git push origin --delete tag <tagname >
切回到某个标签 一般上线之前都会打 tag,就是为了防止上线后出现问题,方便快速回退到上一版本。下面的命令是回到某一标签下的状态:
1 git checkout -b branch_name tag_name
放弃工作区的修改 1 git checkout <file -name >
放弃所有修改:
恢复删除的文件 1 2 3 git rev-list -n 1 HEAD -- <file_path> git checkout <deleting_commit> ^ -- <file_path>
以新增一个 commit 的方式还原某一个 commit 的修改
回到某个 commit 的状态,并删除后面的 commit 和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit
1 2 3 4 5 6 7 git reset <commit-id> git reset --mixed HEAD^ git reset --soft HEAD~3 git reset --hard <commit-id>
修改上一个 commit 的描述 如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit
查看 commit 历史
查看某段代码是谁写的 blame 的意思为‘责怪’,你懂的。
显示本地更新过 HEAD 的 git 命令记录 每次更新了 HEAD 的 git 命令比如 commit、amend、cherry-pick、reset、revert 等都会被记录下来(不限分支),就像 shell 的 history 一样。 这样你可以 reset 到任何一次更新了 HEAD 的操作之后,而不仅仅是回到当前分支下的某个 commit 之后的状态。
修改作者名 1 git commit --amend --author='Author Name <email@address .com>'
修改远程仓库的 url 1 git remote set-url origin <URL >
增加远程仓库 1 git remote add origin <remote-url>
列出所有远程仓库
查看两个星期内的改动 1 git whatchanged --since ='2 weeks ago'
把 A 分支的某一个 commit,放到 B 分支上 这个过程需要 cherry-pick 命令,参考
1 git checkout <branch-name > && git cherry-pick <commit-id >
给 git 命令起别名 简化命令
1 2 3 4 5 git config --global alias .<handle> <command> 比如:git status 改成 git st,这样可以简化命令 git config --global alias .st status
存储当前的修改,但不用提交 commit 详解可以参考廖雪峰老师的 git 教程
保存当前状态,包括 untracked 的文件 untracked 文件:新建的文件
展示所有 stashes
回到某个 stash 的状态 1 git stash apply <stash@ {n}>
回到最后一个 stash 的状态,并删除这个 stash
删除所有的 stash
从 stash 中拿出某个文件的修改 1 git checkout <stash@{n}> -- <file-path>
展示所有 tracked 的文件
展示所有 untracked 的文件
展示所有忽略的文件 1 git ls-files --others -i --exclude-standard
强制删除 untracked 的文件 可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的 untracked 文件。clean 命令,注意两点 :
clean 后,删除的文件无法找回
不会影响 tracked 的文件的改动,只会删除 untracked 的文件1 git clean <file -name > -f
强制删除 untracked 的目录 可以用来删除新建的目录,注意 :这个命令也可以用来删除 untracked 的文件。详情见上一条
1 git clean <directory-name > -df
展示简化的 commit 历史
把某一个分支导出成一个文件 1 git bundle create <file > <branch-name >
从包中导入分支 新建一个分支,分支内容就是上面 git bundle create 命令导出的内容
1 git clone repo.bundle <repo-dir> -b <branch-name>
执行 rebase 之前自动 stash
从远程仓库根据 ID,拉下某一状态,到本地分支 1 git fetch origin pull/<id > /head:<branch-name >
详细展示一行中的修改
清除 gitignore 文件中记录的文件
展示所有 alias 和 configs 注意: config 分为:当前目录(local)和全局(golbal)的 config,默认为当前目录的 config
1 2 git config --local --list (当前目录) git config --global --list (全局)
展示忽略的文件
commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit 1 git log Branch1 ^Branch2
在 commit log 中显示 GPG 签名
删除全局设置 1 git config --global --unset <entry -name>
新建并切换到新分支上,同时这个分支没有任何 commit 相当于保存修改,但是重写 commit 历史
展示任意分支某一文件的内容 1 git show <branch-name>:<file-name>
clone 下来指定的单一分支 1 git clone -b <branch-name> --single-branch https://github.com/user/repo.git
clone 最新一次提交 只会 clone 最近一次提交,将减少 clone 时间
1 git clone --depth =1 https://github.com/user/repo.git
忽略某个文件的改动 关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
1 git update -index --assume-unchanged path /to /file
恢复 track 指定文件的改动
1 git update -index --no -assume-unchanged path/to /file
忽略文件的权限变化 不再将文件的权限变化视作改动
1 git config core.fileMode false
以最后提交的顺序列出所有 Git 分支 最新的放在最上面
1 git for -each -ref --sort =-committerdate --format ='%(refname:short)' refs/heads/
在 commit log 中查找相关内容 通过 grep 查找,given-text:所需要查找的字段
1 git log --all --grep ='<given-text>'
把暂存区的指定 file 放到工作区中 不添加参数,默认是 -mixed
强制推送 1 git push -f <remote-name > <branch-name >