Note: Git Command 须在 repository 目录下操作, Ctrl+C, 退出指令.
1、版本库(repository)操作
版本库又名仓库, 此处可以简单理解成一个目录,在这个目录里面,每个文件的修改、删除,Git都可以跟踪管理。
版本库的上级目录称为工作目录,只有工作目录中的文件才能保存到版本库中。
key word | command | function |
---|
init | git init | 创建版本库,创建成功,会在此目录下生成一个.git的隐藏目录 |
remote | git remote add origin [url for the repository] | 本地创建的Git管理项目关联到GitHub上某个Repository |
clone | git clone [url for the repository] | 从Github获取项目 |
2、分支(branch)操作
key word | command | function |
---|
status | git status | 查看当前分支处于哪个分支,有哪些变动 |
| git status -s | 简单模式查看本地库/暂存区/工作目录的文件变动 |
reset | git reset --hard origin/master | 将本地分支的HEAD切回远程分支master最新的version |
branch | git branch | 查看本地所有分支,当前分支前标有* |
| git branch -r | 查看远程所有分支 |
| git branch -a | 查看本地及远程所有分支,当前分支前标有* |
| git branch [branch] | 创建本地分支 |
| git branch -m [old branch] [new branch] | 重命名已merge的分支 |
| git branch -M [old branch] [new branch] | 强制重命名分支 |
| git branch -d [branch] | 删除已merge的分支 |
| git branch -D [branch] | 强制删除,适用于本地分支存在未提交或者未merge的改动 |
diff | git diff | 对比当前分支缓冲区与暂存区 |
| git diff --cached | 对比当前分支暂存区文件及其更新内容 |
| git diff [version 1] [version 2] | 对比当前分支两个版本哪些文件更新了哪些内容 |
| git diff [branch 1] [branch 2] | 对比[branch 1]和[branch 2],哪些文件有更新,更新了哪些内容,选定分支可以是本地也可以是远程分支 |
| git diff [branch 1] [branch 2] --stat | 对比[branch 1]和[branch 2],哪些文件有更新,只展示文件名,不展示文件内容的差异 |
merge | git merge [branch] | 合并[branch]到当前分支 |
checkout | git checkout [branch] | 切换到[branch] |
| git checkout -b [branch] | 创建并切换到[branch] |
| git checkout -b [local branch] origin/[remote branch] | 创建本地分支[local branch] 并与远程分支 [remote branch] 建立映射关系 |
commit | git commit -m [commit message] | 提交代码到本地仓库 |
fetch | git fetch | 将本地branch,远程branch,更新code到最新状态 |
| git fetch -p | 更新本地远程分支,删除远程分支中已被删除的branch |
pull | git pull | 获取远程分支最新code, 同步到当前分支 |
| git pull origin [branch] | 获取指定远程分支[branch]最新code, 同步到当前分支 command == git fetch + git merge origin [branch] |
push | git push origin [local branch]:[remote branch] | 推送本地到远程仓库, push的一般形式 |
| git push origin [local branch] | 省略远程分支名时,表示将本地分支[local branch]推送到与之存在追踪关系的远程分支上【通常情况下,两者同名】,当这个远程分支不存在时,则会被新建 |
| git push origin :[remote branch] | 省略本地分支名时,表示删除指定的远程分支,因为此时等同于推动一个空的本地分支到此远程分支[remote branch], command == git push origin --delete [remote branch] |
3、文件(file)操作
file支持任意格式,.xml,.json, .java, .groovy ...
key word | command | function |
---|
cat | cat [file] | 查看[file]内容 |
add | git add [file] | 提交指定的修改后的文件[file]到暂存区 |
| git add -all | 提交所有修改后的文件到暂存区 |
commit | git commit [file] -m [message] | 提交修改后的文件到本地仓库 |
reset | git reset --cached [file] | 从缓存区移除文件,将其变为未跟踪的状态 |
diff | git diff [file] | 对比尚未暂存的[file]更新了哪些内容,退出按q键 |
| git diff --cached [file] | 对比暂存区的[file]更新了哪些内容,退出按q键 |
| git diff [version1:file] [version2:file] | 对比[file]两个版本的不同,退出按q键 |
checkout | git checkout --[file] | 恢复[file]到最后一次添加到暂存区的状态 |
vim | vim [file] | 编辑[file] |