我们主分支上面提交代码,我们使用git commit的命令进行提交,很简单,我在进行详细的描述。
分支
创建分支
我们通过实际操作看一下分支是什么样子的,比如说我们先要创建一个名字为newImage的分支。创建分支前:

使用命令 git branch newImage
之后

在新的分支上面提交内容
当我们向新的分支上面提交一些新的东西,如果我们直接执行git commit
。

我们会发现master分支前进了,但是newImage分支还是待在原地。我们仔细看一下上图,我们发现现在在master上面有一个星号,这表示的是我们现在在master分支上面。所以我们正确的操作是,先切换到新分支上面,然后进行提交代码,我们执行的命令是:
git checkout newImage ; git commit

分支与合并
merge
假如我们现在有两个分支,每一个分支上面都有一个独立的提交记录。这就意味着没有一个分支包含我们所有的修改的内容,我们要将这两个分支进行合并,现在我们看一下分支的情况:

现在我们将bugFix合并到master分支上面,使用命令:git merge bugFix

但是我们希望将master 分支合并到bugFix上面,我们需要先进行分支的切换,然后在进行合并,具体的命令如:git checkout bugFix; git merge master

上面这样执行的是因为master继承自bugFix,多以Git什么都不需要做,知识简单的把bugFix移动到master指向的那个提交记录就可以了。
rebase
我们要是想将bugFix分支直接移到master分支上面,移动以后会让两个分支的功能看起来像按顺序开发的,但是实际上他们是并行开发的

我们执行git rebase master
,现在bugFix的分支上面的工作在master分支的最顶端,我们看一下C3依然存在,而C3'是我们Rebase到master分支上的C3副本,现在的问题是master还没有更新,我们线面进行master分支的更新。

现在我们先切换到master分支上面,把它rebase到bugFix上面,合并的命令:git rebase bugFix

在提交树上移动
HEAD
HEAD是一个对当前检出记录的符号引用-- 也就是指向我们正在这个基础上进行工作的提交记录。
HEAD总是指向当前分支最近一次的提交情况。HEAD通常情况下指向分支名,在提交的时候,改变了分支的状态,这一改变导致HEAD是可见的。

执行命令:git checkout C1; git checkout master ;git commit; git checkout c2

分离的HEAD
分离的HEAD就是让其指向了某一个具体的提交记录,而不再是分支名
我们看一下现在的情况是:HEAD指向master,master指向c1

如果我们使用指令git checkout C1

现在的执行变成了:HEAD指向C1
相对引用
- 我们可以使用^向上移动一个提交记录
- 使用^<num>向移动多个提交记录

我们可以看一下,根据上面的意思也就是master^ 就是master的父节点。
所以我们执行命令git checkout master ^

我们也可以将HEAD作为相对引用的参照,我们现在就举个栗子:
执行命令:git checkout C3; git checkout HEAD ^; git checkout HEAD ^; git checkout HEAD ^
执行命令前:

执行命令后:

依次上移,直到:

~操作符
如果在提交树上向上移动很多步骤的话,要敲很多的^,这样的就很麻烦,Git也考虑到了这一点,于是又引入先操作符~。
这个操作符后面可以和一个数字(可选,不跟数字时与^相同,向上移动一次),指定向上移动多少次。
后退之前:

执行:git checkout HEAD~4

我使用相对引用最多的就是移动分支,可以直接使用-f选项让分支指向另一个提交。
git branch -f master HEAD^3
:会将master分支强制指向HEAD的第3级父提交。
执行前:

执行后:

网友评论