Conflicts

As soon equally people can work in parallel, information technology's likely someone's going to step on someone else's toes. This will even happen with a single person, when they are working on different computers. Version control helps u.s.a. manage these conflicts by giving us tools to resolve overlapping changes.

To see how we can resolve conflicts, we must first create ane. The file file_one.txt currently looks like this in both partners' copies of our test_one repository:

          > cat file_one.txt        
          # Co-ordinate to Arthur Samuel (1959), car learning gives computers the ability to learn without beingness explicitly programmed. # Today, this area of research largely considers the construction of algorithms that learn from and make predictions virtually the underlying data. # It draws on the fields of statistics and functional analysis to derive a predictive part based on data. # Machine learning algorithms are often categorized every bit being supervised or unsupervised.        

Let's add a line to one partner's copy just:

          > atom file_one.txt > cat file_one.txt        
          # According to Arthur Samuel (1959), auto learning gives computers the ability to learn without being explicitly programmed. # Today, this surface area of enquiry largely considers the construction of algorithms that larn from and make predictions virtually the underlying data. # It draws on the fields of statistics and functional analysis to derive a predictive part based on information. # Machine learning algorithms are often categorized every bit existence supervised or unsupervised. # The majority of practical machine learning uses supervised learning.        

and then button the modify to GitHub:

          > git add file_one.txt > git commit -chiliad "Add a line in our habitation copy"        
          # [main d570fbd] Add a line in our home copy #  1 file changed, 5 insertions(+), four deletions(-) #  rewrite file_one.txt (100%)        
          > git push origin primary        
          # Counting objects: 3, done. # Delta compression using up to viii threads. # Compressing objects: 100% (three/3), done. # Writing objects: 100% (3/3), 400 bytes | 0 bytes/s, washed. # Total 3 (delta 1), reused 0 (delta 0) # remote: Resolving deltas: 100% (ane/1), completed with 1 local object. # To github.com:KevinKotze/test_one.git #    e7619a2..d570fbd  master -> master        

Now if another partner fabricated a unlike change to their copy without updating from GitHub. For case, if we make utilise of the repository on the Desktop:

          > cd "C:\Users\Kevin Kotze\Desktop\kev_test_one" > atom file_one.txt > cat file_one.txt        
          # According to Arthur Samuel (1959), automobile learning gives computers the ability to larn without beingness explicitly programmed. # Today, this area of research largely considers the construction of algorithms that learn from and make predictions well-nigh the underlying data. # It draws on the fields of statistics and functional analysis to derive a predictive function based on data. # Automobile learning algorithms are frequently categorized as being supervised or unsupervised. # Unsupervised learning issues can exist further grouped into clustering and clan issues.        

We can commit the change locally:

          > git add together file_one.txt > git commit -m "Add together a line in my copy"        
          # [master f8c4459] Add a line in my re-create #  one file changed, ane insertion(+)        

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

          > git push origin master        
          # : Permanently added 'github.com,192.thirty.253.112' (RSA) to the list of known hosts. # To github.com:KevinKotze/test_one.git #  ! [rejected]        principal -> master (fetch showtime) # error: failed to button some refs to 'git@github.com:KevinKotze/test_one.git' # hint: Updates were rejected because the remote contains work that you do # hint: not take locally. This is usually caused by some other repository pushing # hint: to the same ref. Yous may want to first integrate the remote changes # hint: (e.grand., 'git pull ...') earlier pushing again. # hint: Run into the 'Notation about fast-forwards' in 'git push --help' for details.        
The Conflicting Changes

Git detects that the changes made in one copy overlap with those made in the other and stops us from trampling on our previous work. What we accept to do is pull the changes from GitHub, merge them into the copy we're currently working in, so push that.

Allow'southward start by pulling:

          > git pull origin master        
          # Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts. # remote: Counting objects: 8, done. # remote: Compressing objects: 100% (6/six), done. # remote: Total 8 (delta 4), reused half dozen (delta ii), pack-reused 0 # Unpacking objects: 100% (8/eight), done. # From github.com:KevinKotze/test_one #  * co-operative            master     -> FETCH_HEAD #    e7619a2..7fe6b48  master     -> origin/master # Machine-merging file_one.txt # CONFLICT (content): Merge conflict in file_one.txt # Automatic merge failed; set up conflicts and then commit the result.        

git pull tells us there'southward a conflict, and marks that conflict in the affected file:

          > cat file_one.txt        
                      # Co-ordinate to Arthur Samuel (1959), machine learning gives computers the power to acquire without being explicitly programmed. # Today, this area of research largely considers the construction of algorithms that learn from and brand predictions about the underlying information. # Information technology draws on the fields of statistics and functional analysis to derive a predictive office based on data. # Machine learning algorithms are often categorized as beingness supervised or unsupervised. <<<<<<< Caput # The bulk of applied machine learning uses supervised learning. ======= # Unsupervised learning problems can be farther grouped into clustering and association problems. >>>>>>> 7fe6b48c3b10d2c8bf44f2e11c3a3ebf22539678        

To draw our attention to the conflict, the output makes use of the discussion HEAD, which is preceded by <<<<<<<. Git has so inserted ======= as a separator between the alien changes and marked the stop of the content downloaded from GitHub with >>>>>>>. (The string of letters and digits later that marker identifies the commit nosotros've just downloaded.)

It is now upwards to us to edit this file (which volition remove these markers) and reconcile the changes. We can practise anything we want: keep the change fabricated in the local repository, go on the change fabricated in the remote repository, write something new to replace both, or become rid of the modify entirely. Permit's include both of these lines later executing the commands:

          > atom file_one.txt > cat file_one.txt        

Note that it will show you the conflicts. The output would now look every bit follows:

          # According to Arthur Samuel (1959), machine learning gives computers the ability to learn without being explicitly programmed. # Today, this area of research largely considers the construction of algorithms that acquire from and make predictions about the underlying data. # Information technology draws on the fields of statistics and functional assay to derive a predictive function based on data. # Motorcar learning algorithms are often categorized every bit being supervised or unsupervised. # Unsupervised learning problems tin be further grouped into clustering and clan problems. # The majority of practical car learning uses supervised learning.        

To cease merging, we add file_one.txt to the changes being made by the merge and and so commit:

          > git add file_one.txt > git status        
          # On branch primary # Your branch and 'origin/master' have diverged, # and have 1 and four dissimilar commits each, respectively. #   (utilise "git pull" to merge the remote branch into yours) # All conflicts fixed but you are all the same merging. #   (use "git commit" to conclude merge) #  # Changes to be committed: #  #         modified:   file_one.txt        
          > git commit -m "Merge changes from GitHub"        
          [master 8f862b8] Merge changes from GitHub        

Now we can push button our changes to GitHub:

          > git push origin chief        
          # Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the listing of known hosts. # Counting objects: half-dozen, washed. # Delta compression using upward to eight threads. # Compressing objects: 100% (6/vi), done. # Writing objects: 100% (half dozen/half-dozen), 713 bytes | 0 bytes/s, done. # Full 6 (delta 2), reused 0 (delta 0) # remote: Resolving deltas: 100% (2/two), completed with i local object. # To github.com:KevinKotze/test_one.git #    7fe6b48..8f862b8  master -> master        

Git keeps track of what we've merged with what, then we don't take to set things by hand again when the collaborator who made the first change pulls again. For example, after changing to the other repository we can then pull:

          > cd "C:\Users\Kevin Kotze\Documents\GitHub\test_one" > git pull origin chief        
          # Alarm: Permanently added 'github.com,192.xxx.253.112' (RSA) to the list of known hosts. # remote: Counting objects: 6, done. # remote: Compressing objects: 100% (four/iv), done. # remote: Full six (delta two), reused 6 (delta 2), pack-reused 0 # Unpacking objects: 100% (half-dozen/six), done. # From github.com:KevinKotze/test_one #  * branch            chief     -> FETCH_HEAD #    7fe6b48..8f862b8  master     -> origin/master # Updating 7fe6b48..8f862b8 # Fast-forwards #  file_one.txt | ane + #  i file changed, 1 insertion(+)        

And we get the merged file:

          > cat file_one.txt        
          # According to Arthur Samuel (1959), motorcar learning gives computers the power to learn without beingness explicitly programmed. # Today, this surface area of inquiry largely considers the construction of algorithms that learn from and brand predictions near the underlying data. # It draws on the fields of statistics and functional analysis to derive a predictive function based on data. # Automobile learning algorithms are often categorized every bit being supervised or unsupervised. # Unsupervised learning issues can exist farther grouped into clustering and association issues. # The majority of practical automobile learning uses supervised learning.        

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

Git'southward power to resolve conflicts is very useful, only conflict resolution takes time and effort, and can introduce many errors if conflicts are not resolved correctly. If you lot find yourself resolving a lot of conflicts in a projection, consider ane of these approaches to reducing them:

  • Try breaking large files apart into smaller files so that it is less probable that two authors volition be working in the same file at the aforementioned time
  • 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 that volition alter the aforementioned file won't exist worked on at the same time

Yous may too wish to piece of work in the following order when there could exist an instance where you are working on the same file:

Activeness Command
i Update local git pull origin primary
two Make changes echo 100 >> numbers.txt
iii Stage changes git add numbers.txt
iv Commit changes git commit -m "Add 100 to numbers.txt"
5 Update remote git push origin master