创建版本库
//克隆远程版本库
$ 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 "提交的说明"
//修改最后一次提交
$ git commit --amend
查看提交历史
//查看提交历史
$ git log
//查看指定文件的提交历史
$ git log -p <file>
//以列表方式查看指定文件的提交历史
$ git blam <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 --tag
add
add相关命令很简单,主要实现将工作区修改的内容提交到暂存区,交由git管理
//添加当前目录的所有文件到暂存区
$ git add .
//添加指定目录到暂存区,包括子目录
$ git add <dir>
//添加指定文件到暂存区
$ git add <file1>
commit
commit相关命令也很简单,主要实现将暂存区的内容提交到本地仓库,并使得当前分支的HEAD向后移动一个提交点。
//提交暂存区到本地仓库,message代表说明信息
$ git commit -m <message>
//提交暂存区的指定文件到本地仓库
$ git commit <file1> -m <message>
//使用一次新的commit,替代上一次提交
$ git commit --amend -m <message>
branch
涉及到协作,自然会涉及到分支,关于分支,大概有展示分支,切换分支,创建分支,删除分支这四种操作。
//列出所有本地分支
$ git branch
//列出所有远程分支
$ git branch -r
//列出所有本地分支和远程分支
$ git branch -a
//新建一个分支,但依然停留在当前分支
$ git branch <branch-name>
//新建一个分支,并切换到该分支
$ git checkout -b <branch-name>
//新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track <branch><remote-branch>
//切换到指定分支,并更新工作区
$ git checkout <branch-name>
//删除分支
$ git branch -d <branch-name>
//删除远程分支
$ git push origin --delete <branch-name>
merge
merge命令把不同的分支合并起来。我们可能从master分支中切出一个分支,然后进行开发完成需求,中间经过分支的commit记录,最后开发完成需要合入master中,这便用到了merge。
//merge之前先拉一下远程仓库最新代码
$ git fetch <remote>
//合并指定分支到当前分支
$ git merge <branch>
reset
reset命令把当前分支指向另一个位置,并且相应的变动工作区和暂存区。
//只改变提交点,暂存区和工作目录的内容都不改变
$ git reset —soft <commit>
//改变提交点,同时改变暂存区的内容
$ git reset —mixed <commit>
//暂存区、工作区的内容都会被修改到与提交点完全一致的状态
$ git reset —hard <commit>
//让工作区回到上次提交时的状态
$ git reset --hard HEAD
push
上传本地仓库分支到远程仓库分支,实现同步。
//上传本地指定分支到远程仓库
$ git push <remote><branch>
//强行推送当前分支到远程仓库,即使有冲突
$ git push <remote> --force
//推送所有分支到远程仓库
$ git push <remote> --all
其他命令
//显示有变更的文件
$ git status
//显示当前分支的版本历史
$ git log
//显示暂存区和工作区的差异
$ git diff
//显示工作区与当前分支最新commit之间的差异
$ git diff HEAD
//选择一个commit,合并进当前分支
$ git cherry-pick <commit>