LogoLogo
LogoLogo
  • The Barbara K. Ostrom (1978) Bioinformatics and Computing Facility
  • Computing Resources
    • Active Data Storage
    • Archive Data Storage
    • Luria Cluster
      • FAQs
    • Other Resources
  • Bioinformatics Topics
    • Tools - A Basic Bioinformatics Toolkit
      • Getting more out of Microsoft Excel
      • Bioinformatics Applications of Unix
        • Unix commands applied to bioinformatics
        • Manipulate NGS files using UNIX commands
        • Manipulate alignment files using UNIX commands
      • Alignments and Mappers
      • Relational databases
        • Running Joins on Galaxy
      • Spotfire
    • Tasks - Bioinformatics Methods
      • UCSC Genome Bioinformatics
        • Interacting with the UCSC Genome Browser
        • Obtaining DNA sequence from the UCSC Database
        • Obtaining genomic data from the UCSC database using table browser queries
        • Filtering table browser queries
        • Performing a BLAT search
        • Creating Custom Tracks
        • UCSC Intersection Queries
        • Viewing cross-species alignments
        • Galaxy
          • Intro to Galaxy
          • Galaxy NGS Illumina QC
          • Galaxy NGS Illumina SE Mapping
          • Galaxy SNP Interval Data
        • Editing and annotation gene structures with Argo
      • GeneGO MetaCore
        • GeneGo Introduction
        • Loading Data Into GeneGO
        • Data Management in GeneGO
        • Setting Thresholds and Background Sets
        • Search And Browse Content Tab
        • Workflows and Reports Tab
        • One-click Analysis Tab
        • Building Network for Your Experimental Data
      • Functional Annotation of Gene Lists
      • Multiple Sequence Alignment
        • Clustalw2
      • Phylogenetic analysis
        • Neighbor Joining method in Phylip
      • Microarray data processing with R/Bioconductor
    • Running Jupyter notebooks on luria cluster nodes
  • Data Management
    • Globus
  • Mini Courses
    • Schedule
      • Previous Teaching
    • Introduction to Unix and KI Computational Resources
      • Basic Unix
        • Why Unix?
        • The Unix Tree
        • The Unix Terminal and Shell
        • Anatomy of a Unix Command
        • Basic Unix Commands
        • Output Redirection and Piping
        • Manual Pages
        • Access Rights
        • Unix Text Editors
          • nano
          • vi / vim
          • emacs
        • Shell Scripts
      • Software Installation
        • Module
        • Conda Environment
      • Slurm
    • Introduction to Unix
      • Why Unix?
      • The Unix Filesystem
        • The Unix Tree
        • Network Filesystems
      • The Unix Shell
        • About the Unix Shell
        • Unix Shell Manual Pages
        • Using the Unix Shell
          • Viewing the Unix Tree
          • Traversing the Unix Tree
          • Editing the Unix Tree
          • Searching the Unix Tree
      • Files
        • Viewing File Contents
        • Creating and Editing Files
        • Manipulating Files
        • Symbolic Links
        • File Ownership
          • How Unix File Ownership Works
          • Change File Ownership and Permissions
        • File Transfer (in-progress)
        • File Storage and Compression
      • Getting System Information
      • Writing Scripts
      • Schedule Scripts Using Crontab
    • Advanced Utilization of IGB Computational Resources
      • High Performance Computing Clusters
      • Slurm
        • Checking the Status of Computing Nodes
        • Submitting Jobs / Slurm Scripts
        • Interactive Sessions
      • Package Management
        • The System Package Manager
        • Environment Modules
        • Conda Environments
      • SSH Port Forwarding
        • SSH Port Forwarding Jupyter Notebooks
      • Containerization
        • Docker
          • Docker Installation
          • Running Docker Images
          • Building Docker Images
        • Singularity
          • Differences from Docker
          • Running Images in Singularity
      • Running Nextflow / nf-core Pipelines
    • Python
      • Introduction to Python for Biologists
        • Interactive Python
        • Types
          • Strings
          • Lists
          • Tuples
          • Dictionaries
        • Control Flow
        • Loops
          • For Loops
          • While Loops
        • Control Flows and Loops
        • Storing Programs for Re-use
        • Reading and Writing Files
        • Functions
      • Biopython
        • About Biopython
        • Quick Start
          • Basic Sequence Analyses
          • SeqRecord
          • Sequence IO
          • Exploration of Entrez Databases
        • Example Projects
          • Coronavirus Exploration
          • Translating a eukaryotic FASTA file of CDS entries
        • Further Resources
      • Machine Learning with Python
        • About Machine Learning
        • Hands-On
          • Project Introduction
          • Supervised Approaches
            • The Logistic Regression Model
            • K-Nearest Neighbors
          • Unsupervised Approaches
            • K-Means Clustering
          • Further Resources
      • Data Processing with Python
        • Pandas
          • About Pandas
          • Making DataFrames
          • Inspecting DataFrames
          • Slicing DataFrames
          • Selecting from DataFrames
          • Editing DataFrames
        • Matplotlib
          • About Matplotlib
          • Basic Plotting
          • Advanced Plotting
        • Seaborn
          • About Seaborn
          • Basic Plotting
          • Visualizing Statistics
          • Visualizing Proteomics Data
          • Visualizing RNAseq Data
    • R
      • Intro to R
        • Before We Start
        • Getting to Know R
        • Variables in R
        • Functions in R
        • Data Manipulation
        • Simple Statistics in R
        • Basic Plotting in R
        • Advanced Plotting in R
        • Writing Figures to a File
        • Further Resources
    • Version Control with Git
      • About Version Control
      • Setting up Git
      • Creating a Repository
      • Tracking Changes
        • Exercises
      • Exploring History
        • Exercises
      • Ignoring Things
      • Remotes in Github
      • Collaborating
      • Conflicts
      • Open Science
      • Licensing
      • Citation
      • Hosting
      • Supplemental
Powered by GitBook

MIT Resources

  • https://accessibility.mit.edu

Massachusetts Institute of Technology

On this page

Was this helpful?

Export as PDF
  1. Mini Courses
  2. Version Control with Git

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.txt

Output

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

Let’s add a line to the collaborator’s copy only:

nano git.txt
cat git.txt

Output

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 copy

and then push the change to GitHub:

git add git.txt
git commit -m "Add a line in our collaborator's copy"

Output

[main c5adc32] Add a line in our collaborator's copy
 1 file changed, 1 insertion(+)
git push origin main

Output

Counting objects: 5, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 333 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To git@github.com:BMCBCC/practiceGitHub.git
   29f5ad5..c5adc32  main -> main

Now let’s have the owner make a different change to their copy without updating from GitHub:

nano git.txt
cat git.txt

Output

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 owner's copy

We can commit the change locally:

git add git.txt
git commit -m "Add a line in owner's copy"

Output

[main 8b4c562] Add a line in owner's copy
 1 file changed, 1 insertion(+)

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

git push origin main

Output

To git@github.com:BMCBCC/practiceGitHub.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'git@github.com:BMCBCC/practiceGitHub.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git pull origin main

Output

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), done.
From github.com:BMCBCC/practiceGitHub
 * branch            main       -> FETCH_HEAD
Auto-merging git.txt
CONFLICT (content): Merge conflict in git.txt
Automatic merge failed; fix conflicts and then commit the result.

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:

cat git.txt 

Output

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
<<<<<<< HEAD
A line is added to owner's copy
=======
A line is added to collaborator's copy
>>>>>>> c5adc3205dbed5ac3893d2b24bb2acea975f491d

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:

cat git.txt

Output

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
We removed the conflict on this line

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

git add git.txt
git status

Output

# On branch main
# All conflicts fixed but you are still merging.
#   (use "git commit" to conclude merge)
#
# Changes to be committed:
#
#       modified:   git.txt
#
git commit -m "Merge changes from GitHub"

Output

[main e65b358] Merge changes from GitHub

Now we can push our changes to GitHub:

git push origin main

Output

Counting objects: 10, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 620 bytes | 0 bytes/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 3 local objects.
To git@github.com:BMCBCC/practiceGitHub.git
   c5adc32..e65b358  main -> main

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:

remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0
Unpacking objects: 100% (6/6), done.
From github.com:BMCBCC/practiceGitHub
 * branch            main       -> FETCH_HEAD
Updating c5adc32..e65b358
Fast-forward
 git.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

We get the merged file:

cat git.txt

Output

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
We removed the conflict on this line

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:

  1. Pull from upstream more frequently, especially before starting new work

  2. Use topic branches to segregate work, merging to main when complete

  3. Make smaller more atomic commits

  4. 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:

  1. Clarify who is responsible for what areas with your collaborators

  2. 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

  3. 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

PreviousCollaboratingNextOpen Science

Last updated 1 year ago

Was this helpful?

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, them into the copy we’re currently working in, and then push that. Let’s start by pulling:

merge