#注意:
在提交代码前一定要先hook,再pull,然后再add,commit,push。
#CREATE
克隆一个已经存在的远程库。
$git clone ssh://user@domain.com/repo.git
创建一个新的本地库。
$git init
#local changes
查看在工作区修改的文件。
$git status
查看文件差异
$git diff //查看工作区和暂存区的差异
$git diff --cached 或 git diff --staged //比较暂存区和最新提交的差异
$dif diff HEAD //比较工作区和最新提交的差异
$git diff <commit-id1> <commit-id2> //比较两个提交之间的差异
添加工作区所有的修改到暂存区(index)。
$git add .
只添加文件夹<file>
内的所有修改到暂存区。
$git add -p <file>
提交所有追踪文件的修改。
$git commit -a
提交上一次放在暂存区的修改。
$git commit -m "description string"
用新的提交来修改最顶端的提交,但是不能修改远程提交。
$git commit -amend
#commit history
显示所有的提交记录,从最新的一条提交开始显示。
$git log
$git log --pretty=oneline //一条提交只用一行来简化显示
显示特定文件的修改。
$git log -p <file>
谁,在什么时候,修改了文件<file>
里的什么内容。
$git blame <file>
查看历史提交记录。
$git reflog
#branches & tags
列出所有存在的分支。
$git branch
$git branch -a //显示本地和远程分支
$git branch -v //显示分支时,同时显示commit ID和提交说明
$git branch -av //在远程分支的基础上创建一个新的分支
切换分支。
$git checkout <branch>
$git checkout -b <new-branch> //切换并新建一个分支
$git checkout --track <remote/branch>
撤銷誤刪的文件<file>
$git checkout -- <file>
在现在的分支上新建一个分支
$git branch <new-branch>
基于远程分支创建一个新的分支并追踪
$git checkout --track <remote/branch>
刪除本地分支。
$git branch -d <branch>
删除远程分支。
$git branch -dr <remote/branch>
给目前的提交打标签
$git tag <tag-name>
将本地master分支推送到远程:
$git push origin master
将本地dev分支和远程dev分支建立链接:
git branch --set-upstream-to=origin/dev dev
#update & publish
将本次变更强行推送至服务器,这种方法可以覆盖错误的提交。
$git push --force <origin> <master>
取消本地目录下关联的远程库
$git remote remove <origin>
列出所有目前已经配置好的远程仓库
$git remote -v
显示远程仓库的信息
$git remote show <remote>
添加新的远程仓库。
$git remote add <shortname> <url>
从远程仓库下载所有的修改,但是不合并到HEAD中。
$git fetch <remote>
从远程库下载所有的修改、路径,并合并到HEAD中。
$git pull <remote> <branch>
推送本地库到远程库。
$git push <remote> <branch>
推送你的标签到远程
$git push --tags
#merge & rebase
合并分支<branch>
到当前分支
$git merge <branch>
重新绑定当前的HEAD到<branch>
分支上,但是不能重新绑定已推送到远程的提交。
$git rebase <branch>
终止重新绑定。
$git rebase --abort
解决冲突后继续重新绑定。
$git rebase --continue
用你配置的合并工具来解决冲突。
$git mergetool
用编辑器手动修改冲突,并在解决后标识文件已经解决。
$git add <resolved-file>
$git rm <resolved-file>
#undo
仅仅只是撤销已提交的版本库,不会修改暂存区和工作区
$git reset --soft <commit ID>
仅仅只是撤销已提交的版本库和暂存区,不会修改工作区
$git reset --mixed <commit ID>
彻底将工作区、暂存区和版本库记录恢复到指定的版本库
$git reset --hard <commit ID>
重置HEAD指针到以前的提交,并且保存所有的本地修改。
$git reset <commit ID>
彻底清空暂存区,而不改动工作区和版本库。
$git reset HEAD .
重置HEAD指针到以前的提交,并且保存未提交的本地修改。
$git reset --keep <commit ID>
删除暂存区和工作区里的文件<file>
$git rm <file>
删除暂存区和工作区的文件。
$git rm -f
撤销错误添加到暂存区里的文件。
$git rm --cache <file>
恢复误删的工作区的文件<file>
$git checkout -- <file>
以暂存区为基准来反向删除工作区。
$git clean -xfd
舍弃指定文件<file>
中的本地修改。
$git checkout HEAD <file>
还原一条提交。
$git revert <commit>