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., 
mainormaster). 
 - 
Commands:
- Interactive Rebase:
 
git rebase -i HEAD~nAllows you to rewrite commit history for the last
ncommits (e.g., squashing, editing messages).- Rebase a Feature Branch:
 
git checkout feature-branch git rebase mainMoves the
feature-branchcommits on top ofmain. - 
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
- Initialize a Repository:
 
   mkdir git-advanced-techniques
   cd git-advanced-techniques
   git init
- 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"
- Create a Feature Branch:
 
   git checkout -b feature-branch
   echo "New feature content" >> fileA.txt
   git commit -am "Add new feature to fileA"
- Switch Back to Main Branch:
 
   git checkout main
2. Practice Git Rebase
- 
Update
feature-branchwith Changes frommain:- Add a new change to 
main: 
echo "Update on main branch" >> fileB.txt git commit -am "Update fileB on main branch" - Add a new change to 
 
- 
Rebase
feature-branchontomain:
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 
- 
Perform an Interactive Rebase:
- Edit commit history on 
feature-branch: 
git rebase -i HEAD~2 - Edit commit history on 
 
- Choose actions like squash or edit during the rebase process.
 
3. Practice Git Cherry-Pick
- Create a New Branch for Cherry-Picking:
 
   git checkout -b hotfix-branch
- 
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> 
- 
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 --graphto visualize your commit history after rebasing or cherry-picking: 
  git log --oneline --graph --all
Scenario Application
If you're working on a larger project:
- Use rebase to update your feature branch with the latest changes from 
mainto avoid merge clutter. - Use cherry-pick to apply bug fixes to both the development and production branches without merging unrelated commits.
 
Happy Learning !!!
    
Top comments (0)