how to collaborate via git and github
Using git and github to collaborate on code and writing is common in research teams that are trying to work reproducibly. Github is really good at version control and allows different people to work on the same project at the same time while avoiding the accumulation of duplicate versions of things that people on the team are too scared to delete.
But… collaborating via github is not like google docs, where you and your collaborators are making changes on a document in real time. Because everyone will be working independently and every so often “pushing” your changes to github, there is the potential for your changes to conflict. To avoid this happening, follow the golden rule of git:
Always “pull” before you “push”
As you are learning git, you might like to use the git tab within RStudio (Option A), but as you become more familiar with the process you might find it faster to type commands in the terminal (Option B).
When you are working on a repo with someone else, it is possible that your collaborator has pushed their changes to your repo since the last time you were working. Whenever you sit down to work on your project/site, it is good practice to “pull” before you “push”.
It is a good idea to use git pull before you think about pushing new content, because by getting the latest version of the repo from git onto your local machine you are much less likely to create merge conflicts (aka clashes with your collaborators).
When you have made some changes and want to push those changes to git, just knit your documents and then tell github that you have some content to add.
First press the commit button.
In the popup window…
Check your github repo to make sure that your changes have appeared.
Once you get a bit more familiar with the process of pulling, committing and pushing your changes to github, you might like to speed up the process by typing commands into the terminal.
First, does your RStudio have a Terminal tab? If not, choose Tools-Terminal-New Terminal to get one.
You want to pull the most up-to-date version of your project from git. Type…
git pull
… into the terminal. If you already have the most recent version, it will let you know that you are Already up to date.
Then let github know which files you want to add. Type…
git add .
… into the terminal. This one won’t give you any feedback. This says hey git, I want to add some stuff, in fact all things that have changed (aka .) This is the same as “staging” all the files that have changed.
Commit your files with a message that lets your collaborators (or your future self) know what has changed. Type…
git commit -m “a message that makes sense”
… into the terminal. This one will give you a list of files that have been changed as feedback.
And finally, push your changes to github. Type…
git push
… into the terminal. Wait for it to chug and complete the push.
Check your github repo to make sure that your changes have appeared.
Watch Jenny do Option A and B in this youtube video
Jenny Bryan who works for R-Studio has written a great site called happygitwithr which has lots of good tips and tricks if you are looking for more.