Conflict adalah salah satu hal yang paling bikin panik developer baru. Tapi tenang — setelah kamu tahu cara membacanya, conflict jadi sesuatu yang biasa dan mudah diselesaikan.
Kapan Conflict Terjadi?
Conflict muncul ketika dua orang (atau dua branch) mengubah baris yang sama pada file yang sama, lalu di-merge. Git tidak bisa memilih mana yang "benar" secara otomatis, jadi dia memintamu untuk memutuskan.
Contoh skenario yang sering terjadi:
- Kamu dan rekan kerja sama-sama edit file
config.php di baris database host
- Kamu lupa
git pull sebelum mulai kerja, terus banyak perubahan yang bertabrakan
- Merge feature branch ke main setelah main sudah banyak update
Cara Membaca Marker Conflict
Saat conflict terjadi, Git otomatis menandai bagian yang bermasalah di dalam file dengan marker khusus:
<<<<<<< HEAD
$host = 'localhost'; ← versi kamu (branch aktif)
=======
$host = '192.168.1.10'; ← versi dari branch yang di-merge
>>>>>>> feature/update-config
Penjelasan setiap bagian:
| Marker |
Artinya |
<<<<<<< HEAD |
Awal conflict — isi dari branch kamu saat ini (HEAD) |
======= |
Pemisah antara kedua versi |
>>>>>>> nama-branch |
Akhir conflict — isi dari branch yang sedang di-merge |
Cara 1: Resolve Manual (Teks Editor)
Ini cara paling dasar. Buka file yang conflict, hapus marker Git, dan tinggalkan kode yang benar:
# Langkah demi langkah:
# 1. Lihat file mana yang conflict
git status
# 2. Buka file tersebut, edit secara manual
# Hapus marker dan pilih/gabungkan kode yang benar
# 3. Setelah selesai, tambah ke staging
git add namafile.php
# 4. Lanjutkan merge
git commit
# atau kalau rebase:
git rebase --continue
Contoh: setelah resolve, file akan terlihat seperti ini (bersih tanpa marker):
$host = '192.168.1.10'; // ← kita pilih versi ini, hapus sisanya
Cara 2: Resolve di VS Code
VS Code punya UI bawaan yang memudahkan resolve conflict. Saat buka file yang conflict, akan muncul tombol di atas setiap bagian:
- Accept Current Change — pakai versi kamu (HEAD)
- Accept Incoming Change — pakai versi yang di-merge
- Accept Both Changes — gabungkan keduanya
- Compare Changes — lihat diff side by side
Setelah klik salah satu, marker langsung hilang dan kamu tinggal save, git add, lalu git commit.
Cara 3: Pakai git mergetool
# Set tool yang dipakai
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Buka merge tool
git mergetool
Conflict Saat Rebase
# Saat rebase dan ada conflict:
# 1. Fix conflict di file yang bermasalah
# 2. Stage file yang sudah difix
git add namafile.php
# 3. Lanjutkan rebase
git rebase --continue
# Kalau mau batal total
git rebase --abort
Tips Mencegah Conflict
- Selalu
git pull sebelum mulai kerja — sync dulu dengan remote
- Buat branch kecil dan sering di-merge — jangan biarkan branch hidup terlalu lama
- Komunikasikan dengan tim file mana yang sedang dikerjakan
- Gunakan
git fetch + git rebase origin/main sebelum push untuk meminimalkan conflict
⚠️ Jangan Panik
Conflict tidak merusak apapun. Kamu selalu bisa abort dengan git merge --abort atau git rebase --abort untuk kembali ke kondisi sebelum merge/rebase. Take your time, baca marker dengan teliti, dan resolve satu per satu.
Conflict is one of the things that scares new developers the most. But calm down — once you know how to read it, conflict becomes something ordinary and easy to solve.
When Does Conflict Occur?
Conflict occurs when two people (or two branches) change the same line in the same file, then merge. Git can't automatically pick the "right" one, so it asks you to decide.
Examples of frequent scenarios:
- You and your colleague both edit the config.php file in the host database row
- You forget git pull before you start work, then a lot of changes collide
- Merge feature branch to main after playing has been updated a lot
How to Read Conflict Marker
When a conflict occurs, Git automatically marks the problematic section in the file with a special marker:
<<<<<<< HEAD
$host = 'localhost'; ← versi kamu (branch aktif)
=======
$host = '192.168.1.10'; ← versi dari branch yang di-merge
>>>>>>> feature/update-config
Explanation of each section:
| Marker |
Meaning |
| < < < < < < < HEAD |
The beginning of the conflict — the contents of your current branch (HEAD) |
| ======= |
Separator between the two versions |
| > > > > > > branch-name |
End of conflict — the contents of the branch that is being merged |
Method 1: Resolve Manual (Editor Text)
This is the most basic way. Open the conflicting file, remove the Git marker, and leave the correct code:
# Langkah demi langkah:
# 1. Lihat file mana yang conflict
git status
# 2. Buka file tersebut, edit secara manual
# Hapus marker dan pilih/gabungkan kode yang benar
# 3. Setelah selesai, tambah ke staging
git add namafile.php
# 4. Lanjutkan merge
git commit
# atau kalau rebase:
git rebase --continue
Example: after resolving, the file will look like this (clean without markers):
$host = '192.168.1.10'; // ← kita pilih versi ini, hapus sisanya
Method 2: Resolve in VS Code
VS Code has a built-in UI that makes it easy to resolve conflicts. When opening the conflicting files, a button will appear above each section:
- Accept Current Change — use your version (HEAD)
- Accept Incoming Change — use themerged version
- Accept Both Changes — combine them
- Compare Changes — see diff side by side
After clicking one, the marker immediately disappears and you just save, git add, then git commit.
Method 3: Use mergetool git
# Set tool yang dipakai
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Buka merge tool
git mergetool
Conflict During Rebase
# Saat rebase dan ada conflict:
# 1. Fix conflict di file yang bermasalah
# 2. Stage file yang sudah difix
git add namafile.php
# 3. Lanjutkan rebase
git rebase --continue
# Kalau mau batal total
git rebase --abort
Tips to Prevent Conflict
- Always git pull before starting work — sync with remote
- Make branches small and often merged - don't let branches live too long
- Communicate with the team which files are being worked on
- Use git fetch + git rebase origin/main before push to minimize conflict
⚠️ Jangan Panik
Conflict tidak merusak apapun. Kamu selalu bisa abort dengan git merge --abort atau git rebase --abort untuk kembali ke kondisi sebelum merge/rebase. Take your time, baca marker dengan teliti, dan resolve satu per satu.
Siap Mulai Karir IT-mu?
Bergabung dengan ribuan alumni DebugGo yang sudah berhasil masuk industri teknologi.
Lihat Kelas DebugGo
Komentar 0