Git
Merge traits
Learn how git merge preserves the history.
Merge features and merge commit
The git merge command brings together the histories of two branches while preserving their independent timelines intact. It creates a special merge commit with two parents to join the diverged branches. This transparently documents when and from which branch a feature was merged.
git checkout main
git merge ozellik-dali
Existing commits are never altered or rewritten, making merge a safe and non-destructive operation.
Forcing a merge commit: --no-ff
Thanks to the merge commit, Git history clearly reflects branching and merging points. Using the --no-ff flag forces the creation of a merge commit even when a fast-forward is possible. This leaves a permanent record of when a feature was integrated into the main branch.
git merge --no-ff ozellik-dali
git log --graph --oneline
In graph view, you can trace branches diverging and converging back into the main trunk.
Exercise Join branches with a merge commit
Merge the ozellik branch using git merge -m "Merge ozellik" ozellik and print the latest commit subject with git log -1 --format="%s".
Show example solution
git init -q
git config user.name "Deniz"
git config user.email "deniz@example.com"
echo "a" > a.txt
git add a.txt
git commit -qm "Ana commit"
git checkout -q -b ozellik
echo "b" > b.txt
git add b.txt
git commit -qm "Ozellik commit"
git checkout -q main
echo "c" > c.txt
git add c.txt
git commit -qm "Ana dal guncelleme"
git merge -m "Merge ozellik" ozellik
git log -1 --format="%s"
Test yourself
-
How does git merge preserve history when joining two diverged branches?
- Preserves all history by creating a new merge commit with two parents
- Regenerates the hash values and timestamps of the commits
- Removes all past commits of the target branch from the working directory
- Compresses the branch by deleting all its commits into one
Show answer
Preserves all history by creating a new merge commit with two parents — git merge preserves history entirely by generating a special merge commit that has two parents.
-
Force merge commit creation
git merge ____ ozellik
Show answer
git merge --no-ff ozellik
-
Merge concepts
- git merge
- Merges branches preserving history
- Merge commit
- A commit with two parents
- --no-ff
- Forces a merge commit creation
- Tahribatsız işlem
- Never alters existing commits
Next lesson: What is rebase?