Merging
Merging means combining the changes from one branch into another. It's a fundamental operation in Git that allows
you to integrate different lines of development and bring work from feature branches back into the main codebase.
Use Cases:
- Integrating completed features into the main branch
- Combining bug fixes with the current codebase
- Synchronizing work from multiple developers
- Bringing experimental changes into production code
- Consolidating multiple commits with squash merge
Examples:
How to Merge
-
Switch to the branch you want to merge into (usually main):git switch main
-
Update your branch with the latest changes before merging:git pullYou should ensure that the branch you switched to is updated with the latest changes from the repo before merging.
-
Merge a branch into your current branch:git merge [target branch]This merges the "demo1" branch into your current branch.
Squash Merging
Squash merge is used if you want all changes from a specific branch to your current branch in a single
commit.
How to Squash Merge
- Create a new branch and switch over.
- Make changes such as change text, add new files, etc.
- Commit those changes.
- Switch back to your main branch.
-
Use git command:
git merge --squash [target branch]
You may notice that this DID NOT merge together. It simply copied all the changes made in your new
branch and pasted it in your main branch and then you can manually commit those changes in the main branch.
This keeps the project history clean and easy to read.
Abort Merging
Aborting a merge will cancel a merge that is currently in progress and returns the repo to the
state it was before the merge started.
git merge --abort
This is handy if you have any issues during a merge like a conflict that you don't want to resolve.