Home > Articles

This chapter is from the book

11.2 Pulling and Merge Conflicts

In Section 11.1, Alice didn’t make any changes while Bob was making his commit, so there was no chance of conflict, but this is not always the case. In particular, when two collaborators edit the same file, it is possible that the changes might be irreconcilable. Git is pretty smart about merging in changes, and in general conflicts are surprisingly rare, but it’s important to be able to handle them when they occur. In this section, we’ll consider both non-conflicting and conflicting changes in turn.

11.2.1 Non-conflicting Changes

We’ll start by having Alice and Bob make non-conflicting changes in the same file. Suppose Alice decides to change the top-level heading on the About page from “About” to “About Us”, as shown in Listing 11.2.

un11fig01.jpg

Listing 11.2: Alice’s change to the About page’s h1.
~/repos/website/about.html

After making this change, Alice commits and pushes as usual:

un11fig01.jpg
[website (main)]$ git commit -am "Change page heading"
[website (main)]$ git push

Meanwhile, Bob decides to add a new image (Figure 11.11)4 to the About page. He first downloads it with curl as follows:

Figure 11.11

Figure 11.11: An image for Bob to add to the About page.

un11fig02.jpg
[website-copy (main)]$ curl -o images/polar_bear.jpg >                           -L https://cdn.learnenough.com/polar_bear.jpg

(As noted in Section 10.1, you should type the backslash character \ but you shouldn’t type the literal angle bracket >.) He then adds it to about.html using the img tag, as shown in Listing 11.3, with the result shown in Figure 11.12.

Figure 11.12

Figure 11.12: The About page with an added image.

un11fig02.jpg

Listing 11.3: Adding an image to the About page.
~/tmp/website-copy/about.html

Note that Bob has included an alt attribute in Listing 11.3, which is a text alternative to the image. The alt attribute is actually required by the HTML5 standard, and including it is a good practice because it’s used by web spiders and by screen readers for the visually impaired.

Having made his change, Bob commits as usual:

un11fig02.jpg
[website-copy (main)]$ git add -A
[website-copy (main)]$ git commit -m "Add an image"

When he tries to push, though, something unexpected happens, as shown in Listing 11.4.

un11fig02.jpg

Listing 11.4: Bob’s push, rejected.

Because of the changes Alice already pushed, Git won’t let Bob’s push go through: As indicated by the first highlighted line in Listing 11.4, the push was rejected by GitHub. As indicated by the second highlighted line, the solution to this is for Bob to pull:

un11fig02.jpg
[website-copy (main)]$ git pull

Even though Alice made changes to about.html, there is no conflict because Git figures out how to combine the diffs. In particular, git pull brings in the changes from the remote repo and uses merge to combine them automatically, adding the option to add a commit message by dropping Bob into the default editor, which on most systems is Vim (Figure 11.13). (This is just one of many reasons why Learn Enough Text Editor to Be Dangerous (https://www.learnenough.com/text-editor) covers Minimum Viable Vim (Section 5.1).) To get the merge to go through, you can simply quit out of Vim using :q.

Figure 11.13

Figure 11.13: The default editor for merging from a git pull.

We can confirm that this worked by checking the log, which shows both the merge commit and Alice’s commit from the original copy (Listing 11.5).

un11fig02.jpg

Listing 11.5: The Git log after Bob merges in Alice’s changes. (Exact results will differ.)

If Bob now pushes, it should go through as expected:

un11fig02.jpg
$ git push

This puts Bob’s changes on the remote repo, which means Alice can pull them in:

un11fig01.jpg
$ git pull

Alice can confirm that her repo now includes Bob’s changes by inspecting the Git log, which should match the results you got in Listing 11.5. Meanwhile, she can refresh her browser to see Bob’s cool new ursine addition (Figure 11.14).

11.2.2 Conflicting Changes

Even though Git’s merge algorithms can often figure out how to combine changes from different collaborators, sometimes there’s no avoiding a conflict. For example, suppose both Alice and Bob notice that the required alt attribute is missing from the whale image included in Listing 10.1 and decide to correct the issue by adding one.

Figure 11.14

Figure 11.14: Confirming that Alice’s repo includes Bob’s added image.

First, Alice adds the alt attribute “Breaching whale” (Listing 11.6).

un11fig01.jpg

Listing 11.6: Alice’s image alt.
~/repos/website/index.html

She then commits and pushes her change:5

un11fig01.jpg
[website (main)]$ git commit -am "Add necessary image alt"
[website (main)]$ git push
un11fig02.jpg

Listing 11.7: Bob’s image alt.
~/tmp/website-copy/index.html

Meanwhile, Bob adds his own alt attribute, “Whale” (Listing 11.7), and commits his change:

un11fig02.jpg
[website-copy (main)]$ git commit -am "Add an alt attribute"

If Bob tries to push, he’ll be met with the same rejection message shown in Listing 11.4, which means he should pull—but that comes at a cost:

un11fig02.jpg
[website-copy (main)]$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), 415 bytes | 207.00 KiB/s, done.
From https://github.com/mhartl/website
   679afb8..81c190a main        -> origin/main
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
[website-copy (main|MERGING)]$

As indicated in the second highlighted line, Git has detected a merge conflict from Bob’s pull, and his working copy has been put into a special branch state called main|MERGING.

Bob can see the effect of this conflict by viewing index.html in his text editor, as shown in Figure 11.15. Supposing Bob prefers Alice’s more descriptive alt text, he can resolve the conflict by deleting all but the line with alt=”Breaching whale”, as seen in Figure 11.16. (In fact, as seen in Figure 11.15, Atom includes two “Use me” buttons to make it easy to pick one of the options. Clicking on the bottom “Use me” button gives the same result shown in Figure 11.16.)

Figure 11.15

Figure 11.15: A file with a merge conflict.

Figure 11.16

Figure 11.16: The HTML file edited to remove the merge conflict.

After saving the file, Bob can commit his change, which causes the prompt to revert back to displaying the main branch, and at that point he’s ready to push:

un11fig02.jpg
[website-copy (main|MERGING)]$ git commit -am "Use longer alt attribute"
[website-copy (main)]$ git push

Alice’s and Bob’s repos now have the same content, but it’s still a good idea for Alice to pull in Bob’s merge commit:

un11fig01.jpg
[website (main)]$ git pull

Because of the potential for conflict, it’s a good idea to do a git pull before making any changes on a project with multiple collaborators (or even just being edited by the same person on different machines). Even then, on a long enough timeline some conflicts are inevitable, and with the techniques in this section you’re now in a position to handle them.

11.2.3 Exercises

  1. Change your default Git editor from Vim to Atom. Hint: Google for it. (This is an absolutely classic application of technical sophistication (Box 8.2): With a well-chosen Google search, you can often go from “I have no idea how to do this” to “It’s done” in under 30 seconds.)

  2. The polar bear picture added in Listing 11.3 (Figure 11.11) requires attribution under the Creative Commons Attribution 2.0 Generic license. As Alice, link the image to the original attribution page, as shown in Listing 11.8. Then run git commit -a without including -m and a command-line message. This should drop you into the default Git editor. Quit the editor without including a message, which cancels the commit.

  3. Run git commit -a again, but this time add the commit message “Add polar bear attribution link”. Then hit return a couple of times and add a longer message of your choice. (One example appears in Figure 11.17.) Save the message and exit the editor.

    Figure 11.17

    Figure 11.17: Adding a longer message in a text editor.

  4. Run git log to confirm that both the short and longer messages correctly appear. After pushing the changes to GitHub, navigate to the page for the commit to confirm that both the short and longer messages correctly appear.

  5. As Bob, pull in the changes to the About page. Verify by refreshing the browser and by running git log -p that Bob’s repo has been properly updated.

    un11fig01.jpg

Listing 11.8: Linking to the polar bear image’s attribution page.
~/repos/website/about.html

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020