Git branch management

Almost every version control system supports branches in some form. Using a branch means that you can detach from the main line of development and continue working without affecting the main line.

Some people call Git's branching model "the nirvana feature", and it is because of it that it separates Git from the family of version control systems.

Create branch command:

git branch (branchname)

Switch branch command:

git checkout (branchname)

When you switch branches, Git replaces the contents of your working directory with a snapshot of the branch's last commit, so multiple branches don't require multiple directories.

Merge branch command:

git merge

You can merge into the unified branch multiple times, or you can choose to delete the merged branch directly after the merge.


list branches

List branches basic command:

git branch

With no arguments, git branch will list your local branches.

[root@localhost newrepo]# git branch
* master

What this example means is that we have a branch called "master" and that branch is the current branch.

When you do git init, Git will create the "master" branch for you by default.

If we create a branch manually. Just execute git branch (branchname).

[root@localhost newrepo]# git branch testing
[root@localhost newrepo]# git branch
* master
  testing

Now we can see that there is a new branch testing.

This way a new branch is created after the last commit of the update, and if there is an update commit later, and then switch to the "testing" branch, Git will restore your working directory to what it was when you created the branch

Next to demonstrate how to switch branches, we use git checkout (branch) to switch to the branch we want to modify.

[root@localhost newrepo]# ls
test.c
[root@localhost newrepo]# echo 'runoob.com' > test.txt
[root@localhost newrepo]# git add .
[root@localhost newrepo]# git commit -m 'add test.txt'
[master 76f8be1] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git checkout testing
Switched to branch 'testing'
[root@localhost newrepo]# ls
test.c[root@localhost newrepo]# git branch
  master
* testing

当切换到"testing"分支的时候,我们添加的新文件test.txt被移除了, 切换回"master"分支的时候,它有重新出现了。

[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# ls
test.c  test.txt

可以使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下,从而在该分支中操作。

[root@localhost newrepo]# git checkout -b newtest
Switched to a new branch 'newtest'
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git rm test.c
rm 'test.c'
[root@localhost newrepo]# ls
test.txt
[root@localhost newrepo]# git commit -am 'remove test.c'
[newtest 5ceb56c] remove test.c
 1 file changed, 4 deletions(-)
 delete mode 100644 test.c
[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# ls
test.c  test.txt

我们创建了一个分支,在该分支的上下文中移除了一个文件,然后切换回我们的主分支,那个文件又回来了。

使用分支将工作切分开来,从而让我们能够在不同上下文中做事,并来回切换。

删除分支

删除分支命令:

git branch -d (branchname)

要删除"testing"分支:

[root@localhost newrepo]# git branch
* master
  newtest
  testing
[root@localhost newrepo]# git branch -d testing
Deleted branch testing (was afe659f).
[root@localhost newrepo]# git branch
* master
  newtest

分支合并

一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。 你可以使用以下命令将任何分支合并到当前分支中去:

git merge
[root@localhost newrepo]# git branch
* master
  newtest
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git merge newtest
Updating 76f8be1..5ceb56c
Fast-forward
 test.c | 4 ----
 1 file changed, 4 deletions(-)
 delete mode 100644 test.c
[root@localhost newrepo]# ls
test.txt

以上实例中我们将 newtest 分支合并到主分支去,test2.txt 文件被删除。

合并冲突

合并并不仅仅是简单的文件添加、移除的操作,Git 也会合并修改。

[root@localhost newrepo]# git branch
* master
  newtest
[root@localhost newrepo]# cat test.txt
runoob.com

首先,我们创建一个叫做"change_site"的分支,切换过去,我们将内容改为 www.runoob.com 。

[root@localhost newrepo]# git checkout -b change_site
Switched to a new branch 'change_site'
[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# head -1 test.txt
www.runoob.com
[root@localhost newrepo]# git commit -am 'changed the site'
[change_site f79f097] changed the site
 1 file changed, 1 insertion(+), 1 deletion(-)

上面的命令将修改的内容提交到 "change_site" 分支中。 

现在,切换回 "master" 分支,我们可以看到内容恢复到我们修改前的,我们再次修改test.txt文件。

[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# head -1 test.txt
runoob.com
[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# cat test.txt
runoob.com
new line
[root@localhost newrepo]# git diff
diff --git a/test.txt b/test.txt
index 3277d64..08a84ff 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 runoob.com
+new line
[root@localhost newrepo]# git commit -am 'new line'
[master 4f9493f] new line
 1 file changed, 1 insertion(+)

现在这些改变已经记录到 "master" 分支了。

此时change_site分支的test.txt内容为

www.runoob.com

master分支的test.txt的内容为

runoob.com
new line

接下来我们将 "change_site" 分支合并到主分支。

[root@localhost newrepo]# git merge change_site
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@localhost newrepo]# cat test.txt
<<<<<<< HEAD
runoob.com
new line
=======
www.runoob.com
>>>>>>> change_site

我们将change_site分支合并到 "master" 分支,一个合并冲突就出现了,接下来我们需要手动去修改它。

[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# cat test.txt
www.runoob.com
new line
[root@localhost newrepo]# git diff
diff --cc test.txt
index 08a84ff,43b979a..0000000
--- a/test.txt
+++ b/test.txt
@@@ -1,2 -1,1 +1,2 @@@
- runoob.com
+ www.runoob.com
 +new line

在 Git 中,我们可以用 git add 要告诉 Git 文件冲突已经解决

[root@localhost newrepo]# git status -s
UU test.txt
[root@localhost newrepo]# git add test.txt
[root@localhost newrepo]# git status -s
M  test.txt
[root@localhost newrepo]# git commit
[master bd7f492] Merge branch 'change_site'
[root@localhost newrepo]# git checkout change_site
Switched to branch 'change_site'
[root@localhost newrepo]# cat test.txtwww.runoob.com

现在我们成功解决了合并中的冲突,并提交了结果。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326537636&siteId=291194637