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. elasticsearch搜索引擎的常用方法

    1.term和termsterm和terms等查询,不会对查询对字段进行分词处理,适合于date.num.id等确切数据进行搜索 如果需要查询keywords,则查询等keywords必须是查询字段中 ...

  2. Feign 动态URL 解决记录

    Feign中使用动态URL请求 (应当是spring-cloud-starter-openfeign,不知道和一般的feign有何差别) 在spring项目下,假设有这样个Feign的消费接口,原来写 ...

  3. 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)

    1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...

  4. JVM学习——垃圾回收GC(学习过程)

    JVM学习-垃圾回收(GC) 2020年02月19日06:03:56,开始学习垃圾回收,学习资料来源(张龙老师的JVM课程) JVM内存数据区域知识复习 学习垃圾回收之前,要对JVM内部的内存区域有详 ...

  5. NSSCTF-[UTCTF 2020]Zero

    做misc嘛,先把题目一开始就给的一串英文翻译一下, 可以看到说明的是这个txt文档可能是包含其他的文本量,这个文本里面还有其他的东西,只是正常是看不到, 使用binwalk或者fomost分离尝试, ...

  6. [Unity] 在软件标题栏显示工作路径

    (一)问题 项目开发中常会有开多个分支,同时启动多个 Unity 程序的情况,来回切换的时候就容易混淆,有时候还需要用 Show In Explorer 或者其他标志来确认当前使用的是哪个分支. 于是 ...

  7. [旧][Android] View 工作原理(二)

    备注 原发表于2016.05.27,资料已过时,仅作备份,谨慎参考 前言 本文大量参照<Android 开发艺术探索>及参考资料的内容整合,主要帮助自己理清 View 的工作原理.深入学习 ...

  8. RENIX操作之XML报文模板说明——网络测试仪实操

    我们在网络测试仪的日常使用过程中,经常涉及到编辑和修改报文.利用RENIX软件,可以把当前编辑好的报文模板导出为XML文件,下次使用时可以直接导入使用,从而提升测试工作效率.本文以"添加UD ...

  9. Smartbi权限安全管理系统_保障数据权限安全

    思迈特软件Smartbi具有完善的安全管理体系,Smartbi权限安全管理系统它可以控制用户功能权限.数据访问权限.资源访问权限.Smartbi权限安全管理系统支持按用户.用户组.角色进行管理:支持多 ...

  10. 开源绘画应用 Pinta 已移植到GTK 3和.NET 6

    Pinta 是一款开源绘画应用,适用于 Linux.Windows 和 macOS.你可以用它来进行自由手绘/素描.你也可以用它在现有的图片上添加箭头.方框.文字等. 年初发布了 Pinta 2.0. ...