Working with write access

Author

Marie-Hélène Burle

Small teams often collaborate by granting everybody permission to write directly to the project. If you work on a paper with your thesis supervisor or a few co-authors, you will probably work this way. This is what we will cover in this section.

Bigger teams (e.g. research labs, big organizations) usually have a different workflow that we will cover in the next section.

Setup

You initiate the project

In this situation, you are the author of a project (you have a project under version control on your own machine) and you want to initiate a collaboration with others using GitHub as a remote.

Create a remote on GitHub

You need to create a remote on GitHub.

Create a repository on GitHub
  • Go to the GitHub website, login, and go to your home page.
  • Look for the Repositories tab & click the green New button.
  • Enter the name you want for your repo, without spaces.
  • Make the repository public or private.

Invite collaborators

  • Go to your GitHub project page.
  • Click on the Settings tab.
  • Click on the Manage access section on the left-hand side (you will be prompted for your GitHub password).
  • Click on the Invite a collaborator green button.
  • Invite your collaborators with one of their GitHub user name, their email address, or their full name.

You are invited to a project

In this second situation, someone else started a project and they are inviting you to collaborate to it, giving you write access to the project.

In this case, you need to clone the project: cd to the location where you want your local copy, then:

git clone <remote-address> <local-name>

This sets the project as a remote to your new local copy and that remote is automatically called origin.

Without <local-name>, the repo will have the name of the last part of the remote address.

Collaborative workflow

Pulling and pushing

The remotes section of our introductory course to Git covers pushing and pulling to a remote. Remember that to update a remote with your changes, you “push” them with git push.

When you collaborate with others however, you will not be the only person accessing a remote. Git is extremely powerful in that it allows for collaborators to work on a project synchronously, but if one of your collaborators has made changes to the remote (pushing from their own local version of the project), you won’t be able to push.

Instead, you will get the following message:

To <your-project>.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'xxx.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 integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So how does this work?

You first have to update your local version of the project with their change by running git pull.

If there are no conflicts (the same lines of the same files having conflicting versions) Git will automatically merge their changes with yours and now you can push the merged changes back to the remote.

Now… what if there are conflicts?

Resolving conflicts

Git works line by line. As long as your collaborators and you aren’t working on the same line(s) of the same file(s) at the same time, there will not be any problem. If however you modified one or more of the same line(s) of the same file(s), Git will not be able to decide which version should be kept.

When you git pull their work on your machine, the automatic merging will get interrupted and Git will ask you to resolve the conflict(s) before the merge can resume. It will conveniently tell you which file(s) contain the conflict(s).

There are fancy tools to resolve conflicts, but you can do it in any text editor: simply open the file(s) listed by Git as having conflicts and look for the following markers:

<<<<<<< HEAD
This is your version.
=======
This is the alternative version of the same section of the file.
>>>>>>> alternative version

These markers are added by Git to signal the areas of conflict. It is up to you to choose between the two versions (or create a third one) and remove the conflict markers. After that, you can stage the file(s) which contained the conflicts to finish the merge (and then you can commit).