0
0
Gitdevops~5 mins

Merge commit creation in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work on different features or fixes in separate branches, you need to combine them into one. A merge commit is a special commit that joins these branches together, keeping the history clear and showing where the branches merged.
When you finish a feature in 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 main code.
When multiple team members work on different branches and you need to unify their work.
When you want to keep a clear record of when and how branches were combined.
When resolving conflicts between branches before finalizing the combined code.
Commands
Switch to the main branch where you want to merge changes.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the 'feature-branch' into the current branch, creating a merge commit if needed.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
Show the latest commit with a graph to confirm the merge commit was created.
Terminal
git log --oneline --graph --decorate -1
Expected OutputExpected
* 5d6e7f8 (HEAD -> main, feature-branch) Add new feature to file.txt
--oneline - Show commits in a short, one-line format
--graph - Display commit history as a graph
--decorate - Show branch and tag names
Key Concept

If you remember nothing else from this pattern, remember: a merge commit joins two branches and keeps a clear history of their combination.

Common Mistakes
Trying to merge without switching to the target branch first.
Git merges into the current branch, so merging on the wrong branch combines changes where you don't want them.
Always run 'git checkout' to switch to the branch you want to merge into before running 'git merge'.
Using 'git merge' when the branches have no new commits, expecting a merge commit to be created.
Git will do a fast-forward merge without creating a merge commit if possible, so no merge commit appears.
Use 'git merge --no-ff feature-branch' to force a merge commit even if fast-forward is possible.
Summary
Switch to the branch where you want to combine changes using 'git checkout'.
Run 'git merge' with the branch name to join changes and create a merge commit.
Use 'git log --oneline --graph --decorate -1' to verify the merge commit and see the history.