git 常用命令
git操作示意图

git创建项目仓库
command |
comment |
| git init |
初始化本地git仓库(创建新仓库) |
| git remote add origin url |
关联远程仓库 |
| git pull |
 |
| git fetch |
获取远程仓库中所有的分支到本地 |
回退操作
command |
comment |
| git reset --hard HEAD |
将当前版本重置为HEAD(通常用于merge失败回退) |
| git reset --hard HEAD^ |
回退到上一个版本 |
| git reset --hard (commit_id) |
回退到某个版本 |
| git reset HEAD file |
撤回暂存区的文件修改到工作区 |
| git checkout HEAD -- filename |
撤销修改的文件(==TortoiseGit Revert) |
| git checkout -- file |
撤销修改的文件 |
| git checkout (commit_id) |
取某个版本到本地 |
commit & push
command |
comment |
| git status |
查看当前版本状态(是否修改) |
| git add xyz |
添加xyz文件至[stash/index/cache] |
| git add . |
增加当前子目录下所有更改过的文件至[stash/index/cache] |
| git commit -m 'xxx' |
提交 |
| git commit --amend -m 'xxx' |
合并上一次提交(用于反复修改) |
| git commit -am 'xxx' |
将add和commit合为一步 |
| git rm xxx |
删除[stash/index/cache]中的文件 |
| git rm -r * |
递归删除 |
| git rm -r --cached 文件/文件夹名字 |
取消文件被版本控制 |
| git push origin master |
推送本地分支到远程master分支 |
| git push origin :hotfixes/BJVEP933 |
删除远程仓库的hotfixes/BJVEP933分支 |
取出两次提交之间的差异文件列表
command |
comment |
| git diff --name-only SHA1 SHA2 |
取出SHA1和SHA2的差异文件列表 |
| git diff --name-only HEAD~10 HEAD~5 |
取出最近10个和最近5个提交之间的差异文件列表 |
常规操作
command |
comment |
| git config --global core.autocrlf false |
取消换行符自动转换(windows 推荐) |
| git remote add origin git@sample.com/xxxx.git |
增加远程定义(用于push/pull/fetch) |
| git reflog |
获取执行过的命令 |
| git log --graph |
查看分支合并图 |
| git merge --no-ff -m '合并描述' 分支名 |
不使用Fast forward方式合并,采用这种方式合并可以看到合并记录 |
| git check-ignore -v 文件名 |
查看忽略规则 |
| git add -f 文件名 |
强制将文件提交 |
| git reflog |
打印所有的日志,假如:ABC三个节点,回退到B后,仍旧打印所有日志 |
| git cherry-pick ff44785404a8e |
合并提交ff44785404a8e的修改 |
| git fetch |
获取所有远程分支(不更新本地分支,另需merge) |
| git fetch --prune |
获取所有原创分支并清除服务器上已删掉的分支 |
| git pull origin master |
获取远程分支master并merge到当前分支 |
| git mv README README2 |
重命名文件README为README2 |
| git rebase master |
把master分支上的变更,加到当前分支 |
忽略已加入到版本库中的文件
command |
comment |
| git update-index --assume-unchanged file |
忽略单个文件 |
| git rm -r --cached 文件/文件夹名字 |
(. 忽略全部文件) |
| git update-index --no-assume-unchanged file |
取消忽略文件 |
git branch
command |
comment |
| git branch |
查看分支列表 |
| git branch --contains 50089 |
显示包含提交50089的分支 |
| git branch -a |
显示所有分支 |
| git branch -r |
显示所有远程跟踪分支 |
| git branch -v |
查看所有分支的最后一次操作 |
| git branch -vv |
查看当前分支 |
| git branch --merged |
显示所有已合并到当前分支的分支 |
| git branch --no-merged |
显示所有未合并到当前分支的分支 |
| git branch -m master master_copy |
本地分支改名 |
| git checkout -b master_copy |
从当前分支创建新分支master_copy并检出 |
| git checkout -b master master_copy |
上面的完整版 |
| git checkout features/performance |
检出已存在的features/performance分支 |
| git checkout --track issue3 |
检出远程分支issue3并创建本地跟踪分支 |
| git checkout v2.0 |
检出版本v2.0 |
| git checkout -b devel origin/develop |
从远程分支develop创建新本地分支devel并检出 |
| git checkout -- README |
检出head版本的README文件(可用于修改错误回退) |
| git branch -d 分支名 |
删除本地分支(本分支修改已合并到其他分支) |
| git branch -D 分支名 |
强行删除分支 |
| git branch origin :分支名 |
删除远处仓库分支 |
| git merge origin/master |
合并远程master分支至当前分支 |
git tag
command |
comment |
| git tag |
列出所有标签列表 |
| git tag 标签名 |
添加标签(默认对当前版本) |
| git tag 标签名 commit_id |
对某一提交记录打标签 |
| git tag -a 标签名 -m '描述' |
创建新标签并增加备注 |
| git show 标签名 |
查看标签信息 |
| git tag -d 标签名 |
删除本地标签 |
| git push origin 标签名 |
推送标签到远程仓库 |
| git push origin --tags |
推送所有标签到远程仓库 |
| git push --tags |
把所有tag推送到远程仓库 |
| git push origin :refs/tags/标签名 |
从远程仓库中删除标签 |
git log
command |
comment |
| git log |
显示提交日志 |
| git log -1 |
显示1行日志 -n为n行 |
| git log -5 |
|
| git log --stat |
显示提交日志及相关变动文件 |
| git log -p -m |
|
| git log --graph |
查看分支合并图 |
| git log --pretty=format:'%h %s' --graph |
图示提交日志 |
| git show dfb02e6e4f2f7b573337763e5c0013802e392818 |
显示某个提交的详细内容 |
| git show dfb02e |
可只用commitid的前几位 |
| git show HEAD |
显示HEAD提交日志 |
| git show HEAD^ |
显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本 |
| git show HEAD~3 |
|
| git show -s --pretty=raw 2be7fcb476 |
|
| git show 标签名 |
显示(标签名)的日志及详细内容 |
| git log 标签名 |
显示(标签名)的日志 |
git diff
command |
comment |
| git diff |
显示所有未添加至[stash/index/cache]的变更 |
| git diff --cached |
显示所有已添加[stash/index/cache]但还未commit的变更 |
| git diff HEAD^ |
比较与上一个版本的差异 |
| git diff HEAD -- ./lib |
比较与HEAD版本lib目录的差异 |
| git diff origin/master..master |
比较远程分支master上有本地分支master上没有的 |
| git diff origin/master..master --stat |
只显示差异的文件,不显示具体内容 |
暂存操作
command |
comment |
| git stash |
暂存当前修改 |
| git stash apply |
恢复最近的一次暂存 |
| git stash apply stash@{0} |
应用第一次暂存 |
| git stash show -p stash@{0} |
参考第一次暂存 |
| git stash pop |
恢复暂存并删除暂存记录 |
| git stash list |
查看暂存列表 |
| git stash drop 暂存名(例:stash@{0}) |
移除某次暂存 |
| git stash clear |
清除暂存 |
其他操作
command |
comment |
| git ls-files |
列出git [stash/index/cache]包含的文件 |
| git show-branch |
图示当前分支历史 |
| git show-branch --all |
图示所有分支历史 |
| git whatchanged |
显示提交历史对应的文件修改 |
| git revert dfb02e6e4f2f7b573337763e5c0013802e392818 |
撤销提交dfb02e6e4f2f7b573337763e5c0013802e392818 |
| git ls-tree HEAD |
内部命令:显示某个git对象 |
| git rev-parse v2.0 |
内部命令:显示某个ref对于的SHA1 HASH |
| git grep "delete from" |
文件中搜索文本“delete from” |
| git grep -e '#define' --and -e SORT_DIRENT |
|
| git gc |
|
| git fsck |
|
git config
command |
comment |
| git config --global user.name "xxx" |
配置用户名 |
| git config --global user.email "xxx@xxx.com" |
配置邮件 |
| git config --global color.ui true |
git status等命令自动着色 |
| git config --global color.status auto |
|
| git config --global color.diff auto |
|
| git config --global color.branch auto |
|
| git config --global color.interactive auto |
|
| git config --global https.proxy http://127.0.0.1:1080 |
设置git的代理配置 |
| git config --global https.proxy https://127.0.0.1:1080 |
设置git的代理配置 |
| git config --global http.proxy socks5://127.0.0.1:1080 |
设置git的代理配置 |
| git config --global https.proxy socks5://127.0.0.1:1080 |
设置git的代理配置 |
| git config --global --unset http.proxy |
删除git的代理配置 |
| git config --global --unset https.proxy |
删除git的代理配置 |
| git config --global http.https://github.com.proxy socks5://127.0.0.1:1080 |
只对github.com设置代理 |
| git config --global --unset http.https://github.com.proxy |
取消github.com的代理 |
ssh访问代理设置
ssh proxy
需要修改~/.ssh/config文件, 没有的话新建一个. 同样仅为github.com设置代理:
| Host github.com
User git
ProxyCommand nc -v -x 127.0.0.1:1086 %h %p
|
如果是在Windows下, 则需要个性%home%.ssh\config, 其中内容类似于:
| Host github.com
User git
ProxyCommand connect -S 127.0.0.1:1086 %h %p
|
服务器转移
git从一个服务器移植到另一个服务器的步骤:
| git clone --mirror <URL to my OLD repo location>
cd <New directory where your OLD repo was cloned>
git remote set-url origin <URL to my NEW repo location>
git push -f origin
|