命令总结
工作区(Working Directory)-> 暂存区(Stage or Index)-> 本地仓库(Local Repository)-> 远程仓库(Remote repository)
工作区有一个隐藏目录 .git
,它是 Git 的版本库。

git 只能跟踪文本文件的改动,不能跟踪二进制文件(word 、图片、 视频等)
create & clone
git init |
create new repository |
git clone path/to/repository |
clone local repository |
git clone username@host:path/to/repository |
clone remote repository |
add & remove
git add <filename>/* |
add (all)changes to INDEX |
git rm <filename> |
remove/delete |
commit & synchronize
git commit -m "Commit message" |
commit changes |
git remote add origin <server> |
connect local repository to remote repository |
git push origin master |
push changes to remote repository |
git pull |
update local repository with remote changes |
branches
git branch |
查看分支 |
git checkout -b <branch> |
create new branch 相当于
git branch <branch>
git checkout <branch> |
git checkout master |
switch to master branch |
git branch -d <branch> |
delete branch |
git push origin <branch> |
push branch to remote repository |
merge
git merge <branch> |
合并指定分支到当前分支 |
git merge --no-ff -m "commit message" <branch> |
不要用 fast forward 方式合并 |
restore
git status |
查看仓库状态 |
git diff |
比较 工作区与暂存区 |
git diff --cached |
比较 暂存区与本地仓库 |
git diff master origin/master |
比较 本地仓库与远程仓库 |
git checkout (HEAD) <file>/. |
回到上次 add 或 commit 的状态 |
git reset (HEAD) <file> |
git add 的逆向,退回到 add 之前 |
git reset --hard <commit> |
回退版本,会重写工作区。比如:
git reset --hard HEAD^ 回退到上一个版本。 |
在 git 中,用 HEAD
表示当前版本,上一个版本就是 HEAD^
,上上一个版本就是 HEAD^^
HEAD~100
就是往上 100 个版本。
others
git --help |
查看命令 |
git log --pretty=oneline --abbrev-commit --graph |
查看log |
git reflog |
查看所有操作记录 |
git tag <tag> <commit ID> |
create tag |
git stash |
把工作现场收起来 |
git stash list |
查看 |
git stash pop |
相当于 git stash apply
git stash drop |
参考
工作区和暂存区
Git的4个阶段的撤销更改
git – 简明指南
高阶
Git 官方 book