Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Merge Conflicts | Arbeide med Grener i Git
Git-Essensielt

Merge Conflicts

Sveip for å vise menyen

Understanding Merge Conflicts

A merge conflict occurs when Git cannot automatically reconcile the changes made on two divergent branches. This typically happens when changes are made to the same lines of code in the same files in both branches since their last common ancestor.

Example Workflow

First, list all branches in the repository:

git branch
Listing all branches

You are currently on the master branch, so modify the branch_learning.txt file by adding a new line and directly commit this change:

echo "New line from the master branch" >> branch_learning.txt
git commit -a -m "Add a new line specifying the master branch to branch_learning.txt"
Modifying file on the master branch

Now switch to the feature branch, add a different new line to the branch_learning.txt file, and directly commit this change:

git checkout feature/new-feature
echo "New line from the feature branch" >> branch_learning.txt
git commit -a -m "Add a new line specifying the feature branch to branch_learning.txt"
Modifying file from the feature branch

Both commits are successful, so switch back to the master branch and merge the feature branch with it:

git checkout master
git merge feature/new-feature
Merge attempt

Oops, a merge conflict occurred because the same line in the same file differs between branches, and Git cannot automatically resolve it.

Merge conflict

Let's also run the git status command to get more information about the conflict:

git status
Checking status

This message indicates that you must either abort the merge or fix the conflicts and commit the changes afterward. You’ll proceed with the latter option and resolve the conflicts in the next chapter.

question mark

What causes a merge conflict in Git?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 4. Kapittel 5
some-alt