Choosing between git rebase and git merge remains one of the most discussed topics in the community. Some may say that you should always use merging, some may say that rebasing is a more correct way to do things. There is no right or wrong way of using these two commands. It mainly depends on the user and the workflow. In the scope of this topic we will show you how to rebase your branch.

Steps to rebasing branch

Here are the steps to follow while rebasing a branch:

Fetching changes

You should receive the latest changes from a remote git repository. Thus the first step is running git fetch:

git fetch

Integrating changes

The second step is running git rebase. Rebase is a Git command which is used to integrate changes from one branch into another. The following command rebase the current branch from master (or choose any other branch like develop, suppose, the name of remote is origin, which is by default):

git rebase origin/master

After git rebase, conflicts may occur. You should resolve them and add your changes by running git add command:

git add .
Do not run git commit after git add .

After resolving the conflicts and adding the changes to the staging area, you must run git rebase with the --continue option:

git rebase --continue
If you want to cancel the rebasing rather than resolving the conflicts, you can run the following:
git rebase --abort

Pushing changes

The final step is git push (forced). This command uploads local repository content to a remote repository. To do that, run the command below:

git push origin HEAD -f
--force that is the same as -f overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch.

Here is an alternative and safer way to push your changes:

git push --force-with-lease origin HEAD
--force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's work by force pushing.

Rebasing vs Merging

Rebasing and merging are both used to integrate changes from one branch into another differently. They are both used in different scenarios. If you need to update a feature branch, always choose to rebase for maintaining the branch history clean. It keeps the commit history out of the branch, contrary to the git merge command, which is its alternative. While, for individuals, rebasing is not recommended in the case of feature branch because when you share it with other developers, the process may create conflicting repositories. Once you need to put the branch changes into master, use merging. If you use merging too liberally, it will mess up git log and make difficult to understand the history. Merging preserves history whereas rebasing rewrites it. If you need to see the history absolutely the same as it happened, then use merge.

Fetching

The git fetch command downloads commits, files, and refs from a remote repository into the local repository. It updates your remote-tracking branches. The git fetch command allows you to see the progress of the central history, not forcing you to merge the changes into your repository. It does not affect your local work process.

How to Rebase Git Branch的更多相关文章

  1. git使用命令, 特别:git checkout -b a 与 git branch a区别

    摘自: https://my.oschina.net/u/587974/blog/74341 创建分支: $ git branch mybranch 切换分支: $ git checkout mybr ...

  2. 转 git使用命令, 特别:git checkout -b a 与 git branch a区别

    创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...

  3. 自动提交Git branch代码评审到Review Board系统

    背景 敏捷软件开发中,越小的反馈环,意味着软件质量越容易得到保证. 作为组件团队,我们的开发任务中,往往存在一些特性涉及到几十个功能点,开发周期持续数周或数月的情况.如何在开发过程中保证软件质量,是个 ...

  4. git branch(git 的branch 使用方法)

    查看分支:         $ git branch    该命令会类出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 创 ...

  5. 管理分支:git branch

    新建分支的意义: 创建一个单独的工作分支,避免对主分支master造成太多的干扰,也方便与他们交流协作. 进行高风险的工作时,创建一个实验性的分支,扔掉一个烂摊子总比收拾一个烂摊子好得多. 合并别人工 ...

  6. git branch使用

    (1) git配置global信息: git config --global user.name "Your Name" git config --global user.emai ...

  7. git branch用法总结

    git branch      git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如:   #git branch* master   newbranch ...

  8. Git branch 和 Git checkout常见用法

    git branch 和 git checkout经常在一起使用,所以在此将它们合在一起 1.Git branch 一般用于分支的操作,比如创建分支,查看分支等等, 1.1 git branch 不带 ...

  9. git branch几个简单操作

    1.git branch  该命令会列出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 2.git branch deve ...

随机推荐

  1. kali中安装arpspoof 报错

    情境今天在使用arpspoof这个命令的时候,提示没有命令找不到,此时就想着安装一下没想到,碰上kali源不支持的问题  解决所以,此时需要做的就是安装阿里云或者其他镜像  步骤1. vim /etc ...

  2. 【琉忆分享】新手如何学习PHP?附上PHP知识导图。

    你好,是我--琉忆.PHP程序员面试系列图书作者. 作为一名PHP开发者过来人,也是经历了菜鸟到老手的过程,在此给那些想学PHP的同学指条路,即使你是转行学PHP一样可以学会PHP. (如果觉得下面这 ...

  3. Solution -「AGC 012F」「AT 2366」Prefix Median

    \(\mathcal{Description}\)   Link.   给定序列 \(\{a_{2n-1}\}\),将 \(\{a_{2n-1}\}\) 按任意顺序排列后,令序列 \(b_i\) 为前 ...

  4. python中特殊参数self的作用

    特殊参数self的作用:self会接收实例化过程中传入的数据,当实例对象创建后,实例便会代替 self,在代码中运行. self代表的是类的实例本身,方便数据的流转.对此,我们需要记住两点: 第一点: ...

  5. [Python]数据类型、常量、变量和运算符(未完待续)

    标识符 一个ASCII标识符需要同时满足以下三个条件: 1.第一个字符必须是字母表中的字母或者下划线_ 2.标识符的其他部分由字母.数字.下划线组成 3.标识符对大小写是敏感的,即A与a是不同的 一般 ...

  6. 企业bi解决方案,商业智能BI作用

    ​随着越来越多的公司将商业智能BI引入到日常运营和商业决策中,BI的热点逐渐起来了.商业智能系统兴起,那BI对企业有什么好处呢? 简单的说,就是可以帮助企业管理者直观清晰的看到自己想要关注的数据,帮助 ...

  7. 同事会建模,会数据分析,会可视化图表,而你只会用EXCEL?

    ​小李是一家外企的数据分析师,平时处理的都是亿万行级别数据量的报表,为了可以胜任这份工作,小李早早地就学会了各种大数据工具,而且做出来的数据模型高度自动化,效率极高,为公司创造了非常大的价值.因为小李 ...

  8. vim中的incsearch不好用,没有动态效果,为什么——incsearch is not working

    虽然使用Linux好多年了,使用vim也好多年了,但是使用vim进行search的时候重来也没有想过要添加动态效果,近来突然有了兴致想要添加这个功能,不过发现居然不好用,在百度上找了好长时间居然没有一 ...

  9. 泛型种树(generic) 代码

    再这个案例中我们,为什么要使用泛型约束 使用约束的原因 约束指定类型参数的功能和预期. 声明这些约束意味着你可以使用约束类型的操作和方法调用. 如果泛型类或 方法对泛型成员使用除简单赋值之外的任何操作 ...

  10. C#特性(属性)Attribute

    先明确一个概念: 元数据..NET中元数据是指程序集中的命名空间.类.方法.属性等信息.这些信息是可以通过Reflection读取出来的. 再来看个例子: #define BUG //#define ...