Logo

dev-resources.site

for different kinds of informations.

Git update-refs in a nutshell

Published at
5/23/2024
Categories
git
rebase
versioncontrol
Author
ibethus
Categories
3 categories in total
git
open
rebase
open
versioncontrol
open
Author
7 person written this
ibethus
open
Git update-refs in a nutshell

Original article on my blog (🇫🇷)

When working on a versioned project, it is relatively common to end up with a "stack" of branches. What could be more frustrating than having to rebase all the other branches on top of the first one when updating it? This process becomes even more tedious when the number of branches is high or when they are regularly modified.

Git version 2.381 introduces a solution to this problem: the update-refs rebase option.

Stack?

Branch stacking is a way to break down important features, allowing for smaller pull requests. In the example below, three branches dependent on each other have been created, allowing "my feature" to be divided into three distinct blocks.

The assumption here is that when each of these branches is individually merged into the main branch, the code will still be functional, as it has been appropriately divided.

initial state

Let's say a small typo was made in the commit of the my-feature-v1 branch. A second commit needs to be added to fix the issue, resulting in the following history:

after commit

In this situation, the only solution to update my-feature-v2 and my-feature-v3 is to checkout and rebase v2 on v1, then v3 on v2.

This cumbersome process is remedied by the update-refs rebase option.

The solution

update-refs

The goal of this option is to automatically update the n branches on which the branch being rebased depends.

Thus, in our case, we can simply checkout our my-feature-v3 branch and run the command git rebase my-feature-v1 --update-refs. With this option, Git updates not only the v3 branch but also the v2 branch to preserve the history. The result of this operation is as follows:

after rebase update-refs

In practice:

Image description

Updating the Remote Repository

Even if the branches being manipulated have already been pushed to the remote repository, it is possible to update all of them in a single command: git push --force-with-lease origin my-feature-v1 my-feature-v2 my-feature-v3

Here, a specific force push is used to preserve the remote history2.

Global Activation

Why not always use the update-refs option during a rebase? The Git team asked themselves the same question and decided to include this possibility in the configuration. Thus, git config --global rebase.updateRefs true activates the option globally for each of your rebases.

In Summary

  • git rebase {my-branch} --update-refs updates a branch and all its dependencies
  • git push --force-with-lease origin my-branch-1 my-branch-2 my-branch-n pushes the update of these branches to the remote repository

Happy rebasing!


  1. The release note is available here 

  2. See the use case 

rebase Article's
30 articles in total
Favicon
Understanding Git Rebase Merge: Chronological vs Logical Order and Commit History
Favicon
A Study on Git Rebase
Favicon
Git Rebase and Code Refactoring for VShell Tool
Favicon
Git Merge vs Rebase
Favicon
Como atualizar um repositório 'forkado' com git rebase
Favicon
Why I prefer rebase over merge (and everything else)
Favicon
Step-by-Step Guide to Rebasing Your Branch onto Dev
Favicon
Git update-refs in a nutshell
Favicon
A Guide to Understanding the Nuances of Git Merge and Rebase
Favicon
Git Merge vs. Rebase: Key Differences
Favicon
Rebasing a Branch Created from Another Branch to Main After Squash Merge: A Step-by-Step Guide
Favicon
A Hands-on Guide to Git Rebase & Resolving Conflicts
Favicon
Refactoring and Rebasing
Favicon
Everything you need to know about git rebase
Favicon
git rebase and git revert to fix the broken application.
Favicon
A clean Git history with Git Rebase and Conventional Commits
Favicon
How to use Git rebase to alter previous commits in the project history
Favicon
Git remove file from not last commit
Favicon
Fixing the branch source with git rebase
Favicon
Project Management: Static Analysis Tools
Favicon
Interactive rebase
Favicon
OSD600 - Refactoring My SSG
Favicon
จะทำยังไงดีน้าาาา.. อยากแก้คำผิดใน commit message ที่ผ่านๆ มา
Favicon
GitHub Workflow, Merge and Rebase
Favicon
Rebase and Merge Don't Mix
Favicon
Professional Version Control with Git: Pt 3 – Rebase and Bisect
Favicon
Git Merge vs Rebase
Favicon
Git Rebase and Interactive Rebase
Favicon
Atomic commits will help you git legit.
Favicon
NOTES: Merge or Rebase

Featured ones: