Git 简明教程

Git - Managing Branches

分支操作允许创建另一行开发内容。我们可以使用此操作将开发进程分叉到两个不同的方向。例如,我们为 6.0 版本发布了一个产品,我们可能希望创建一个分支,以便使 7.0 功能的开发与 6.0 的 bug 修复保持分离。

Branch operation allows creating another line of development. We can use this operation to fork off the development process into two different directions. For example, we released a product for 6.0 version and we might want to create a branch so that the development of 7.0 features can be kept separate from 6.0 bug fixes.

Create Branch

Tom 使用 git branch <branch name> 命令创建一个新分支。我们可以从现有分支创建一个新分支。我们可以使用特定的提交或标签作为起点。如果没有提供任何特定提交 ID,那么将以 HEAD 为起点创建分支。

Tom creates a new branch using the git branch <branch name> command. We can create a new branch from an existing one. We can use a specific commit or tag as the starting point. If any specific commit ID is not provided, then the branch will be created with HEAD as its starting point.

[jerry@CentOS src]$ git branch new_branch

[jerry@CentOS src]$ git branch
* master
new_branch

新分支已创建;Tom 使用 git branch 命令列出可用分支。Git 在当前签出的分支前显示星号标记。

A new branch is created; Tom used the git branch command to list the available branches. Git shows an asterisk mark before currently checked out branch.

下面显示创建分支操作的图片表示形式 −

The pictorial representation of create branch operation is shown below −

before branch create
after branch create

Switch between Branches

Jerry 使用 git checkout 命令在分支之间切换。

Jerry uses the git checkout command to switch between branches.

[jerry@CentOS src]$ git checkout new_branch
Switched to branch 'new_branch'
[jerry@CentOS src]$ git branch
master
* new_branch

Shortcut to Create and Switch Branch

在上面的示例中,我们分别使用了两个命令创建并切换分支。Git 为 checkout 命令提供 –b 选项;此操作创建一个新分支,然后立即切换到新分支。

In the above example, we have used two commands to create and switch branches, respectively. Git provides –b option with the checkout command; this operation creates a new branch and immediately switches to the new branch.

[jerry@CentOS src]$ git checkout -b test_branch
Switched to a new branch 'test_branch'

[jerry@CentOS src]$ git branch
master
new_branch
* test_branch

Delete a Branch

可以通过使用 –D 选项和 git 分支命令删除分支。但在删除现有分支之前,请切换到另一个分支。

A branch can be deleted by providing –D option with git branch command. But before deleting the existing branch, switch to the other branch.

Jerry 目前在 test_branch ,他希望删除该分支。因此,他切换分支并删除分支,如下所示。

Jerry is currently on test_branch and he wants to remove that branch. So he switches branch and deletes branch as shown below.

[jerry@CentOS src]$ git branch
master
new_branch
* test_branch

[jerry@CentOS src]$ git checkout master
Switched to branch 'master'

[jerry@CentOS src]$ git branch -D test_branch
Deleted branch test_branch (was 5776472).

现在,Git 只会显示两个分支。

Now, Git will show only two branches.

[jerry@CentOS src]$ git branch
* master
new_branch

Rename a Branch

Jerry 决定在他的字符串操作项目中添加对宽字符的支持。他已经创建了一个新分支,但分支名称不恰当。因此,他使用 –m 选项,后跟 old branch namenew branch name ,更改分支名称。

Jerry decides to add support for wide characters in his string operations project. He has already created a new branch, but the branch name is not appropriate. So he changes the branch name by using –m option followed by the old branch name and the new branch name.

[jerry@CentOS src]$ git branch
* master
new_branch

[jerry@CentOS src]$ git branch -m new_branch wchar_support

现在,git branch 命令将显示新的分支名称。

Now, the git branch command will show the new branch name.

[jerry@CentOS src]$ git branch
* master
wchar_support

Merge Two Branches

杰瑞实现了一个返回宽字符长度的字符串长度的函数。现在代码将显示如下所示:

Jerry implements a function to return the string length of wide character string. New the code will appear as follows −

[jerry@CentOS src]$ git branch
master
* wchar_support

[jerry@CentOS src]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git diff

上述命令会产生以下结果 −

The above command produces the following result −

t a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..8fb4b00 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,4 +1,14 @@
#include <stdio.h>
+#include <wchar.h>
+
+size_t w_strlen(const wchar_t *s)
+
{
   +
   const wchar_t *p = s;
   +
   +
   while (*p)
   + ++p;
   + return (p - s);
   +
}

在测试后,他提交并推送下代码更改到新的分支。

After testing, he commits and pushes his changes to the new branch.

[jerry@CentOS src]$ git status -s
M string_operations.c
?? string_operations

[jerry@CentOS src]$ git add string_operations.c

[jerry@CentOS src]$ git commit -m 'Added w_strlen function to return string lenght of wchar_t
string'

[wchar_support 64192f9] Added w_strlen function to return string lenght of wchar_t string
1 files changed, 10 insertions(+), 0 deletions(-)

请注意,杰瑞将这些更改推送到新的分支,这才使他使用分支名称 wchar_support ,而不是 master 分支。

Note that Jerry is pushing these changes to the new branch, which is why he used the branch name wchar_support instead of master branch.

[jerry@CentOS src]$ git push origin wchar_support   <−−− Observer branch_name

上述命令将产生以下结果。

The above command will produce the following result.

Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 507 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
To gituser@git.server.com:project.git
* [new branch]
wchar_support -> wchar_support

在提交更改后,新分支将显示如下所示:

After committing the changes, the new branch will appear as follows −

commit in branch

汤姆很想知道杰瑞在他的私有分支中做了什么,并从 wchar_support 分支中查看了日志。

Tom is curious about what Jerry is doing in his private branch and he checks the log from the wchar_support branch.

[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src

[tom@CentOS src]$ git log origin/wchar_support -2

上述命令将产生以下结果。

The above command will produce the following result.

commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 16:10:06 2013 +0530

Added w_strlen function to return string lenght of wchar_t string


commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 10:21:20 2013 +0530

Removed executable binary

通过查看提交消息,汤姆得知杰瑞实现了一个宽字符的 strlen 函数,他还希望在主分支中实现相同的功能。他决定不重新实现,而是通过将自己的分支与主分支合并来采用杰瑞的代码。

By viewing commit messages, Tom realizes that Jerry implemented the strlen function for wide character and he wants the same functionality in the master branch. Instead of re-implementing, he decides to take Jerry’s code by merging his branch with the master branch.

[tom@CentOS project]$ git branch
* master

[tom@CentOS project]$ pwd
/home/tom/top_repo/project

[tom@CentOS project]$ git merge origin/wchar_support
Updating 5776472..64192f9
Fast-forward
src/string_operations.c | 10 ++++++++++
1 files changed, 10 insertions(+), 0 deletions(-)

在合并操作后,主分支将显示如下所示:

After the merge operation, the master branch will appear as follows −

merge branch

现在,分支 wchar_support 已与主分支合并。我们可以通过查看提交消息或通过查看对 string_operation.c 文件所做的修改来验证这一点。

Now, the branch wchar_support has been merged with the master branch. We can verify it by viewing the commit message or by viewing the modifications done into the string_operation.c file.

[tom@CentOS project]$ cd src/

[tom@CentOS src]$ git log -1

commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3
Author: Jerry Mouse
Date: Wed Sep 11 16:10:06 2013 +0530

Added w_strlen function to return string lenght of wchar_t string

[tom@CentOS src]$ head -12 string_operations.c