Git: New branch, push, delete, merge

New Remote branch

Create a local branch:

$ git checkout -b local
  • This is equivalent to execute the following two commands:
$ git branch local
$ git checkout local

Look at the current state branch:

$ git branch
* local
  master
  release

An asterisk (*) indicates the current branch is located. The new branch is now the state is successfully created and has been switched to the new branch.

The local branch of the new push to a remote server

Remote branch and the local branch of the same name (of course free to named):

$ git push origin local:local

Use git branch -aview all the branches, you will see remotes/origin/dbg_lichen_starthe remote branch, indicating the success of the new remote branch.

Delete remote branch

I prefer simple way, pushing an empty remote branch to branch, in fact, equivalent to delete remote branch:

$ git push origin :local

You can also use:

$ git push origin --delete local

Both approaches can delete the specified remote branch

Merge branch

After the completion of branch development, and finally merge to merge back into the branch.

There are two merging branches: Use merge or rebase.

The use of these two methods, the history of the branch will be very different after the merger.

merge

Use merge history can combine multiple processes.

As shown below, bugfix bifurcated branch is branched off from the master.

When the master branch to branch, merge bugfix if the master branch of the state has not been changed, then this merger is very simple . History bugfix branch of the master branch that contains all historical records, by moving the position of the master branch to branch bugfix in the latest, Git will merge. Such merging is called fast-forward (fast-forward) were combined.

 

But history's master branch records may have a new update bugfix branch after branch off. In this case, the content should modify the content of the master branch and bugfix branches converge together.

Therefore, the merger of the two changes will generate a submission. At this time, master HEAD branch will move to the submission.

note:

 

When the merge is performed, if set non fast-forward option , will generate even in the case of merger can fast-forward and submit a new consolidation.

执行non fast-forward后,分支会维持原状。那么要查明在这个分支里的操作就很容易了。

rebase

跟merge的例子一样,如下图所示,bugfix分支是从master分支分叉出来的。

如果使用rebase方法进行分支合并,会出现下图所显示的历史记录。现在我们来简单地讲解一下合并的流程吧。

使用rebase合并分支

首先,rebase bugfix分支到master分支, bugfix分支的历史记录会添加在master分支的后面。如图所示,历史记录成一条线,相当整洁。

这时移动提交X和Y有可能会发生冲突,所以需要修改各自的提交时发生冲突的部分。

使用rebase合并分支

rebase之后,master的HEAD位置不变。因此,要合并master分支和bugfix分支,即是将master的HEAD移动到bugfix的HEAD这里。

使用rebase合并分支

注意:

Merge和rebase都是合并历史记录,但是各自的特征不同。

  • merge
    保持修改内容的历史记录,但是历史记录会很复杂。
  • rebase
    历史记录简单,是在原有提交的基础上将差异内容反映进去。
    因此,可能导致原本的提交内容无法正常运行。

您可以根据开发团队的需要分别使用merge和rebase。
例如,想简化历史记录,

  • 在topic分支中更新merge分支的最新代码,请使用rebase。
  • 向merge分支导入topic分支的话,先使用rebase,再使用merge。

 

参考链接:https://backlog.com/git-tutorial/cn/stepup/stepup1_4.html

Guess you like

Origin blog.csdn.net/fly910905/article/details/92991796
Recommended