13.2 Github
Git is a version control system.
Its original purpose was to help groups of developers work collaboratively on big software projects. Git manages the evolution of a set of files – called a repository – in a sane, highly structured way (such as “Track Changes” features from Microsoft Word on steroids)
Install git for windows here
- NOTE: When asked about “Adjusting your PATH environment”, make sure to select “Git from the command line and also from 3rd-party software”. Otherwise, we believe it is good to accept the defaults.
- Note that RStudio for Windows prefers for Git to be installed below C:/Program Files and this appears to be the default. This implies, for example, that the Git executable on my Windows system is found at C:/Program Files/Git/bin/git.exe. Unless you have specific reasons to otherwise, follow this convention.
Now: Introduce yourself to Git
Use a Git client
- “Git” is really just a collection of individual commands you execute in the shell (Appendix A). This interface is not appealing for everyone. Some may prefer to do Git operations via a client with a graphical interface. Git and your Git client are not the same thing, just like R and RStudio are not the same thing. A Git client and an integrated development environment, such as RStudio, are not necessary to use Git or R, respectively.
- Sourcetree is a recommended option
The most basic commands are listed below:
pwd (print working directory). Shows directory or “folder” you are currently operating in. This is not necessarily the same as the R working directory you get from getwd().
ls (list files). Shows the files in the current working directory. This is equivalent to looking at the files in your Finder/Explorer/File Manager. Use ls -a to also list hidden files, such as .Rhistory and .git.
cd (change directory). Allows you to navigate through your directories by changing the shell’s working directory. You can navigate like so: go to subdirectory foo of current working directory: cd foo go to parent of current working directory: cd .. go to your “home” directory: cd ~ or simply cd go to directory using absolute path, works regardless of your current working directory: cd /home/my_username/Desktop. Windows uses a slightly different syntax with the slashes between the folder names reversed, - Pro tip 1: Dragging and dropping a file or folder into the terminal window will paste the absolute path into the window. - Pro tip 2: Use the tab key to autocomplete unambiguous directory and file names. Hit tab twice to see all ambiguous options.
- Use arrow-up and arrow-down to repeat previous commands. Or search for previous commands with CTRL + r.
A few Git commands:
- git status is the most used git command and informs you of your current branch, any changes or untracked files, and whether you are in sync with your remotes.
- git remote -v lists all remotes. Very useful for making sure git knows about your remote and that the remote address is correct.
- git remote add origin GITHUB_URL adds the remote GITHUB_URL with nickname origin.
- git remote set-url origin GITHUB_URL changes the remote url of origin to GITHUB_URL. This way you can fix typos in the remote url.
HTTPS remotes look like https://github.com/
/ .git. Clone the new GitHub repository to your computer via RStudio
- In RStudio, start a new Project:
- File > New Project > Version Control > Git.
- In “Repository URL”, paste the URL of your new GitHub repository. It will be something like this https://github.com/jennybc/myrepo.git.
We create a new Project, with the preferred “GitHub first, then RStudio” sequence. Why do we prefer this? Because this method of copying the Project from GitHub to your computer also sets up the local Git repository for immediate pulling and pushing. Under the hood, we are doing git clone.
Click “Create Project” to create a new directory, which will be all of these things:
- a directory or “folder” on your computer
- a Git repository, linked to a remote GitHub repository
- an RStudio Project
Make local changes, save, commit
- Do this every time you finish a valuable chunk of work, probably many times a day.
- A commit functions like a snapshot of all the files in the repo, at a specific moment.
- Every time you make a commit you must also write a short commit message. Ideally, this conveys the motivation for the change.
- PNG is the “no brainer” format in which to store figures for the web.