Git常用命令清单
2017-01-08 23:58
639 查看
配置
显示配置信息 git config --list 配置用户名称与邮件地址 git config [--global] user.name "YourName" git config [--global] user.email "YourEmail" 配置文本编辑器 git config [--global] core.editor vim 配置别名 git config [--global] alias.co checkout git config [--global] alias.br branch git config [--global] alias.ci commit git config [--global] alias.st status git config [--global] alias.last 'log -1 HEAD' git config [--global] alias.unstage 'reset HEAD --'
帮助
git help <verb>
新建代码库
在当前目录中初始化一个Git仓库 git init 新建一个目录,将其初始化为Git代码库 git init <project-name> 克隆现有的仓库 git clone <url> 克隆现有仓库的某个分支 git clone -b <branchname> <url> 克隆现有的仓库,并且自定义本地仓库的名字 git clone <url> <project-name> 克隆现有的仓库,并且指定一个远程主机的简写 git clone -o <shortname> <url> 把现有仓库导出为裸仓库,即一个不包含当前工作目录的仓库 git clone --bare <project-path> <xxx.git>
增加/删除文件
添加指定文件到暂存区 git add <file> 添加指定目录到暂存区,包括子目录 git add <dir> 添加当前目录的所有文件到暂存区 git add . 删除工作区文件,并且将这个删除放入暂存区 git rm <file> 在暂存区停止追踪指定文件,但该文件会保留在工作区 git rm --cached <file> 重命名工作区文件,并且将这个重命名放入暂存区 git mv <old-file> <new-file>
撤销
取消暂存的文件 git reset HEAD <file> 取消工作区中对文件的修改,将其还原成上次提交的样子 git checkout -- <file> 取消工作区所有的修改,将其还原成上次提交的样子 git checkout -- . 储藏工作区 git stash 还原储藏的工作区 git stash pop
代码提交
提交暂存区到仓库区 git commit -m "备注" 提交暂存区的指定文件到仓库区 git commit <file> -m "备注" 提交工作区,跳过暂存区直接到仓库区 git commit -a -m "备注" 重做上一次commit,如果代码没有任何新变化,则用来改写上一次commit的提交信息 git commit --amend -m "备注"
查看信息
检查当前文件状态 git status 检查当前文件状态,紧凑输出 git status -s 只列出所有已被跟踪、已修改但没提交的文件 git status -uno 检查某个目录中的文件状态 git status <目录> 查看工作区和暂存区的差异 git diff 查看暂存区和仓库中上一次commit的差异 git diff --cached 查看各个分支当前所指的对象 git log --oneline --decorate 查看分叉历史 git log --oneline --decorate --graph --all 显示指定文件是什么人在什么时间修改过 git blame <file>
分支
列出所有本地分支 git branch 列出所有远程分支 git branch -r 列出所有本地分支和远程分支 git branch -a 列出所有的本地分支,并显示每一个分支的最后一次提交信息 git branch -v 查看设置的所有跟踪分支 git branch -vv 切换到指定的分支,并更新工作区 git checkout <branch-name> 切换到上一个分支,并更新工作区 git checkout - 新建一个分支,但依然停留在当前分支 git branch <branch-name> 新建一个分支,并切换到新分支上 git checkout -b <branch-name> 新建一个分支,指向指定commit git branch <branch-name> <commit> 新建一个跟踪分支 git branch --track <new-branch-name> <remote-name>/<branch-name> 新建一个跟踪分支,并同时切换到新分支上 git checkout -b <new-branch-name> <remote-name>/<branch-name> 新建一个同名跟踪分支,并同时切换到新分支上 git checkout --track <remote-name>/<branch-name> 设置本地指定分支跟踪一个远程分支 git branch -u <remote-name>/<branch-name> <local-branch-name> 设置本地当前分支跟踪一个远程分支 git branch -u <remote-name>/<branch-name> 合并指定分支到当前分支 git merge <branch-name> 合并指定远程分支到当前分支 git merge <remote-name>/<branch-name> 不使用fast-forward(快进)方式合并,保留分支的commit历史 git merge --no-ff <branch-name> 使用squash方式合并,把多次分支commit历史压缩为一次 git merge --squash <branch-name> 选择一个commit,合并进当前分支 git cherry-pick <commit> 删除指定分支 git branch -d <branch-name>
远程同步
列出所有远程仓库的简写 git remote 列出所有远程仓库的简写与其对应的 URL git remote -v 查看远程仓库更多信息 git remote show <remote-name> 添加一个新的远程 Git 仓库,同时指定一个简写 git remote add <shortname> <url> 修改一个远程仓库的简写名 git remote rename <old-shortname> <new-shortname> 移除一个远程仓库 git remote rm <remote-name> 抓取所有的远程仓库 git fetch --all 从远程仓库中抓取所有的数据到本地数据库 git fetch <remote-name> 从远程仓库中抓取特定分支的更新到本地数据库 git fetch <remote-name> <remote-branch-name> 从远程仓库抓取某个分支的更新,再与本地的指定分支合并 git pull <remote-name> <remote-branch-name>:<local-branch-name> 从远程仓库抓取某个分支的更新,然后合并到当前分支 git pull <remote-name> <remote-branch-name> 如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名 git pull <remote-name> 推送本地指定分支到远程仓库指定分支 git push <remote-name> <local-branch-name>:<remote-branch-name> 如果省略远程分支名,推送本地指定分支到远程仓库的同名分支,如果该远程分支不存在,则会被新建 git push <remote-name> <local-branch-name> 如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支 git push <remote-name> :<remote-branch-name> 如果当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略 git push <remote-name> 推送本地指定分支到远程仓库,并在本地分支和远程分支间建立跟踪 git push -u <remote-name> <local-branch-name> 推送所有分支到远程仓库 git push <remote-name> --all 删除指定远程分支 git push <remote-name> --delete <remote-branch-name>
标签
列出所有的标签 git tag 查看标签信息 git show <tagname> 新建一个附注标签,指向当前commit git tag -a <tagname> -m "备注" 新建一个轻量标签,指向当前commit git tag <tagname> 新建一个标签,指向某次commit git tag <tagname> <commit> git tag -a <tagname> <commit> -m "备注" 删除本地标签 git tag -d <tagname> 删除远程标签 git push <remote-name> --delete tag <tagname> 推送指定标签到远程服务器 git push <remote-name> <tagname> 推送所有标签到远程服务器 git push <remote-name> --tags 新建一个分支,指向某个标签 git checkout -b <branch-name> <tagname>
补充:
参考书籍和博客
《progit-zh-v2.1.1.pdf》https://www.gitbook.com/book/bingohuang/progit2/details
阮一峰:常用 Git 命令清单 http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
阮一峰:Git远程操作详解 http://www.ruanyifeng.com/blog/2014/06/git_remote.html
阮一峰:Git 使用规范流程 http://www.ruanyifeng.com/blog/2015/08/git-use-process.html
阮一峰:Git分支管理策略 http://www.ruanyifeng.com/blog/2012/07/git.html
git merge –no-ff图解 https://segmentfault.com/q/1010000002477106