Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Updating a branch with git rebase
Jessica Temporal
Jessica Temporal

Posted on • Originally published atjtemporal.com on

     

Updating a branch with git rebase

The commandgit rebase can be used to make various history adjustments, from rewriting the commit tree (and by doing that rewriting the history) to even pushing commits forward as if the branch were created in the future.

In this pro tip I will show you how to usegit rebase to update a branch.

Creating a example history

Suppose you have a project and you were working on a task on the branchtask-2, meanwhile someone else was working on the branchtask-1 that was merged intomain and thus making the branchmain more up-to-date as shown in the drawing below:

drawing showing the current history state

Similarly, the history graph should look like the image below:

image showing the main branch history

Knowing thatmain is more up-to-date than your branchtask-2 and following the good practice of always working with the most up-to-date project, you decide it’s time to update your working branch, which has the current history looking like this:

image showing the result of git log --graph command showing the branch task-2 with outdated history

Updating the current branch

There are a few ways to update the current branch, one of them is usinggit merge and merging the most updated branch, in this casemain, into the branch you want to update in this casetask-2. The other way is using the rebase that I’m going to show you now.

There are two ways of using rebase to make this update, let’s see the first one which isbeing on the branch you want to update , so you need to go to the branch to be updated, in this casetask-2 and userebase indicating the branch that is the source of changes:

git checkout task-2git rebase main
Enter fullscreen modeExit fullscreen mode

By running these commands you will see in your terminal the message saying that the update was done successfully“Successfully rebased and updated refs/heads/task-2.” as you can see in the following image:

imagem com o resultado dos comandos anteriores

The second way isindependent of the current branch , this format is a shortcut for the two previous commands, just use rebase, pass the source branch of the changes followed by the target branch:

git rebase main task-2
Enter fullscreen modeExit fullscreen mode

This command should also show the same message as the previous image. Regardless of the way you choose to use rebase, your history should look something like the drawing below:

desenho do histórico atualizado após o git rebase

And the history graph should look like this:

grafo de histórico após atualizar o ramo task-2 com o rebase

Wrapping up

Wrapping up, we have an important thing to point out now that our branch is updated: the rebase will only happen without interruptions, as shown in this pro tip blog post, if there are no conflicts, otherwise the rebase will be suspended and the conflicts must be resolved before continuing.

ow you know how to update a branch using rebase, if you want more details about the commandgit rebase, I recommendreading its documentation.

Below you can see the git study card that can help you remember the commandgit rebase to update a branch:


GitFicha #027: git rebase

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

DevRel 🥑 • Author • 🎙Podcaster Pizza De Dados • Creator of gitfichas.com • GitHub ⭐️ • cross-stitcher & knitter • 🇧🇷 & 🇨🇦 • she/her
  • Location
    Canada
  • Pronouns
    she/her
  • Work
    Auth0 by Okta
  • Joined

More fromJessica Temporal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp