Git Merge and Rebase Commands Guide

Merge and Rebase

git merge - Merge Branches

Integrates changes from the specified branch into the current branch.

Option Description
--no-ff Always create a merge commit (no fast-forward)
--ff-only Merge only if fast-forward is possible
--squash Combine all changes into a single commit
--abort Abort the merge when conflicts occur
--continue Continue the merge after resolving conflicts
-m <message> Specify the commit message for the merge

Examples:

git merge feature - Merge the feature branch into the current branch
git merge --no-ff feature - Create a merge commit without fast-forwarding
git merge --ff-only origin/main - Merge only if fast-forward is possible
git merge --squash feature - Combine all changes from the feature branch into a single commit
git merge --abort - Abort the merge when conflicts occur
git merge -m "Merge feature branch" feature - Merge with a custom commit message

git rebase - Reconstruct Commit History

Changes the base of a branch and reorganizes the commit history.

Option/Argument Description
<branch-name> Rebase the current branch onto the specified branch
-i, --interactive Interactive rebase (allows editing, reordering, and combining commits)
--onto <new-base> <old-base> <branch> Move a range of commits to a new base
--continue Continue the rebase after resolving conflicts
--abort Abort the rebase and return to the original state
--skip Skip the current commit and continue the rebase

Examples:

git rebase main - Rebase the current branch onto the tip of the main branch
git rebase -i HEAD~3 - Interactively edit the last 3 commits
git rebase --onto main feature-base feature - Move commits from feature-base to feature onto the tip of main branch
git rebase --continue - Continue the rebase after resolving conflicts
git rebase --abort - Abort the rebase and return to the original state

Note: Rebasing rewrites commit history, which can cause problems if done on changes that have already been pushed. Avoid rebasing on shared branches and use it only on your personal branches.

git cherry-pick - Apply Specific Commits

Selects and applies specific commits from other branches to the current branch.

Option/Argument Description
<commit-hash> Hash value of the commit to apply
-n, --no-commit Stage changes only without committing
-e, --edit Edit the commit message
-x Add the original commit hash to the commit message
-s, --signoff Add a Signed-off-by line to the commit message
--continue Continue cherry-pick after resolving conflicts
--abort Abort cherry-pick and return to the original state
--skip Skip the current commit and continue cherry-pick

Examples:

git cherry-pick abc123 - Apply commit abc123 to the current branch
git cherry-pick abc123 def456 - Apply multiple commits in sequence
git cherry-pick abc123..def456 - Apply a range of commits (excluding abc123)
git cherry-pick abc123^..def456 - Apply a range of commits (including abc123)
git cherry-pick -n abc123 - Add changes to the staging area without committing
git cherry-pick --continue - Continue cherry-pick after resolving conflicts
git cherry-pick --abort - Abort cherry-pick

Note: Cherry-picking is useful when you want to include only specific commits from other branches, but it can make future merges more complex because the same changes will exist in multiple places.

Cherry-Pick Best Practices Between Develop and Staging Environments

The following shows best practices in script format for selectively applying specific features or fixes from the develop environment to the staging environment.

# 1. Update to the latest state
git checkout develop
git pull origin develop    # Update develop environment to the latest

git checkout staging
git pull origin staging    # Update staging environment to the latest

# 2. Identify commits to apply from the develop branch
git checkout develop
git log --oneline -n 10    # Display the last 10 commits to identify which ones to apply

# 3. Note the commit IDs (example: abc123, def456)

# 4. Switch to the staging branch and execute cherry-pick
git checkout staging

# For applying a single commit
git cherry-pick abc123

# For applying multiple commits (in sequence)
git cherry-pick abc123 def456

# For applying a range of commits
git cherry-pick abc123^..def456    # Including abc123
# or
git cherry-pick abc123..def456     # Excluding abc123

# 5. Handling conflicts
# If conflicts occur, edit files to resolve the conflicts
git add .                  # Stage resolved files
git cherry-pick --continue # Continue cherry-pick

# If you want to abort midway
# git cherry-pick --abort

# 6. Push to remote repository
git push origin staging    # Push changes to the staging branch
            

Tips:

  • It's important to update both environments (develop and staging) to the latest state before cherry-picking.
  • Use the -x option to include the original commit hash in the commit message (example: git cherry-pick -x abc123).
  • When cherry-picking multiple commits, apply them in the correct order considering their dependencies.
  • Consider merging branches instead of cherry-picking when migrating entire large features.
  • Keep track of which environment cherry-picked commits came from to make tracking easier.
  • Regularly merge from develop to staging to minimize differences between environments.