DEV Community

Vivesh
Vivesh

Posted on

1

Rebasing and Cherry-Picking

Advanced Git Techniques: Rebasing and Cherry-Picking

1. Git Rebase

Git rebase is used to integrate changes from one branch into another while maintaining a cleaner history.

  • Key Concepts:

    • Moves or replays commits from one branch onto another.
    • Often used to keep a feature branch updated with changes from the main branch (e.g., main or master).
  • Commands:

    • Interactive Rebase:
    git rebase -i HEAD~n
    

    Allows you to rewrite commit history for the last n commits (e.g., squashing, editing messages).

    • Rebase a Feature Branch:
    git checkout feature-branch
    git rebase main
    

    Moves the feature-branch commits on top of main.

  • Best Practices:

    • Avoid rebasing shared branches to prevent conflicts.
    • Use interactive rebases to clean up commit history before merging.

2. Git Cherry-Pick

Git cherry-pick applies specific commits from one branch to another.

  • Key Concepts:

    • Useful when you want to bring in a particular change without merging the entire branch.
    • Can apply one or multiple commits.
  • Commands:

    • Apply a Single Commit:
    git cherry-pick <commit-hash>
    
    • Apply Multiple Commits:
    git cherry-pick <commit-hash1> <commit-hash2>
    
    • Handle Conflicts During Cherry-Pick: If conflicts occur, resolve them, then use:
    git cherry-pick --continue
    
  • Use Cases:

    • Backporting a bug fix to an older release branch.
    • Applying specific changes to multiple branches without merging.

Comparison

Technique Purpose Usage Scenario
Rebase Reorganizes commit history for clarity. Updating a feature branch with main branch.
Cherry-Pick Applies specific commits to another branch. Backporting bug fixes or applying hotfixes.

Task: Practice Advanced Git Techniques on Your Git Repository

1. Setup a Sample Repository

  1. Initialize a Repository:
   mkdir git-advanced-techniques
   cd git-advanced-techniques
   git init
Enter fullscreen mode Exit fullscreen mode
  1. Create and Commit Changes:
   echo "File A, initial content" > fileA.txt
   git add fileA.txt
   git commit -m "Initial commit for fileA"

   echo "File B, initial content" > fileB.txt
   git add fileB.txt
   git commit -m "Initial commit for fileB"
Enter fullscreen mode Exit fullscreen mode
  1. Create a Feature Branch:
   git checkout -b feature-branch
   echo "New feature content" >> fileA.txt
   git commit -am "Add new feature to fileA"
Enter fullscreen mode Exit fullscreen mode
  1. Switch Back to Main Branch:
   git checkout main
Enter fullscreen mode Exit fullscreen mode

2. Practice Git Rebase

  1. Update feature-branch with Changes from main:

    • Add a new change to main:
     echo "Update on main branch" >> fileB.txt
     git commit -am "Update fileB on main branch"
    
  • Rebase feature-branch onto main:

     git checkout feature-branch
     git rebase main
    
  • If there are conflicts, resolve them and continue:

     git status
     # Edit conflicting files
     git add <conflicting-files>
     git rebase --continue
    
  1. Perform an Interactive Rebase:

    • Edit commit history on feature-branch:
     git rebase -i HEAD~2
    
  • Choose actions like squash or edit during the rebase process.

3. Practice Git Cherry-Pick

  1. Create a New Branch for Cherry-Picking:
   git checkout -b hotfix-branch
Enter fullscreen mode Exit fullscreen mode
  1. Apply a Specific Commit:

    • Identify the commit hash from the log:
     git log --oneline
    
  • Cherry-pick a specific commit from feature-branch:

     git cherry-pick <commit-hash>
    
  1. Handle Conflicts (if any):

    • Resolve conflicts and continue:
     git status
     # Fix conflicts
     git add <conflicting-files>
     git cherry-pick --continue
    

4. Verify the Results

  • Use git log --oneline --graph to visualize your commit history after rebasing or cherry-picking:
  git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

Scenario Application

If you're working on a larger project:

  • Use rebase to update your feature branch with the latest changes from main to avoid merge clutter.
  • Use cherry-pick to apply bug fixes to both the development and production branches without merging unrelated commits.

Happy Learning !!!

Please leave your appreciation by commenting on this post!

Everyone is welcome

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay