Difference between revisions of "Solving conflicts on GitLab"

From mi-linux
Jump to navigationJump to search
(Created page with "Main Page >> GitLab server >> Solving conflicts on GitLab")
 
Line 1: Line 1:
 
[[Main Page]] >> [[GitLab server]] >> Solving conflicts on GitLab
 
[[Main Page]] >> [[GitLab server]] >> Solving conflicts on GitLab
 +
 +
== Solving conflicts ==
 +
 +
But what happens if 2 developers push a change to the same file? Well, let's take a look.
 +
 +
Let's imagine that our previously created file "page2.html" contains the following, very simple HTML code:
 +
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
  <head>
 +
    <meta charset="UTF-8">
 +
    <title>My second page</title>
 +
  </head>
 +
  <body>
 +
    <h1>This is our second page</h1>
 +
  </body>
 +
</html>
 +
</pre>
 +
 +
=== Two developers make a change ===
 +
 +
Let's imagine that 2 developers "pull" the latest code above, and then both make a slightly different change, as follow (see added paragraph):
 +
 +
Developer 1:
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
  <head>
 +
    <meta charset="UTF-8">
 +
    <title>My second page</title>
 +
  </head>
 +
  <body>
 +
    <h1>This is our second page</h1>
 +
    <p>Some text about something</p>
 +
  </body>
 +
</html>
 +
</pre>
 +
 +
Developer 2:
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
  <head>
 +
    <meta charset="UTF-8">
 +
    <title>My second page</title>
 +
  </head>
 +
  <body>
 +
    <h1>This is our second page</h1>
 +
    <p>Some other text about something else!</p>
 +
  </body>
 +
</html>
 +
</pre>
 +
 +
=== Two developers push their changes ===
 +
 +
Developer 1 commits his change successfully:
 +
<pre>
 +
git add page2.html
 +
git commit -m "commit by team member 1"
 +
git push -u origin master
 +
</pre>
 +
 +
Message displayed:
 +
<pre>
 +
1 file changed, 1 insertion(+)
 +
</pre>
 +
 +
Then developer 2 tries to commit too:
 +
<pre>
 +
git add page2.html
 +
git commit -m "commit by team member 2"
 +
git push -u origin master
 +
</pre>
 +
 +
But he gets an error message:
 +
<pre>
 +
error: failed to push some refs to 'https://fsegitlab.wlv.ac.uk/in9352/great-work-by-alix.git'
 +
To prevent you from losing history, non-fast-forward updates were rejected
 +
Merge the remote changes (e.g. 'git pull') before pushing again.
 +
</pre>
 +
 +
The Git server tells developer 2 to "pull" the latest code again (which will include the change made by developer 1). Let's do that.
 +
 +
=== Resolving the conflict ===
 +
 +
Let's pull the code again:
 +
 +
<pre>
 +
git pull origin
 +
</pre>
 +
 +
This time a warning message tells us:
 +
 +
<pre>
 +
CONFLICT (content): Merge conflict in page2.html
 +
Automatic merge failed; fix conflicts and then commit the result.
 +
</pre>
 +
 +
Indeed when reopening his local copy of "page2.html", developer 2 is presented with the following:
 +
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
  <head>
 +
    <meta charset="UTF-8">
 +
    <title>My second page</title>
 +
  </head>
 +
  <body>
 +
    <h1>This is our second page</h1>
 +
<<<<<<< HEAD
 +
    <p>Some other text about something else!</p>
 +
=======
 +
    <p>Some text about something</p>
 +
>>>>>>> 8c409afad98a4a8af4ac0f17a3be594802bc5895
 +
  </body>
 +
</html>
 +
</pre>
 +
 +
As you can see both changes (i.e. both paragraphs) are included, and highlighted by Git. It is up to developer 2 to decide which one to keep and push back to the server. Here a conversation between the 2 developers should take place, and a decision made (for example, merging the wording of the 2 paragraphs!), before the change can be pushed.

Revision as of 17:25, 3 March 2015

Main Page >> GitLab server >> Solving conflicts on GitLab

Solving conflicts

But what happens if 2 developers push a change to the same file? Well, let's take a look.

Let's imagine that our previously created file "page2.html" contains the following, very simple HTML code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My second page</title>
  </head>
  <body>
    <h1>This is our second page</h1>
  </body>
</html>

Two developers make a change

Let's imagine that 2 developers "pull" the latest code above, and then both make a slightly different change, as follow (see added paragraph):

Developer 1:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My second page</title>
  </head>
  <body>
    <h1>This is our second page</h1>
    <p>Some text about something</p>
  </body>
</html>

Developer 2:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My second page</title>
  </head>
  <body>
    <h1>This is our second page</h1>
    <p>Some other text about something else!</p>
  </body>
</html>

Two developers push their changes

Developer 1 commits his change successfully:

git add page2.html
git commit -m "commit by team member 1"
git push -u origin master

Message displayed:

1 file changed, 1 insertion(+)

Then developer 2 tries to commit too:

git add page2.html
git commit -m "commit by team member 2"
git push -u origin master

But he gets an error message:

error: failed to push some refs to 'https://fsegitlab.wlv.ac.uk/in9352/great-work-by-alix.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.

The Git server tells developer 2 to "pull" the latest code again (which will include the change made by developer 1). Let's do that.

Resolving the conflict

Let's pull the code again:

git pull origin

This time a warning message tells us:

CONFLICT (content): Merge conflict in page2.html
Automatic merge failed; fix conflicts and then commit the result.

Indeed when reopening his local copy of "page2.html", developer 2 is presented with the following:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My second page</title>
  </head>
  <body>
    <h1>This is our second page</h1>
<<<<<<< HEAD
    <p>Some other text about something else!</p>
=======
    <p>Some text about something</p>
>>>>>>> 8c409afad98a4a8af4ac0f17a3be594802bc5895
  </body>
</html>

As you can see both changes (i.e. both paragraphs) are included, and highlighted by Git. It is up to developer 2 to decide which one to keep and push back to the server. Here a conversation between the 2 developers should take place, and a decision made (for example, merging the wording of the 2 paragraphs!), before the change can be pushed.