Simplify - How to Use Git Merge and Git Rebase Appropriately?

Simplify - How to Use Git Merge and Git Rebase Appropriately?

Daily short news for you
  • Wow, Windsurf has just updated its new policy for free accounts, everyone. The three most notable points are:

    • 25 credits per month to use premium models like gpt-4, sonet 3.5...
    • Unlimited use of the Write mode (similar to Cursor's Agents) with their homegrown Cascade Base model.
    • Unlimited code suggestions, especially with always fast speed (previously limited to slow speed).

    It's worth coming back, right everyone? 🤤

    » Read more
  • Deep Research Mini has started appearing in my free GPT account. This can be considered a simplified version of the Deep Research feature, which is only available in paid GPT accounts.

    I just tried a command: 'The coffee consumption situation in Vietnam in 2024', and it went off to find and compile documents for 34 minutes, producing a pretty neat report. This feature seems quite useful; I need to use it more to know for sure 🤤

    Oh, it's called mini because it uses the 4o-mini model, and currently, you can only use it 5 times a month 🫣

    » Read more
  • R1 hasn't passed, R2 has already arrived 😅

    Although it's just rumors for now, only Deepseek seems to be making a similar impact as OpenAI or Anthropic. What users care about are quality & price 😄

    Deepseek R2 will be releasing soon

    » Read more

Problem

After finishing a feature, we merge it into develop. The git merge command basically combines changes from one branch to another and can create an additional commit to mark the merge.

Recently, you have also heard about the git rebase command. Through research, you have found that it is also used to merge changes from one branch to another. The only difference is how git merge and git rebase create commit history.

However, you may have heard advice that if you are new, don't rush to use git rebase. While git merge is solving your problems well, continue using it. Honestly, I am not certain about using git rebase either, but I have a small tip that I would like to share with you in this article, hoping it will be helpful.

A Common Scenario

In practice, many projects have two basic branches: master and develop. The code in master is put into production while the code in develop runs in the development environment. (In reality, there may be more branches and it can be more complex, but I'm only using a basic example).

To develop a new feature, I check out from develop into a new branch and then start development on it. Branches can follow the rule feature/01, feature/02... with 01, 02... being the names of the features.

initial git tree

During development, there may be multiple features being developed concurrently and independently. What if you need changes from feature/01 to be incorporated into feature/02? Merge feature/01 into feature/02? Not bad! But if you continue doing that, your commit history may become messy. Instead, why not try this approach?

Principle: Always let develop serve as the intermediate branch between feature branches because ultimately the features have to be merged into develop. So, if you need changes from feature/01 into feature/02, merge feature/01 into develop, and then rebase feature/02 onto develop.

git tree after rebase

If you merge feature/01 directly into feature/02, the commit history will look like this:

git tree after merge

Doesn't the git tree look more complex compared to the initial state?

The Golden Rule of Rebase

The golden rule of rebase is clearly stated in the article Merging vs. Rebasing. I will summarize it: never use rebase on a shared branch. In the above example, the shared branch is develop. Why? If we rebase feature/01 or feature/02 onto develop, the commit history on develop will be "rewritten".

attempt to rebase feature onto develop

As shown above, when we rebase feature/01 onto develop (Main in the image), the new commits of develop are added after the latest commit of feature. At this point, the develop branch has a "rewritten" commit history. Just imagine if multiple people are working based on develop from prior to this point. Performing merge operations in the future will cause many confusing issues.

Conclusion

In summary, merge is always sufficient. You don't need to know about rebase, and it's okay. But if you want to have a cleaner git commit history, you can try rebase. The golden rule is to never rebase a feature onto develop (never rebase on a shared branch). Only rebase feature onto develop, and vice versa, merge develop into feature.

References:

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...