A nice feature of collaborative authoring tools such as Google Docs and Overleaf is the ability for several collaborators to edit the same file synchronously, with a feature set similar to GitHub.dev.
Using the Extensions sidebar, search for and install Microsoft’s “Live Share” extension. Once installed, you will have a “Live Share” option in your bottom toolar. Clicking it will automatically copy a URL ending in https://.../join?[randomStringOfCharacters]. If you share this URL with a colleague (or colleagues) you trust, they will be able to collaboratively edit your repository’s text files with you via their web browser. When you commit your changes, they will be listed as co-authors of the commit.
Remark5.1.1.
As of writing, my experience with LiveShare has been hit-or-miss, so I’ll suggest another mechanism for live collaboration. Sometimes when I’m working on a Git repository with a collague on Zoom, I’ll share my screen and give my collaborator control of my mouse and keyboard, or vice versa. Obviously you should only do this with a collaborator you trust, but it’s a simple solution to quickly work together on the same repo!
Section5.2Collaborators and Pull Requests
A particular downside of Live Share (Section 5.1) is that it requires the repository owner to create the Codespace and provision the Live Share session. As a result, there’s no way for a collaborator to make a contribution via Live Share to a GitHub project without the active involvement of the repository owner.
To address this, one solution is for the repository owner to add other GitHub users as collaborators.
Definition5.2.1.
A collaborator for a GitHub repository has the ability to commit and sync changes to the project, as well as adjust certain settings of the repository. GitHub documentation 1
provides some details on the different permissions/abilities that owners have in comparison to collaborators.
Collaborators are added by going to your repository’s Settings tab, using the Collaborators link in the sidebar. Each collaborator will need their own GitHub account, and must accept the invitation to collaborate before gaining access.
Once they have access, a collaborator can either use GitHub.dev (Note 2.4.2) or create their own Codespace (Definition 4.1.1).
Warning5.2.2.
If two collaborators on the same repository make commits on the same branch, they will desynchronize your project’s history: person A’s history will think that commit X is followed by Y on branch main, but person B’s history will think that commit X is followed by Z on branch main.
As seen in Figure 1.1.1, Git is meant to support non-linear history. However, to support this, contributors must name their distinct branches.
Note5.2.3.
One workflow to support multiple collaborators on the same repository is to never directly commit to the main branch, even if you’re the owner.
To commit to an alternative branch in GitHub.dev or Codespaces, select main in the bottom toolbar, then type the name of your new branch, and select “Create new branch”. The name of this branch could be topical, e.g. add-derivative-chapter, but it’s also fine to pick some other unique identifier, e.g. lastname-YYYY-MM-DD.
Once a collaborator is working on a branch, they are free to edit as they wish, and can (and should) commit and push/sync with GitHub to persist their contributions to the team’s repository.
To facilitate communication among collaborators working on different branches, it’s good practice to open a draft pull request once a new branch is created.
Definition5.2.4.
A pull request (or PR for short) is a discussion thread for a branch that proposes changes to a different (often, the main) branch. When the branch’s changes are ready to be merged, this can be accomplished by pressing a button on the pull request webpage.
A PR can be marked as a draft or ready to review.
Note5.2.5.
Depending on whether the collaborator is using GitHub.dev or Codespaces, they may be prompted to create a pull request when first pushing/syncing changes. If not, a pull request can be created by navigating to the repository page on GitHub.com.
Recent pushes to a branch will reveal a prompt to create the pull request immediately. Otherwise, the PR can be created by using the Pull Requests tab of the page.
Unless the PR is for a single commit that’s immediately “ready for review”, a new PR should be created as a draft.
With a draft pull request created, the contributor can continue to commit and push/sync to the branch until it is ready for review. The discussion features of GitHub can allow contributors to discuss the proposed changes, whether they are in draft or review-ready status.
Note5.2.6.
A draft pull request has a large button near the end of the discussion thread to mark the pull request as ready for review.
A ready for review pull request can be converted to a draft by using a small link on the right sidebar.
Depending on the complexity or maturity of the project, you may wish to develop a review process with your collaborators, or simply use the PR workflow to clearly communicate when changes are being made to the main branch, and ensure no two collaborators make incompatible changes to the same branch. Whatever you choose, you’ll eventually want to incorporate these branched changes into your main branch.
Note5.2.7.
There are several options for merging a branch’s pull request. I recommend the “Squash and Merge” option, which converts all the branch’s commits/changes into a single new commit extending the target branch.
As long as contributions are made using appropriate branches and pull requests, you will have minimal problems with conflicting changes made between different collaborators, with GitHub handling the merging process automatically, even if two collaborators edit the same file. (But not always, see Section 5.4.)
But a common error that I frequently make myself: what if you forget to create a branch with your work, and you acccidentally commit to main directly? The first safety rail I recommend is to set up a policy on your repository that will prevent this accidental commit to be pushed to GitHub.
Note5.2.8.
To prevent unintended changes to your main branch, follow the instructions at GitHub’s documentation 2
, using main as your “Branch name pattern”, and enabling required pull requests.
Finally, there’s no button to push that will fix a commit made to the local copy of main accidentally, but there is a quick-enough fix nonetheless.
Note5.2.9.
With branch protection Note 5.2.8 enabled, if you accidentally make updates directly to your personal main branch, attempting to push these from a Codespace will result in tthe error message Can't push refs to remote.
Try running "Pull" first to integrate your changes.
To fix this, open the Terminal and type Listing 5.2.10, changing my-new-branch to the branch name you want to create. Use Enter to execute the command.
Then, copy-paste Listing 5.2.11 and your local main branch will match the official repository, and any changes you’ve made will be reflected on the my-new-branch branch instead.
Section5.3Forks
One great thing about working with open source on GitHub is that not only can you collaborate with your trusted colleagues, but you can also work with collaborators who do not have write access to your repository.
Definition5.3.1.
A fork for a public repository is a copy of the project’s entire history, made either for the main branch or for all publicly shared branches.
Managing contributions from forked repositories is done using the same workflow as I recomend for collaborating with trusted colleagues that you’ve given write access to your repository (Section 5.2). The only difference is that an outside collaborator is creating branches and making commits on their forked copy of your project, not a branch of your original repository. But GitHub still gives essentially the same options for the outside collaborator to create a pull request to your project, without given them access to any data you aren’t already sharing with the public.
Note5.3.2.
To create a fork of a public repository, press the “Fork” button on its GitHub.com homepage. You can name this fork whatever you like, it will be tracked on GitHub as a fork of the original project, with the ability to make “upstream” contributions by way of pull requests.
Those of us who work in open source typically love getting pull requests from random collaborators. For example, if you find a typo in this book, you can fxi it by creating a fork at https://github.com/StevenClontz/github-for-mathematicians/fork, editing the appropriate source/*.ptx file to fix the word, and open a pull request.