获取与创建项目
git init
# 在当前目录新建一个Git代码库
$ git init
# 在指定目录初始化为Git代码库
$ git init [project-name]
- 1
- 2
- 3
- 4
- 5
git clone
# 下载一个项目和它的整个代码历史到当前路径
$ git clone [url]
# 下载一个项目和它的整个代码历史到指定目录
$ git clone [url] [project-name]
- 1
- 2
- 3
- 4
- 5
配置
Git的设置文件为.gitconfig
,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
git config
# 显示当前的Git配置
$ git config --list
# 编辑Git配置文件
$ git config -e [--global]
# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
# 设置大小写敏感
$ git config core.ignorecase false
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
暂存区操作
git add
# 添加指定文件到暂存区
$ git add [file1] [file2] ...
# 添加指定目录到暂存区,包括子目录
$ git add [dir]
# 添加当前目录的所有文件到暂存区
$ git add .
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
git rm
# 删除暂存区中的指定文件,但该文件会保留在工作区
$ git rm --cached [file]
# 在暂存区与工作区中删除指定文件
$ git rm -f [file]
- 1
- 2
- 3
- 4
- 5
git mv
# 在暂存区与工作区中重命名或移动文件
$ git mv [file-original] [file-renamed]
- 1
- 2
提交操作
git commit
# 提交暂存区到仓库区
$ git commit -m [message]
# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]
# 提交工作区所有已跟踪文件的变化到仓库区
$ git commit -am [message]
# 提交时显示所有diff信息
$ git commit -v
# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]
# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
撤销操作
git checkout
# 恢复暂存区的指定文件到工作区
$ git checkout [file]
# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]
# 恢复暂存区的所有文件到工作区
$ git checkout .
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
git reset
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [HEAD] [file1] [file2] ...
# 重置暂存区的指定文件夹,与上一次commit保持一致,但工作区不变
$ git reset HEAD [dir]
# 重置暂存区,与上一次commit保持一致,但工作区不变
$ git reset [HEAD]
# 重置当前分支的HEAD为指定commit,同时重置暂存区,与指定commit保持一致,但工作区不变
$ git reset [commit]
# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
# 慎用,指定commit之后的提交会消失
$ git reset --hard [commit]
# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
git revert
# 撤销上次提交,并把这次撤销作为一次最新的提交
$ git revert HEAD
# 撤销指定的版本,撤销也会作为一次提交进行保存
$ git revert [commit]
- 1
- 2
- 3
- 4
- 5
贮藏操作
git stash
# 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中
$ git stash
# 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容
$ git stash pop
# 读取指定版本号为stash@{1}的保存内容,恢复工作区的相关内容
$ git stash apply stash@{1}
# 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复
$ git stash list
# 清空Git栈
$ git stash clear
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
信息查看
git status
# 显示文件信息变更,-s参数输出简短结果
$ git status [-s]
- 1
- 2
git diff
# 显示工作区相比于暂存区中文件的改动
$ git diff
# 显示摘要而非改动的详细信息
$ git diff --stat
# 查看暂存区中指定文件相比于上次提交的改动
$ git diff --cached [file]
# 查看工作区文件相比于上次提交的改动
$ git diff HEAD
# 显示当前分支与其他分支之间的差异
$ git diff [other-branch]
# 显示两个分支之间的差异
$ git diff [first-branch] [second-branch]
# 比较上次提交commit和上上次提交
$ git diff HEAD HEAD^
# 显示两次提交之间的差异
$ git diff [first-commit] [second-commit]
# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
git log
# 显示当前分支的版本历史
$ git log
# 显示指定分支的版本历史
$ git log [branch]
# 显示在第一个分支且不在第二个分支的提交信息
$ git log [first-branch] ^[second-branch]
# 显示当前分支的简洁版本历史
$ git log --oneline
# 显示commit历史,以及标签信息
$ git log --decorate
# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat
# 显示commit历史,以及每次commit修改的内容
$ git log -p
# 以拓扑结构显示当前分支的简洁版本历史
$ git log --oneline --graph
# 显示指定用户过去5次的提交日志
$ git log --author=[author] --oneline -5
# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]
# 显示指定文件相关的每一次diff
$ git log -p [file]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
git shortlog
# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn
- 1
- 2
git blame
# 显示指定文件是什么人在什么时间修改过
$ git blame [file]
- 1
- 2
git show
# 显示指定提交的元数据和内容变化
$ git show [commit]
# 显示指定标签的元数据和内容变化
$ git show [tag]
# 显示指定提交发生变化的文件
$ git show --name-only [commit]
# 显示指定提交的指定文件内容
$ git show [commit]:[filename]
# 显示上次提交的元数据和内容变化
$ git show [HEAD]
# 显示上上次提交的元数据和内容变化
$ git show HEAD^
# 显示倒数第3次提交的元数据和内容变化
$ git show HEAD~2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
分支管理
git branch
# 新建分支
$ git branch [branch-name]
# 新建一个分支,指向指定commit
$ git branch [branch-name] [commit]
# 重命名本地分支
$ git branch -m [old-branchname] [new-branchname]
# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch-name] [remote-branch]
# 在现有分支与指定的远程分支之间建立追踪关系
$ git branch --set-upstream-to=origin/[remote-branch] [branch-name]
# 删除指定分支
$ git branch -d [branch-name]
# 删除与指定远程分支的追踪关系
$ git branch -dr origin/[remote-branch]
# 列出所有本地分支
$ git branch
# 列出所有远程分支
$ git branch -r
# 列出所有本地分支和远程分支
$ git branch -a
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
git checkout
# 查看当前分支信息
$ git checkout
# 切换到指定分支
$ git checkout [branch]
# 切换回上一分支
$ git checkout -
# 新建分支并切换到该分支
$ git checkout -b [branch]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
git merge
# 合并指定分支到当前分支
$ git merge [branch]
- 1
- 2
git cherry-pick
# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]
- 1
- 2
冲突解决
在 Git 中,可以用 git add 要告诉 Git 文件冲突已经解决
# 修改冲突文件
$ vim [file]
# 提交修改文件
$ git add [file]
$ git commit
- 1
- 2
- 3
- 4
- 5
- 6
标签操作
如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用git tag给它打上标签。比如说,我们想为项目发布一个”1.0”版本。我们可以用git tag -a v1.0
命令给最新一次提交打上(HEAD)”v1.0”的标签。
git tag
# 创建标签并指定标签信息
$ git tag -a v1.0 -m [message]
# 给指定提交创建标签并指定标签信息
$ git tag -a v0.9 -m [message] [commit]
# 查看所有标签
$ git tag
# 查看特定标签
$ git tag --list 'v1.*'
# 删除指定标签
$ git tag -d v1.0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
远程仓库
git remote
# 列出所有关联的远程仓库
$ git remote
# 列出所有关联的远程仓库的详细信息
$ git remote -v
# 将远程仓库与本地仓库关联并指定别名
$ git remote add [shortname] [url]
# 修改远程仓库在本地的别名(默认为origin)
$ git remote rename [old-shortname] [new-shortname]
# 查看远程仓库的详细信息
$ git remote show origin
# 移除远程仓库与本地仓库的关联
$ git remote rm origin
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
git push
# 将本地指定分支推送到指定远程分支(若不存在则新建)
$ git push origin [branch-name]:[remote-branch]
# 将本地指定分支推送到指定远程分支(若不存在则新建),同时指定origin为默认主机,并建立追踪关系
$ git push origin -u [branch-name]:[remote-branch]
# 将本地指定分支推送到同名远程分支(若不存在则新建)
$ git push origin [branch-name]
# 将当前分支推送到存在追踪关系的远程分支
$ git push
# 不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机
$ git push --all
# 如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做git pull合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用-–force选项
$ git push --force
# 删除远程分支
$ git push origin --delete [branch-name]
# 推送一个空分支到远程分支,相当于删除远程分支
$ git push origin :[branch-name]
# 推送本地指定tag
$ git push origin [tag-name]
# 推送本地所有tag
$ git push --tags
# 删除远程tag
$ git push origin --delete tag [tag-name]
# 推送一个空tag到远程tag,相当于删除远程tag
$ git push origin :refs/tags/[tag-name]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
git pull
# 将指定远程分支拉取到本地指定分支(若不存在则新建)
$ git pull origin [remote-branch]:[branch-name]
# 将默认远程分支master拉取到本地指定分支(若不存在则新建)
$ git pull origin :[branch-name]
# 将与指定远程分支取回本地(未与当前分支合并)
$ git pull origin [remote-branch]
# 拉取与当前分支存在追踪关系的远程分支
$ git pull
# 强制拉取远程分支,并重写工作区
$ git pull --force
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
git fetch
# 将指定远程分支拉取到本地指定分支(若不存在则新建)
$ git fetch origin [remote-branch]:[branch-name]
# 将默认远程分支master拉取到本地指定分支(若不存在则新建)
$ git fetch origin :[branch-name]
# 将指定远程分支取回本地(未与当前分支合并)
$ git fetch origin [remote-branch]
# 将与当前分支存在追踪关系的远程分支取回本地(未与当前分支合并)
$ git fetch
# 将取回的远程分支合并到当前分支(FETCH_HEAD可省略)
$ git merge [FETCH_HEAD]
# 推送本地指定tag
$ git push origin [tag-name]
# 获取远程所有tag
$ git fetch --tags
# 获取远程指定tag
git fetch origin tag [tag-name]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
附图
参考链接
常用 Git 命令清单
菜鸟教程–Git 教程
git常用命令解释
Git push 常见用法
Git 远程分支常用管理–查看+删除+重命名
Git查看、删除、重命名远程分支和tag
git常用命令之git push使用说明
Git Stash用法
git入门(5)-Git revert和git reset版本的回退
Git 常用命令