推荐读物:pro.git
接下来是正文,转载自度娘
Git 常用命令速查表
*************************************************
和原项目同步 1.添加原项目的地址
git remote add upstream https://github.com/StudyByMyself/study.git
git pull upstream master 合并到主分支
**************************************************
创建版本库
$ git clone <url> #克隆远程版本库
$ git init #初始化本地版本库
修改和提交
$ git status #查看状态
$ git diff #查看变更内容
$ git add . #跟踪所有改动过的文件
$ git add <file> #跟踪指定的文件
$ git mv <old> <new> #文件改名
$ git rm <file> #删除文件
$ git rm --cached <file> #停止跟踪文件但不删除
$ git commit -m “commit message” #提交所有更新过的文件
$ git commit --amend #修改最后一次提交
查看提交历史,共用一次commit
$ git log #查看提交历史
$ git log -p <file> #查看指定文件的提交历史
$ git blame <file> #以列表方式查看指定文件的提交历史
撤消
$ git reset --hard HEAD #撤消工作目录中所有未提交文件的修改内容
$ git checkout HEAD <file> #撤消指定的未提交文件的修改内容
$ git revert <commit> #撤消指定的提交
分支与标签
$ git branch #显示所有本地分支
$ git checkout <branch/tag> #切换到指定分支或标签
$ git branch <new-branch> #创建新分支
$ git branch -d <branch> #删除本地分支
$ git tag #列出所有本地标签
$ git tag <tagname> #基于最新提交创建标签
$ git tag -d <tagname> #删除标签
合并与衍合
$ git merge <branch> #合并指定分支到当前分支
$ git rebase <branch> #衍合指定分支到当前分支
远程操作
$ git remote -v #查看远程版本库信息
$ git remote show <remote> #查看指定远程版本库信息
$ git remote add <remote> <url> #添加远程版本库
$ git fetch <remote> #从远程库获取代码
$ git pull <remote> <branch> #下载代码及快速合并
$ git push <remote> <branch> #上传代码及快速合并
$ git push <remote> :<branch/tag-name> #删除远程分支或标签
$ git push --tags #上传所有标签
Git命令小记
由于自己平常git用的不多不熟练,最近写个小东西并把代码托管到github,才发现之前看的《Pro Git》和《看日记学git》完全打水漂。重翻《Pro Git》,把一些重要的常见的命令记下来,备忘,具体的请man。
符号约定:
[]:可选 <>:必选
Git 配置
git config [--global] user.name <name> 设置用户名
git config [--global] user.email <email> 设置邮箱
git config [--global] core.editor <editor> 设置编辑器
git config [--global] github.user <user> 设置github帐号名
git config [--global] github.token <token> 设置github的token
--global是对当前系统用户的全局设置,在~/.gitconfig中。对系统所有用户进行配置,/etc/gitconfig。对当前项目,.git/config
Git 创建库
git clone <url> ssh/http(s)/git三种协议,ssh和https可推送
git init 初始化Git仓库
Git 日常操作
git add <file> 将文件加入index file
git rm [--cached] 删除,加--cached表示仅从index file中删除文件,即放弃跟踪
git mv <src> <dest> 移动/更名
git diff --cached/--staged 当前索引与上次提交(有哪些需要commit)
git diff 当前索引与工作目录(有哪些需要add)
git diff HEAD[^] 工作目录与上次提交(当前目录与上次提交有何改变)
git commit [-a] -m <msg> 提交
git commit --amend [-m <msg>] 修复上次提交
git reset HEAD <file> 同--mixed,default option
git reset --mixed HEAD 撤销 commit 和index file,只保留 working tree 的信息
git reset --hard HEAD[^] 将 working tree 和 index file 都撤销到以前状态
git reset --soft HEAD[^] 只撤销 commit,而保留 working tree 和 index file 的信息
回复到某个状态。以git reset --soft HEAD为例,commit回退到
HEAD(相当于无变化),若是HEAD^,则commit回退到HEAD^
git gc 用垃圾回收机制清除由于 reset 而造成的垃圾代码
git status 显示当前工作目录状态
git log [-p] 显示提交历史(many useful options to be learned)
git branch [branch] 显示/新建分支
git branch -d/-D 删除分支(d表示“在分支合并后删除分支”,D表示无论如何都删除分支)
git show-branch
git checkout <branch> 切换分支(分支未commit无法切换)
git merge <branch> 合并分支
git merge == git pull .
git show <branch | commit | tag | etc> 显示对应对象的信息
git grep <rep> [object] (在指定对象(历史记录)中)搜索
git cat-file 查看数据
git cat-file <-t | -s | -e | -p | (type)> <object> type can be one of: blob, tree, commit, tag
git ls-files [--stage] show information about files in the index and the working tree(实际是查看索引文件)
git watchchanged <since>..<until> 显示两个commit(当然也可以是branch)的区别
git remote [-v] 显示远程仓库,加-v选项可显示仓库地址
git remote add <repo_name> <url> 添加远程仓库,repo_name为shortname,指代仓库地址
git remote rename <old_name> <new_name> 更名
git remote rm <repo_name> 删除远程仓库
git remote show <repo_name> 查看远程仓库信息
git remote fetch <repo_name> 从远程仓库抓取数据(并不合并)
git pull <repo_name> <branch_name> 拉去数据并合并到当前分支
git push <repo_name> <branch_name> 推送指定分支到指定仓库
git fetch <repo_name> <branch_name>[:<local_branch_name>] 拉去数据,未合并
Git 标签
git 标签相关……
Git 相关环境变量
GIT_DIR: 如果指定了那么git init将会在GIT_DIR指定的目录下创建版本库
GIT_OBJECT_DIRECTORY: 用来指示对象存储目录的路径。即原来$GIT_DIR/objects下的文件会置于该变量指定的路径下
Git 常见变量
HEAD: 表示最近一次的 commit。
MERGE_HEAD: 如果是 merge 产生的 commit,那么它表示除 HEAD 之外的另一个父母分支。
FETCH_HEAD: 使用 git-fetch 获得的 object 和 ref 的信息都存储在这里,这些信息是为日后 git-merge 准备的。
HEAD^: 表示 HEAD 父母的信息
HEAD^^: 表示 HEAD 父母的父母的信息
HEAD~4: 表示 HEAD 上溯四代的信息
HEAD^1: 表示 HEAD 的第一个父母的信息
HEAD^2: 表示 HEAD 的第二个父母的信息
COMMIT_EDITMSG: 最后一次 commit 时的提交信息