Conflicts
How to resolve conflicts
As soon as people can work in parallel, they’ll likely step on each other’s toes. This will 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 different changes to each copy. Version control helps us manage these conflicts by giving us tools to resolve overlapping changes.
To see how we can resolve conflicts, we must first create one. The file git.txt currently looks like this in both collaborator's copies of our practicegit repository:
cat git.txtOutput
Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."
"git" can mean anything, depending on your mood.
I decide to use git to help my work
According to a recent StackOverflow survey, over 90% of developers use GitLet’s add a line to the collaborator’s copy only:
nano git.txt
cat git.txtOutput
Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."
"git" can mean anything, depending on your mood.
I decide to use git to help my work
According to a recent StackOverflow survey, over 90% of developers use Git
A line is added to collaborator's copyand then push the change to GitHub:
Output
Output
Now let’s have the owner make a different change to their copy without updating from GitHub:
Output
We can commit the change locally:
Output
but Git won’t let us push it to GitHub:
Output

Git rejects the push because it detects that the remote repository has new updates that have not been incorporated into the local branch. What we have to do is pull the changes from GitHub,merge them into the copy we’re currently working in, and then push that. Let’s start by pulling:
Output
The git pull command updates the local repository to include those changes already included in the remote repository. After the changes from remote branch have been fetched, Git detects that changes made to the local copy overlap with those made to the remote repository, and therefore refuses to merge the two versions to stop us from trampling on our previous work. The conflict is marked in in the affected file:
Output
Our change is preceded by <<<<<<< HEAD. Git has then inserted ======= as a separator between the conflicting changes and marked the end of the content downloaded from GitHub with >>>>>>>. (The string of letters and digits after that marker identifies the commit we’ve just downloaded.)
It is now up to us to edit this file to remove these markers and reconcile the changes. We can do anything we want: keep the change made in the local repository, keep the change made in the remote repository, write something new to replace both, or get rid of the change entirely. Let’s replace both so that the file looks like this:
Output
To finish merging, we add git.txt to the changes being made by the merge and then commit:
Output
Output
Now we can push our changes to GitHub:
Output
Git keeps track of what we’ve merged with what, so we don’t have to fix things by hand again when the collaborator who made the first change pulls again:
We get the merged file:
Output
We don’t need to merge again because Git knows someone has already done that.
Git’s ability to resolve conflicts is very useful, but conflict resolution costs time and effort, 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 frequently, especially before starting new work
Use topic branches to segregate work, merging to main when complete
Make smaller more atomic commits
Where logically appropriate, break 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 project management strategies:
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 expected to change the same lines won’t be worked on simultaneously
If the conflicts are stylistic churn (e.g. tabs vs. spaces), establish a project convention that is governing and use code style tools (e.g. htmltidy, perltidy, rubocop, etc.) to enforce, if necessary
Last updated
Was this helpful?
