Merge the Remote Changes Before Pushing Again See the note About

Conflicts

Overview

Questions

  • What practice I do when my changes conflict with someone else's?

Objectives

  • Explicate what conflicts are and when they tin can occur.

  • Resolve conflicts resulting from a merge.

Equally before long as people can work in parallel, it's probable someone's going to step on someone else's toes. This volition even happen with a single person: if we are working on a piece of software on both our laptop and a server in the lab, we could make dissimilar changes to each copy. Version control helps usa manage these conflicts by giving us tools to resolve overlapping changes.

To encounter how we can resolve conflicts, we must starting time create one. The file mars.txt currently looks like this in both partners' copies of our planets repository:

            Cold and dry, but everything is my favorite color The ii moons may be a problem for Wolfman But the Mummy will appreciate the lack of humidity                      

Let's add a line to ane partner's re-create only:

            $ nano mars.txt $ cat mars.txt                      
            Cold and dry, but everything is my favorite color The ii moons may be a problem for Wolfman But the Mummy will appreciate the lack of humidity This line added to Wolfman'due south copy                      

and and so push the change to GitHub:

            $ git add mars.txt $ git commit -m "Add together a line in our domicile copy"                      
            [master 5ae9631] Add a line in our home re-create  1 file inverse, one insertion(+)                      
            Counting objects: five, done. Delta pinch using upwardly to four threads. Compressing objects: 100% (3/three), done. Writing objects: 100% (iii/three), 352 bytes, done. Total 3 (delta ane), reused 0 (delta 0) To https://github.com/vlad/planets    29aba7c..dabb4c8  master -> master                      

Now let'south have the other partner make a dissimilar modify to their copy without updating from GitHub:

            $ nano mars.txt $ cat mars.txt                      
            Cold and dry, but everything is my favorite colour The two moons may be a problem for Wolfman But the Mummy will appreciate the lack of humidity We added a dissimilar line in the other copy                      

We tin commit the modify locally:

            $ git add mars.txt $ git commit -thousand "Add a line in my copy"                      
            [master 07ebc69] Add a line in my copy  1 file changed, 1 insertion(+)                      

but Git won't let us push it to GitHub:

            To https://github.com/vlad/planets.git  ! [rejected]        main -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/vlad/planets.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.thou. 'git pull') hint: before pushing again. hint: Come across the 'Note about fast-forwards' in 'git push --aid' for details.                      

The Conflicting Changes

Git detects that the changes made in ane copy overlap with those fabricated in the other and stops us from trampling on our previous work. What nosotros have to practice is pull the changes from GitHub, merge them into the re-create we're currently working in, and then button that. Permit'southward showtime by pulling:

            remote: Counting objects: five, done. remote: Compressing objects: 100% (2/2), done. remote: Total three (delta 1), reused iii (delta 1) Unpacking objects: 100% (3/iii), done. From https://github.com/vlad/planets  * branch            main     -> FETCH_HEAD Motorcar-merging mars.txt CONFLICT (content): Merge disharmonize in mars.txt Automatic merge failed; fix conflicts and so commit the result.                      

git pull tells u.s. at that place's a conflict, and marks that disharmonize in the affected file:

            Common cold and dry out, but everything is my favorite colour The two moons may be a problem for Wolfman But the Mummy volition appreciate the lack of humidity <<<<<<< HEAD We added a different line in the other copy ======= This line added to Wolfman's re-create >>>>>>> dabb4c8c450e8475aee9b14b4383acc99f42af1d                      

Our change—the i in Head—is preceded by <<<<<<<. Git has then inserted ======= equally a separator between the conflicting changes and marked the terminate of the content downloaded from GitHub with >>>>>>>. (The cord of letters and digits after that mark identifies the commit nosotros've just downloaded.)

It is at present up to the states to edit this file to remove these markers and reconcile the changes. Nosotros can do anything we want: proceed the modify made in the local repository, keep the change fabricated in the remote repository, write something new to supersede both, or go rid of the modify entirely. Let'south replace both so that the file looks like this:

            Cold and dry, simply everything is my favorite color The two moons may be a problem for Wolfman But the Mummy will appreciate the lack of humidity We removed the conflict on this line                      

To finish merging, we add mars.txt to the changes existence made by the merge and then commit:

            $ git add mars.txt $ git status                      
            On branch chief All conflicts fixed but you lot are still merging.   (apply "git commit" to conclude merge)  Changes to be committed:  	modified:   mars.txt                      
            $ git commit -m "Merge changes from GitHub"                      
            [master 2abf2b1] Merge changes from GitHub                      

At present nosotros can button our changes to GitHub:

            Counting objects: 10, done. Delta compression using upwardly to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 697 bytes, done. Total 6 (delta 2), reused 0 (delta 0) To https://github.com/vlad/planets.git    dabb4c8..2abf2b1  chief -> master                      

Git keeps track of what we've merged with what, and so we don't accept to set up things by hand again when the collaborator who made the beginning change pulls again:

            remote: Counting objects: 10, done. remote: Compressing objects: 100% (four/4), done. remote: Total 6 (delta 2), reused 6 (delta 2) Unpacking objects: 100% (6/6), washed. From https://github.com/vlad/planets  * co-operative            master     -> FETCH_HEAD Updating dabb4c8..2abf2b1 Fast-forrard  mars.txt | 2 +-  1 file changed, one insertion(+), 1 deletion(-)                      

Nosotros become the merged file:

            Cold and dry, but everything is my favorite color The 2 moons may be a problem for Wolfman Simply the Mummy will appreciate the lack of humidity We removed the conflict on this line                      

We don't need to merge again because Git knows someone has already done that.

Git's power to resolve conflicts is very useful, merely conflict resolution costs time and attempt, and can introduce errors if conflicts are not resolved correctly. If you find yourself resolving a lot of conflicts in a project, consider these technical approaches to reducing them:

  • Pull from upstream more ofttimes, particularly before starting new piece of work
  • Utilize topic branches to segregate piece of work, merging to master when complete
  • Make smaller more atomic commits
  • Where logically appropriate, pause large files into smaller ones so that it is less likely that two authors will alter the same file simultaneously

Conflicts can also be minimized with projection management strategies:

  • Analyze who is responsible for what areas with your collaborators
  • Discuss what order tasks should be carried out in with your collaborators so that tasks expected to change the same lines won't be worked on simultaneously
  • If the conflicts are stylistic churn (east.g. tabs vs. spaces), institute a project convention that is governing and use code fashion tools (e.g. htmltidy, perltidy, rubocop, etc.) to enforce, if necessary

Solving Conflicts that You lot Create

Clone the repository created by your instructor. Add a new file to it, and modify an existing file (your instructor will tell you which one). When asked by your instructor, pull her changes from the repository to create a conflict, then resolve information technology.

Conflicts on Non-textual files

What does Git do when there is a disharmonize in an image or some other non-textual file that is stored in version control?

A Typical Work Session

You sit down down at your reckoner to piece of work on a shared project that is tracked in a remote Git repository. During your work session, yous have the post-obit actions, only not in this order:

  • Brand changes by appending the number 100 to a text file numbers.txt
  • Update remote repository to lucifer the local repository
  • Celebrate your success with beer(due south)
  • Update local repository to lucifer the remote repository
  • Phase changes to be committed
  • Commit changes to the local repository

In what club should yous perform these actions to minimize the chances of conflicts? Put the commands above in order in the action cavalcade of the table below. When you have the order right, run across if you can write the corresponding commands in the command cavalcade. A few steps are populated to become you started.

club activity . . . . . . . . . . command . . . . . . . . . .
i
2 echo 100 >> numbers.txt
3
four
5
six Celebrate! AFK

Key Points

  • Conflicts occur when two or more people change the same file(s) at the same time.

  • The version control organisation does not allow people to overwrite each other's changes blindly, but highlights conflicts so that they tin can exist resolved.

hillantood66.blogspot.com

Source: http://homepages.cs.ncl.ac.uk/matthew.forshaw/teaching/csc8627/versioncontrol/09-conflict/

0 Response to "Merge the Remote Changes Before Pushing Again See the note About"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel