Git基础命令
| 作用 |
命令 |
备注 |
| 创建Git仓库 |
$git init |
|
| 工作区(文件)-->暂存区 |
$git add [file] |
文件名 |
| 工作区(修改与新文件)-->暂存区 |
$git add . |
提交所有(不包括删除) |
| 工作区(修改与删除)-->暂存区 |
$git add -u |
提交所有(不包括新文件,--update的缩写) |
| 工作区(删除)-->暂存区 |
$git rm [file] |
文件名 |
| 工作区(全部)-->暂存区 |
$git add -A |
提交所有(--all缩写) |
| 暂存区-->版本库 |
$git commit -m [message] |
版本备注 |
| 查看当前仓库状态 |
$git status |
|
| 查看最近提交日志 |
$git long |
|
|
$git long --pretty=oneline |
添加后单行简略显示 |
|
$git long --abbrev-commit |
显示commit |
| 查看操作记录 |
$git reflog |
|
| 查看修改详情 |
$git diff [file] |
文件名 |
Git回退撤销
| 作用 |
命令 |
备注 |
| 撤销暂存区修改 |
$git reset HEAD [file] |
文件名 |
| 版本回退 |
$git reset --hard HEAD^n |
n=回退层数 |
|
$git reset --hard HEAD~1 |
n=回退层数 |
|
$git reset --hard [commit_id] |
回退版本号 |
| 工作区<--版本库(优先)/暂存区 |
$git checkout --[file] |
文件名 |
| 工作区<--版本库(批量) |
$ git reset --hard HEAD |
|
Git清除操作
| 作用 |
命令 |
备注 |
| 查看将会删除的文件 |
$git clean -n |
|
| 删除当前目录下所有没有track过的文件 |
$git clean -f |
忽略.gitignore文件指定的文件夹和文件 |
| 删除当前目录下所有没有track过的文件 |
git clean -xf |
不管他是否是.gitignore文件里面指定的文件夹和文件 |
| 删除指定路径下的没有被track过的文件 |
$git clean -f [path] |
|
| 删除当前目录下没有被track过的文件和文件夹 |
git clean -df |
|
Git云端仓库
| 作用 |
命令 |
备注 |
| 本地版本库关联网络版本库 |
$git remote add origin [adress] |
仓库地址 |
| 将网络库下载到本地 |
$git clone [adress] |
仓库地址 |
| 版本库-->网络库 |
$git push -u origin master |
|
| 版本库<--网络库 |
$git pull |
|
| 查看绑定的远程库 |
$ git remote -v |
|
| 删除已有的远程库 |
$ git remote rm origin |
|
Git分支管理
| 作用 |
命令 |
备注 |
| 创建并切换分支 |
$git checkout -b [name] |
分支名,该语句等效于创建+切换 |
| 查看分支 |
$git branch |
|
| 创建分支 |
$git branch [name] |
分支名 |
| 切换分支 |
$git checkout [name] |
分支名 |
| 合并某分支到当前分支 |
$git merge [name] |
分支名 |
| 删除分支 |
$git branch -d [name] |
分支名,可将-d=>-D以强制删除 |
| 查看分支合并图 |
$ git log --graph --pretty=oneline --abbrev-commit |
|
Git暂离管理
| 作用 |
命令 |
备注 |
| 保存并暂离当前工作区 |
$git stash |
|
| 查看暂离的工作区 |
$git stash list |
|
| 恢复暂离工作区 |
git stash apply [name] |
暂离区名,单个不填,多个需要 |
| 删除暂离工作区 |
git stash drop [name] |
暂离区名 |
| 恢复并删除暂离工作区 |
git stash pop [name] |
暂离区名 |
Git标签管理
| 作用 |
命令 |
备注 |
| 在当前分支打上标签 |
$git tag [tagname] |
标签名 |
| 查看所有标签 |
$git tag |
|
| 根据版本号打标签 |
$git tag [tagname] [commit_id] |
版本号 |
| 查看标签详情 |
$git show [tagname] |
|
| 创建带有说明的标签 |
$ git tag -a [tagname] -m [message] [commit_id] |
-a指定标签名,-m制定说明文字 |
| 删除标签 |
$ git tag -d [tagname] |
|
| 删除远程标签 |
$ git push origin :refs/tags/[tagname] |
删除远程标签需要先删除本地标签 |
| 推送标签到远程 |
$ git push origin [tagname] |
|
| 一次性全部推送 |
$ git push origin --tags |
|
Git指令别名
| 作用 |
命令 |
备注 |
| 创建别名 |
$ git config --global alias.st status |
接下来就可以使用st代替status了 |
相关文件操作
| 作用 |
命令 |
备注 |
| 查看文本文件内容 |
$cat [file] |
文件名 |
| 删除文件 |
$rm [file] |
文件名 |
| 修改文件 |
$vi [file] |
文件名 |
SSH秘钥
| 作用 |
命令 |
备注 |
| 创建ssh秘钥 |
ssh-keygen -t rsa -C "email@email.com" |
|
账户管理
| 作用 |
命令 |
备注 |
| 全局设置名字 |
$git config --global user.name "Name" |
|
| 全局设置邮箱 |
$git config --global user.email "Email" |
|
解决方案
拉取最新代码
$git clone --depth=1 git@git.dev.tencent.com:QDQD/AAKX.git
拉取某个分支最新代码
例:release/1.6.0分支
$git clone -b release/1.6.0 --depth=1 git://serverRepo myRepo
更好的日志展示
$git log --oneline --graph --all
--oneline 压缩模式,精简提交和哈希码
--graph 图形模式, 基于文本格式的历史信息
--all 所有分支历史