Coder Social home page Coder Social logo

git-rebase-workshop's Introduction

git-rebase-workshop

Background

During the development process the code changes relate to each feature are being committed to separate feature branch. Due to the fact that the team works on several features in parallel in most cases the default branch's head commit changes since a feature branch created until it is merged. So, the developer that wants to merge their changes into the default branch first should update the feature branch. There are two main options to do it: merge and rebase.

Initial state

Initial state

After Merge

Merge

After Rebase

Merge

Merging feature branches to the default branch

Merging feature branches to the default branch

Rebasing feature branch on Github

Rebasing feature branch on Github

Rebase Props and Cons

  • Prop: The PR is tidier and contains only relevant to the work on the feature commits.
  • Cons: The conflicts resolution process is more challenging.

Note: Using merge we need to solve conflicts only once. For rebase we have to do it almost for each commit. If we'd have multiple commits in the branch the rebase could be very hard and painful process. The solution is to keep the branch lean.

Workshop

Simple case

  1. Create folder for workshop and initialize git

    mkdir workshop
    cd workshop
    git init
    git commit --allow-empty -m "First commit"
    git commit --allow-empty -m "Second commit"
    git commit --allow-empty -m "Third commit"
    git --no-pager log --format=oneline
  2. Create the big feature branch with one commit

    git checkout -b my-big-feature
    touch big-feature-file.txt
    echo Hello > big-feature-file.txt
    git add big-feature-file.txt
    git commit -m "feat: the first big feature commit"
  3. Return to the main branch

    git checkout -
  4. Create the small feature branch with two commits

    git checkout -b my-small-feature
    touch small-feature-file.txt
    echo "Hello" > small-feature-file.txt
    git add small-feature-file.txt
    git commit -m "feat: the small feature first commit"
    echo "World" >> small-feature-file.txt
    git add small-feature-file.txt
    git commit -m "feat: the small feature second commit"
    git --no-pager log --format=oneline
  5. Merge it into main branch

    git checkout main
    git merge --squash my-small-feature
    git commit -m "feat: the small feature"
    git branch -D my-small-feature
    git --no-pager log --format=oneline
  6. Find last commit that is common for the feature and the default branch

    git checkout my-big-feature
    echo "Last common commit: " `git merge-base my-big-feature main`
    git --no-pager log --format=oneline
  7. Rebase the big feature branch on main and see that is the last common commit now

    git rebase main
    echo "Last common commit: " `git merge-base my-big-feature main`
    git --no-pager log --format=oneline
  8. Return to the default branch and delete the my-big-feature one

    git checkout -
    git branch -D my-big-feature

Now the last default branch's commit is the last common commit for the feature and the default branches.


Keeping the branch lean

  1. Create new branch

    git checkout -b lean-branch
  2. Add the first commit

    touch code.txt
    echo "My feature code" > code.txt
    git add code.txt
    git commit -m "feat: cool feature"
  3. Add the second commit

    touch tests.txt
    echo "My feature tests" > tests.txt
    git add tests.txt
    git commit -m "tests: cool feature"
    git --no-pager log --format=oneline
  4. OMG! You've run tests and figured out that there is a bug. Fix it!

    echo "My feature fixed code" > code.txt
  5. Insert the fix into the first commit so nobody will know we had bugs.

    git add code.txt
    git commit --fixup HEAD~1
    git --no-pager log --format=oneline

Note: You can use the commit sha instead of HEAD~X.

  1. Now we need to rebase commits.

    git rebase -i --autosquash HEAD~3

Press ESCAPE and :q! in the vim editor.

  1. What do we have now?

    git --no-pager log --format=oneline

Note: If you pushed the changes to the upstream after rebase you have to use git push --force to replace fixed commits.

Conclusion

Rebase helps to keep the branch both up-to-date and clean, w/o any extraneous commits. Using interactive rebase we can fixup every commit in our branch even if there are others on top of it. We can rename and drop commits and even to change the order.


Choose REBASE!
Keep your PR tidy!

git-rebase-workshop's People

Contributors

alef83 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.