0
0
Gitdevops~5 mins

git merge command - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work on different features or fixes in separate branches, you need a way to combine those changes into one place. The git merge command helps you bring changes from one branch into another, making your project up to date with all work done.
When you finish a feature on a separate branch and want to add it to the main branch.
When you want to combine bug fixes from a hotfix branch back into the development branch.
When you want to update your current branch with the latest changes from another branch.
When you want to keep your branches synchronized after working separately.
When you want to prepare your code for release by merging all completed work.
Commands
Switch to the main branch where you want to merge changes.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the changes from 'feature-branch' into the current branch (main). This combines the work done on the feature branch into main.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
Check the status to confirm the merge was successful and there are no conflicts or uncommitted changes.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Key Concept

If you remember nothing else from this pattern, remember: git merge combines changes from one branch into another to keep your project up to date.

Common Mistakes
Trying to merge without switching to the target branch first.
Git merges changes into the current branch, so merging on the wrong branch will combine changes where you don't want them.
Always use 'git checkout' to switch to the branch you want to merge into before running 'git merge'.
Ignoring merge conflicts and not resolving them.
Merge conflicts stop the merge process and leave your project in an incomplete state until fixed.
When conflicts appear, open the conflicting files, fix the issues, then use 'git add' and 'git commit' to complete the merge.
Summary
Use 'git checkout' to switch to the branch where you want to add changes.
Run 'git merge branch-name' to combine changes from another branch.
Check 'git status' to confirm the merge succeeded and no conflicts remain.