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. WEB前端开发--1(Web前端开发综述)

    Web前端开发 Web--Web系统      前端--网页上为用户呈现的部分   开发--编写代码 1. 前端与后端 前端:网页上为用户呈现的部分 后端:与数据库进行交互,完成数据存取 2. 网站与 ...

  2. CSS3自定义滚动条样式-webkit内核

    自定义滚动条实现 此部分针对webkit内核的浏览器,使用伪类来改变滚动条的默认样式,详情如下: 滚动条组成部分 1. ::-webkit-scrollbar 滚动条整体部分 2. ::-webkit ...

  3. 剑指Offer系列_09_用两个栈实现队列

    package leetcode.sword_to_offfer.day01; import java.util.LinkedList; /** * 用两个栈实现一个队列.队列的声明如下,请实现它的两 ...

  4. python语法_1基础语法概述

    http://www.runoob.com/python3 章节:教程.基础语法.数据类型.解释器.注释.运算符. 大纲 查看python版本 实现第一个python3.x程序,hello world ...

  5. 3、网络并发编程--udp代码、操作系统发展史、多道技术、进程理论

    昨日内容回顾 socket基本使用 # 内置的模块 import socket s = socket.socket() # 默认是TCP协议 也可以切换为UDP协议 s.bind((ip,port)) ...

  6. tomcat安装笔记

    安装Tomcat 1.下载安装包.上传服务器.解压. 官网下载地址Apache Tomcat - Apache Tomcat 8 软件下载 [root@test /]# mkdir /root/tom ...

  7. CentOS8系统上Zookeeper安装启动

    下载Zookeeper安装包,下载地址为:https://zookeeper.apache.org/releases.html.注意选择bin的gz包,否则安装不成功,提示找不到相应的类. 一.单机模 ...

  8. RFC2544吞吐量测试详细步骤-信而泰Renix软件操作演示

    关键词:RFC1242:RFC2544:吞吐量:吞吐率. 吞吐量概述:吞吐量即吞吐率,这个词首先在RFC1242中被提出,是评估网络设备性能的首要指标,其定义是在设备没有丢帧的情况下的最大的转发速率, ...

  9. Maven-compiler-plugin 插件编译过程中报缺失get/set方法

    一般都是由于使用了lombok插件导致的 解决方法: 首先将compiler插件版本升级到3.3以上, 查询资料发现说有可能是版本太低导致 在configuration中添加上annotationPr ...

  10. idea教程--面板介绍

    面板说明