[remote-branch] -- 远程分支, [branch] -- 本地分支
1 将当前目录初始化Git代码库
- git init [nama]
2 下载一个项目和他的整个代码历史
- git clone [url]
3 设置提交时的用户信息
- git config [--global] user.name "[name]"
- git config [--global] user.email "[email address]"
4 添加文件到暂存区
- git add [file] [file2] ... 指定文件
- git add [dir] 指定目录
- git add . 当前目录下全部文件
5 提交暂存区到仓库
- git commit [file] [file2] ... 指定文件
- git commit -a 提交自上次 commit 之后的变化,直接到仓库
- git commit -v 提交时显示所有的 diff 信息
- git commit --amend -m [message] 如果代码没有任何变化,使用上一次的commit提交
- git commit --amend [file] [file2] 重做上次commit,并包括指定文件的新变化
- git commit -m [messahe] 提交所有暂存区到仓库
6 取回远程主机某个分支内容,再与本地的指定分支合并
- git pull [远程主机名] [remote-branch]:[branch]
- 简写形式: git pull
7 将本地分支的更新,推送到远程主机
- git push [远程主机名] [branch]:[remote-branch]
- 简写形式: git push
8 查看分支
- git branch 本地所有分支
- git branch -r 远程所有分支
- git branch -a 本地远程所有分支
9 操作分支
- git checkout -b [branch] 从当前切出本地分支,并切换到该分支
- git branch [branch] [commit] 新建分支,并指向commit
- git branch --track [branch] [remote-branch] 新建分支,并和远程分支建立追踪关系
- git checkout [branch] 切换到该分支
- git checkout - 切换到上一个分支
- git branch --set-upstream [branch] [remote-branch] 在现有分支和指定的远程分支之间建立追踪关系
- git merge [branch] 合并指定分支到当前分支
- git branch -d [branch] 删除本地分支
- git push origin --delete [branch] 删除远程分支
- git branch -dr [remote-branch] 删除远程分支
10 查看信息
- git status 显示更改的文件
- git log 显示当前分支的版本历史
- git diff 显示当前分支的暂存区和工作区的差异
- git diff HEAD 显示工作区和当前分支最新版commit之间的差异
- git diff [first-branch]...[second-branch]- 显示两次提交之间的差异
- git diff --shortstat "@{0 day ago}" 显示今天你写了多少行代码
- git reflog 显示当前分支的最近几次提交
11 版本回退
- git checkout [file] 恢复暂存区的指定文件到工作区
- git checkout [commit] [file] 恢复某个commit的指定文件到暂存区和工作区
- git checkout . 恢复暂存区的所有文件到工作区
- git reset --hard 回退到上一个版本
- git reset --hard [commit] 回退到指定版本,同时恢复暂存区和工作区且与[commit]保持一致
- git reset --keep [commit] 恢复到指定版本。但是暂存区和工作区不变