Conflicts

Equally shortly every bit people tin can work in parallel, it's likely someone's going to footstep on someone else's toes. This volition 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 run across how nosotros can resolve conflicts, we must kickoff create one. 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), machine learning gives computers the power to learn without being explicitly programmed. # Today, this area of research largely considers the construction of algorithms that learn from and make predictions nigh the underlying data. # Information technology draws on the fields of statistics and functional analysis to derive a predictive role based on data. # Machine learning algorithms are often categorized as beingness supervised or unsupervised.        

Let'due south add a line to 1 partner'southward copy only:

          > atom file_one.txt > cat file_one.txt        
          # 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 learn from and make predictions about the underlying information. # Information technology draws on the fields of statistics and functional analysis to derive a predictive function based on data. # Machine learning algorithms are often categorized equally beingness supervised or unsupervised. # The bulk of practical machine learning uses supervised learning.        

and then push the change to GitHub:

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

At present if another partner fabricated a unlike modify to their copy without updating from GitHub. For example, if we brand use 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), car learning gives computers the ability to learn without being explicitly programmed. # Today, this area of research largely considers the construction of algorithms that learn from and make predictions nearly the underlying data. # It draws on the fields of statistics and functional analysis to derive a predictive office based on data. # Machine learning algorithms are oft categorized as being supervised or unsupervised. # Unsupervised learning problems can be further grouped into clustering and association problems.        

We can commit the change locally:

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

but Git won't let us 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]        master -> primary (fetch kickoff) # fault: failed to push some refs to 'git@github.com:KevinKotze/test_one.git' # hint: Updates were rejected considering the remote contains piece of work that yous exercise # hint: not have locally. This is usually caused past another repository pushing # hint: to the aforementioned ref. Y'all may desire to starting time integrate the remote changes # hint: (e.grand., 'git pull ...') before pushing once more. # hint: See the 'Note nigh fast-frontward' in 'git push button --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 piece of work. What we accept to do is pull the changes from GitHub, merge them into the copy we're currently working in, and then push that.

Allow's start past 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% (vi/6), washed. # remote: Full viii (delta 4), reused 6 (delta 2), pack-reused 0 # Unpacking objects: 100% (eight/eight), done. # From github.com:KevinKotze/test_one #  * branch            master     -> FETCH_HEAD #    e7619a2..7fe6b48  master     -> origin/master # Auto-merging file_one.txt # CONFLICT (content): Merge conflict in file_one.txt # Automatic merge failed; set conflicts and then commit the issue.        

git pull tells united states of america there's a conflict, and marks that conflict in the affected file:

          > cat file_one.txt        
                      # According to Arthur Samuel (1959), machine learning gives computers the power to learn without being explicitly programmed. # Today, this surface area of research largely considers the construction of algorithms that learn from and make predictions about the underlying data. # It draws on the fields of statistics and functional assay to derive a predictive function based on data. # Car learning algorithms are often categorized as existence supervised or unsupervised. <<<<<<< Head # The majority of applied car learning uses supervised learning. ======= # Unsupervised learning problems tin can be further grouped into clustering and association problems. >>>>>>> 7fe6b48c3b10d2c8bf44f2e11c3a3ebf22539678        

To describe our attending to the conflict, the output makes use of the word Head, which is preceded by <<<<<<<. Git has then inserted ======= every bit a separator between the conflicting changes and marked the cease of the content downloaded from GitHub with >>>>>>>. (The string of letters and digits afterward that marker identifies the commit nosotros've merely downloaded.)

It is at present up to us to edit this file (which will remove these markers) and reconcile the changes. We can exercise anything we want: go on the alter made in the local repository, keep the change fabricated in the remote repository, write something new to replace both, or go rid of the change entirely. Let'due south include both of these lines after executing the commands:

          > atom file_one.txt > cat file_one.txt        

Note that information technology volition testify you the conflicts. The output would now wait equally follows:

          # According to Arthur Samuel (1959), machine learning gives computers the ability to learn without beingness explicitly programmed. # Today, this area of research largely considers the structure of algorithms that learn from and brand predictions near the underlying information. # It draws on the fields of statistics and functional analysis to derive a predictive function based on data. # Machine learning algorithms are oftentimes categorized as being supervised or unsupervised. # Unsupervised learning problems tin can be farther grouped into clustering and association bug. # The majority of practical motorcar learning uses supervised learning.        

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

          > git add file_one.txt > git status        
          # On branch master # Your branch and 'origin/master' have diverged, # and have one and 4 different commits each, respectively. #   (use "git pull" to merge the remote branch into yours) # All conflicts fixed only y'all are still merging. #   (utilise "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 our changes to GitHub:

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

Git keeps track of what we've merged with what, so we don't have to fix things past paw again when the collaborator who made the first change pulls again. For example, later changing to the other repository nosotros can then pull:

          > cd "C:\Users\Kevin Kotze\Documents\GitHub\test_one" > git pull origin chief        
          # Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the listing of known hosts. # remote: Counting objects: 6, washed. # remote: Compressing objects: 100% (iv/four), done. # remote: Total 6 (delta 2), reused 6 (delta ii), pack-reused 0 # Unpacking objects: 100% (vi/six), washed. # From github.com:KevinKotze/test_one #  * co-operative            master     -> FETCH_HEAD #    7fe6b48..8f862b8  master     -> origin/master # Updating 7fe6b48..8f862b8 # Fast-forwards #  file_one.txt | 1 + #  1 file changed, ane insertion(+)        

And we get the merged file:

          > cat file_one.txt        
          # According to Arthur Samuel (1959), machine learning gives computers the power to acquire without being explicitly programmed. # Today, this area of research largely considers the structure of algorithms that learn from and brand predictions 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 often categorized as beingness supervised or unsupervised. # Unsupervised learning problems tin can be farther grouped into clustering and association problems. # The bulk of practical car learning uses supervised learning.        

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

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

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

You may as well wish to work in the following club when at that place could be an instance where you are working on the aforementioned file:

Activeness Command
1 Update local git pull origin principal
2 Brand changes echo 100 >> numbers.txt
three Stage changes git add numbers.txt
4 Commit changes git commit -1000 "Add together 100 to numbers.txt"
5 Update remote git push origin master