Keep Only One Commit Record When Merging Branches in Git
By default, Git retains all commit records when merging branches, including commits from the merged branch. However, in some cases, you may only want to keep a single commit record that encapsulates all the changes. This article explores various methods to achieve this.
1. Use the git merge --squash
Command
The git merge --squash
command combines all changes from the feature branch into a single commit on the target branch. This makes the history cleaner and more concise.
Example:
# Switch to the main branch
git checkout main
# Merge branches and squash commits
git merge --squash feature_branch
# Create a new commit
git commit -m "Merge feature_branch"
Note: Squashing the merge retains only one commit record, but the detailed history of the feature branch will be lost.
2. Use the git rebase
Command
The git rebase
command replays commits from one branch onto another. Using git rebase -i
(interactive mode), you can reorder, combine, or delete commit records.
Example:
# Switch to the main branch
git checkout main
# Interactively rebase commits
git rebase -i feature_branch
After running the command, a text editor opens showing a list of commits. You can mark unnecessary commits for deletion or squash them into a single commit. Once you save and close the editor, Git applies the changes.
3. Use the git cherry-pick
Command
The git cherry-pick
command allows you to apply specific commits from one branch to another. This is useful when you want to manually select a single commit to include in the main branch.
Example:
# Switch to the main branch
git checkout main
# Cherry-pick a specific commit
git cherry-pick <commit-hash>
Provide the hash of the desired commit. This method keeps the history cleaner by avoiding the need to merge all commits.
4. Use the git reset
Command
The git reset
command rewinds the branch to a specific commit and discards subsequent commits. This approach can clean up the history and leave only one desired commit.
Example:
# Switch to the feature branch
git checkout feature_branch
# Identify the commit hash to reset to
git log
# Reset to the desired commit and discard subsequent changes
git reset --hard <commit-hash>
Caution: This method is destructive. Ensure you’ve backed up any commits you want to keep.
5. Use the git revert
Command
The git revert
command creates a new commit that undoes the changes from a specific range of commits. This is ideal for situations where you want to undo merges or commits without rewriting history.
Example:
# Switch to the main branch
git checkout main
# Revert specific commits
git revert <start-commit>..<end-commit>
Provide the commit range you want to revert. This will create a new commit to apply the undo changes, leaving only the desired record.
Summary
There are several methods to retain only a single commit record during or after a merge:
git merge --squash
: Compress all changes into one commit.git rebase
: Interactively edit commit history.git cherry-pick
: Selectively apply commits.git reset
: Discard commits after a specific point.git revert
: Undo changes with a new commit.
Choose the method that best suits your workflow and project requirements.