Version control with git(hub)

What are git & github?

Git and Github are most suitable for working with relatively small files. While originally used for code/software, you can use it for other types of small files as well, such as documentation.


Why should I use git(hub)?


Installation

Note: if you want to work with git in the command line on Windows, I can highly recommend using the Ubuntu app (the Linux Subsystem for Windows, downloadable via the Windows store), which may cause fewer Windows-related errors.


The git workflow

When working on a git project (within a folder called a git repository), you will always perform the following steps:

Any commit gets a specific identifier that can be used to reverse (undo) the commit.



Branches

A git repository can exist in multiple “versions” which are called branches. There is always a “master” branch, which you should consider the clean branch. Besides that, you can create other branches that are meant to make your own changes, or try something different without dirtying the clean (master) version. After you have made changes in your own branch and you think they should be incorporated in the master branch, you can then merge your branch with the master branch.



Workflow on github

On Github, the workflow is a bit more extensive, because often you are collaborating and do not want others to just start editing the master branch right away. There are multiple methods to collaborate on a project, but we recommend the following, assuming that there is already a repository for the project and you want to contribute:

  1. On the repository page on Github, fork the repository: this creates a copy of the repository on your own Github account that you have full access to.
  2. In your forked (copied) Github repository, create a new branch for the changes you are about to make with a short but comprehensible name, e.g. “dorienchanges”.
  3. If you want to edit files locally, clone your repository to your local PC, creating a folder in your file explorer (the contents of which can change according to which branch you are on!).
    • Via the command line: git clone https://github.com/UserName/RepositoryName.git
    • Via Rstudio, see this link
  4. Edit the files you want to edit and commit the changes (making a snapshot; include a comprehensible commit message!)
  5. You have now committed changes locally, but they are not yet visible in your remote repository, i.e., the online github repository on your account. In order to get the commits you made locally to be visible online, you need to push them to your remote repository on Github.
    • Via the command line, note that the repository on your account is usually called "origin": git push origin branchnameonwhichyouworked
    • Via Rstudio, see this link
  6. Now the changes are visible in your own account, but not in the main repository. In order to get your changes into the main repository, you need to do a pull request on Github. This is a request to the owners of the original repository to merge your branch with (one of) theirs. Once merged by the owners, you are often prompted to remove your own branch (which is not necessary if you are planning to make more changes later).


Keeping your local copy (clone) up to date

If you are working on a project with many collaborators making changes, the odds are that your own fork (online copy) and/or clone (local copy) are becoming out-of-date quite fast. Therefore, it is recommended to update those copies each time before you start making changes yourself, so you are working on the most recent versions of the files.


Resources

For every piece of software, remember that google is your best friend. Or use one of the following other resources: