官网(git-scm.com);中文网站(git-scm.com.cn);练习网站(learngitbranching.js.org)
0 基本概念
workspace(工作区): 项目目录下的所有文件(.git隐藏版本库除外) local repository(仓库区或本地仓库):工作区有一个隐藏目录.git,这个不属于工作区,这是版本库。其中版本库里面存了很多东西,其中最重要的就是暂存区(stage/index),还有Git为我们自动创建了第一个分支master,以及指向master的一个指针HEAD。 objects(Git 的对象库):位于 .git/objects目录下,里面包含了创建的各种对象及内容
- 当执行
git add时,工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中,暂存区的目录树被更新。 - 当执行
git commit时,暂存区的目录树写到版本库(对象库)中,HEAD会做相应的更新。 - 当执行
git reset HEAD命令时,暂存区的目录树会被重写,被HEAD指向的目录树所替换,但是工作区不受影响。 - 当执行
git rm --cached <file>命令时,会直接从暂存区删除文件,工作区则不做出改变。 - 当执行
git checkout .或者git checkout -- <file>命令时,会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区中的改动。 - 当执行
git checkout HEAD .或者git checkout HEAD <file>命令时,会用 HEAD 指向的分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。
1 配置
注意:* git config使用--system参数时, Git 会读写/etc/gitconfig文件,该文件含有 对系统上所有用户及他们所拥有的仓库都生效的配置值 git config使用--global参数时, Git 会读写~/.gitconfig文件,该文件含有只适用于该用户的配置值 git config使用--local参数时, Git 会读写 由用户定义的各个库中Git 目录下的配置文件(.git/config),该文件含有只适用于该Git库的配置值*
对你的commit操作设置关联的用户名
$ git config --global user.name "[name]"
对你的commit操作设置关联的邮箱地址
$ git config --global user.email "[email address]"
启用有帮助的彩色命令行输出
$ git config --global color.ui auto
来产看config的配置值
$ git config --list
2 创建仓库
仓库包括所有的文件、分支和提交(commits)
2.1 git init本地创建新仓库
2.2 git remote add [origin] [url] 将本地仓库与一个远端仓库连接起来
2.3 git clone [url] Clone一个远端仓库
3 分支
3.1 git branch 查看所有分支
3.2 git branch [branch]基于HEAD创建新[branch]
3.3 git branch -f [branch] [commit] 修改[branch]到[commit]
3.4 git branch -d [branch]删除[branch]
3.5 git branch -u [orgin/branch] [branch] [branch]跟踪[orgin/branch]
3.6 git switch [branch]切换到[branch]
3.7 git switch –c [branch]基于HEAD创建、切换新[branch]
3.7.1 git switch -c [branch] [orgin/branch]基于[orgin/branch]创建、切换新[branch],跟踪[orgin/branch]
4 .gitignore文件
通常在名为 .gitignore 的文件中完成一些文件不要用Git 跟踪。可以在 github.com/github/giti… 找到有用的 模板,推荐用gitignore.io快速生成。
5 同步更改
5.1 git fetch 更新所有的[orgin/branch]
5.2 git merge 当前[branch]合并跟踪的[orgin/branch]
5.3 git pull是 git fetch 和 git merge 的结合
5.3.1 git pull --rebase是 git fetch 和 git rebase 的结合
5.4 git fetch origin [orgin/branch]:[branch]
[orgin/branch]更新到[branch];- 如果
[branch]不存在,可自动创建 - 如果
[orgin/branch]不写为空,会本地创建新[branch]
5.5 git push当前[branch]上传到跟踪的[orgin/branch]
5.4.1 git push origin [branch] [branch]上传到origin
5.6 git push origin [commit]:[orgin/branch]
[commit]上传到[orgin/branch];- 如果
[orgin/branch]不存在,可自动创建; - 如果
[commit]不写为空,会删除[orgin/branch]
6 进行更改
6.1 git add [file]将[file]快照处理,存储stage区
6.2 git commit -m "[msg]"将stage区提交到当前[branch]
6.2.1 git commit --amend 提交并合并到上一次[commit]
6.3 git status 查看当前状态
6.3.1 git status -s 简洁方式查看当前状态
6.4 git blame [file]查看[file]的修改记录
6.5 git diff 查看workspace区的改动
6.6 git diff --staged查看stage区的改动
6.7 git diff HEAD查看workspace区和stage区的改动
6.8 git diff --staged [file] 查看[file]在stage区和[commit]的差异
6.9 git diff [branch-1] [branch-2]显示两次提交之间的差异
6.10 git log列出当前[branch]的历史commit
6.10.1 --oneline显示历史commit的简洁版本
6.10.2 --graph显示历史commit的拓扑图
6.10.3 -p显示每次commit的内容差异
6.10.4 --stat显示每次commit的简略统计信息
6.12 git reflog 查看所有操作记录(包括被删除的commit和 reset )
6.11 git show [commit]查看[commit]的改动
6.11 git show [commit]查看[commit]的改动
7 重做提交
7.1 git revert [commit]撤销到[commit],产生新commit
7.2 git reset [commit]撤销 [commit] 后的所有提交,提交到stage区
7.3 git reset --hard [commit]放弃 [commit] 后的所有提交
7.4 git reset --hard HEAD~10^2~
7.5 git reset --soft HEAD~10^2~
1 ~指向一条线上commit,~10表示前第10个[commit]
2 ^指向分叉线哪一条commit,^2表示第2条`[commit]
7.6 git checkout —- [file] [file]在workspace区修改撤销`
8 缓存修改
8.1 git stash把当前工作现场stash起来
8.2 git stash list查看stash内容
8.3 git stash apply stash@{0}恢复后,stash内容并不删除
8.4 git stash drop stash@{0}删除stash内容
8.5 git stash pop stash@{0}恢复的同时把stash内容也删掉
8.5 git stash clear删除stash的所有记录
9 变基
9.1 git rebase [branch] 当前分支变基到[branch]上
9.1.1 git rebase [branch1] [branch2] branch2变基到branch1上
9.1.2 git rebase -i [commit] 修改到[commit]之间的所有历史commit
9.2 git cherry-pick [commit]...[commit] 当前分支合并commit,生成新的commit
10 标签
10.1 git tag查看所有tag
10.2 git tag [tag]创建新本地[tag]
10.3 git tag -d [tag]删除本地[tag]
10.4 git checkout -b [branch] [tag]创建新本地[tag]指向[branch]
10.5 git tag [tag] [commit] 创建新本地[tag]指向[commit]
10.6 git tag -a [tag] -m [msg] [commit]创建新本地[tag]带有[msg]指向[commit]
10.7 git show [tag]查看[tag]信息
10.8 git push origin --tags推送所有本地tags到远端
10.9 git push origin :refs/tags/<tagname>删除远端tags
10.10 git describe [branch] 查看最近的标签,没有[branch]默认是HEAD
11 快进式合并
通常合并分支时,git一般使用”Fast forward”模式,会直接将 master 分支指向合并的分支,这种模式下进行分支合并会丢失分支信息,也就不能在分支历史上看出分支信息。
$ git merge --no-ff -m <msg> <branch>
术语表
- git: 一个开源的分布式版本控制系统
- GitHub: 一个托管和协作管理 Git 仓库的平台
- remote 远端: 一个 GitHub 上的公共仓库,所有小组成员通过它来交换修改
- clone: 一个仓库的本地版本,包含所有提交和分支
- commit 提交: 一个 Git 对象,是你整个仓库的快照的哈希值
- branch 分支: 一个轻型可移动的 commit 指针
- fork: 一个属于另一用户的 GitHub 上的仓库的副本
- pull request 拉取请求: 一处用于比较和讨论分支上引入的差异,且具有评审、评论、集成测试等功能的地方
- HEAD: 代表你当前的工作目录。使用
git checkout可移动 HEAD 指针到不同的分支、标记(tags)或提交