Merge traits
- Git course
- Intermediate
- Lesson 9/82
- 4 min
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-daliExisting 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 --onelineIn graph view, you can trace branches diverging and converging back into the main trunk.
Practice 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".
Starter code
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"
# ozellik dalini merge et ve son commit basligini yazdirSample 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"Expected output
Merge ozellik
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How does git merge preserve history when joining two diverged branches?
- Preserves all history by creating a new merge commit with two parents Correct answer
- 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
Explanation: git merge preserves history entirely by generating a special merge commit that has two parents.
2 Force merge commit creation
git merge ____ ozellik
git merge --no-ff ozellik
Explanation: The --no-ff flag forces Git to create an explicit merge commit even when fast-forward is possible.
3 Merge concepts
| Term | Meaning |
|---|---|
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 |