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 branchgit merge --no-ff feature
- Create a merge commit without fast-forwardinggit merge --ff-only origin/main
- Merge only if fast-forward is possiblegit merge --squash feature
- Combine all changes from the feature branch into a single commitgit merge --abort
- Abort the merge when conflicts occurgit merge -m "Merge feature branch" feature
- Merge with a custom commit message
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 branchgit rebase -i HEAD~3
- Interactively edit the last 3 commitsgit rebase --onto main feature-base feature
- Move commits from feature-base to feature onto the tip of main branchgit rebase --continue
- Continue the rebase after resolving conflictsgit 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.
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 branchgit cherry-pick abc123 def456
- Apply multiple commits in sequencegit 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 committinggit cherry-pick --continue
- Continue cherry-pick after resolving conflictsgit 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.
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:
-x
option to include the original commit hash in the commit message (example: git cherry-pick -x abc123
).