Skip to content

Latest commit

 

History

History
62 lines (39 loc) · 2.67 KB

File metadata and controls

62 lines (39 loc) · 2.67 KB

Version Control with Git: Git Rebase, Squash, and Cherry-Pick

Git Rebase

Rebasing is a process in Git that is used to apply changes from one branch onto another, or to alter the commit history of a branch.

Key Concepts
  1. Changing Base: Rebase takes the changes made in a branch and reapplies them on another base tip.
  2. Linear History: It creates a linear project history, which can be easier to follow than the merge commits created by git merge.
  3. Interactive Rebase: Allows you to edit, remove, or combine commits in a branch.
Example Use
git checkout feature-branch
git rebase master

Git Squash

Squashing is the process of combining multiple commits into a single commit. It's often used to clean up a messy history before merging a feature branch into the main branch.

Key Concepts
  1. Simplify History: Squashing can make the commit history cleaner and more understandable.
  2. Interactive Rebase: Squashing is typically done using interactive rebase.
Example Use
git rebase -i HEAD~3  # Squash the last 3 commits

In the interactive mode, you can then mark commits to squash into a single commit.

Git Cherry-Pick

Cherry-picking in Git allows you to pick a specific commit from one branch and apply it onto another branch. This is useful for applying changes without merging a whole branch.

Key Concepts
  1. Selective Committing: Cherry-picking enables you to select specific commits to apply.
  2. Avoid Duplication: It helps in avoiding duplicate commits in feature branches.
  3. Conflict Resolution: Similar to merging, cherry-picking can lead to conflicts that need to be resolved manually.
Example Use
git checkout branch-name
git cherry-pick commit-hash

Best Practices and Considerations

  • Rebase Carefully: Avoid rebasing commits that are already pushed to a shared repository as it can rewrite history and create inconsistencies for other collaborators.
  • Commit Granularity: When planning to use squash or cherry-pick, structure your commits such that each one represents a complete and independent change.
  • Resolving Conflicts: Be prepared to resolve conflicts during rebase or cherry-pick, especially when working on branches that diverge significantly.

Conclusion

Git rebase, squash, and cherry-pick are advanced Git features that provide powerful ways to manipulate commit history, streamline changes, and selectively apply changes across branches. While offering great control over the project history and commit organization, they should be used judiciously and with an understanding of their implications, especially in collaborative environments.