The example website for this course is located at https://eeb504.github.io/rmarkdown-website-examples/index.html.
Broadly speaking, there are two classes of git
commands that we have introduced in this short course. The first deals with actions (staging, committing, and pushing to a remote server--e.g. GitHub). The second set is diagnostic (What is going on in this repository--what is its status? What differences have I made for files that are tracked? What differences are staged?).
When you have a new project (such as a new manuscript, thesis chapter, research project, lab protocol, etc.), and seek to set up a new Git repository for this project (which will encompass a parent folder, potential sub-folders, and files), we recommend the following steps:
- Go online to GitHub.com and click on the
+
sign in the upper right and selectNew Repository
(notImport Repository
,New Gist
, orNew Organization
). - Give your repository an informative name and a brief description. This description will be the initial
README.md
file (and thisREADME
file is what people see when they navigate to your repository, assuming that it is public). Check the box forInitialize this repository with a README
and make selections for the drop-down menus inAdd .gitignore
(you can search for popular languages like R, or python) as well asAdd a license
(the(i)
icon next to theAdd a license
drop-down menu provides information on different options, though for academics,GNU General Public License v 3.0
andMIT License
are generally popular). - Clone the repository from GitHub to your local machine. The steps differ based on your client for Git.
- If you use terminal to execute local Git commands, navigate to the
Clone or download
drop-down menu in your GitHub repository and copy theClone with HTTPS
address (e.g.https://github.com/EEB504/rmarkdown-website-examples.git
).- Subsequently, in terminal, navigate to the directory where you would like the repository to be located (e.g.
/Users/MYNAME/Documents
or/Users/MYNAME/Dropbox
) using the command$ cd /Users/MYNAME/FOLDER
- Use the command
$ git clone https://github.com/YourAccount/RepositoryName.git
. This will create a new folder in this directory with the name of the repository (e.g./Users/Char/Documents/MyRepository
).
- Subsequently, in terminal, navigate to the directory where you would like the repository to be located (e.g.
- If you use a graphical interface like GitHub Desktop, GitKraken, or SourceTree, use the menu to clone the repository. As long as your GitHub account as synced with this desktop client, it should be pre-populated in their drop-down menu.
- If you use terminal to execute local Git commands, navigate to the
- Once the repository is on your local machine, begin creating files for analysis and writing up your projects! (Or move the files over and begin tracking them.)
Action Steps
git add
: begins tracking a new file and stages any information (e.g. text) contained in that file.- Until you tell git to track a file with
git add
, it will not record any changes to this file. Git ignores files until you usegit add Filname.Extension
. After that file is added and committed, git will then track any changes made to this file through time.
- Until you tell git to track a file with
git commit
: this command formally saves the changes introduced in the previousgit add
command to the local repository.- If you are using a terminal shell, I advocate for using
git commit -m "INFORMATIVE MESSAGE"
as the-m
flag lets you append a short commit message inline (without having to use Nano/vi/vim to write a message, save it, and exit).
- If you are using a terminal shell, I advocate for using
git push origin master
: this command publishes (or pushes) the changes performed in the last commit to your remote repository (most often, a GitHub repository).
git add
: stages any changes made to files that are already tracked.git commit
: this command formally saves the changes introduced in the previousgit add
command to the local repository.- If you are using a terminal shell, I advocate for using
git commit -m "INFORMATIVE MESSAGE"
as the-m
flag lets you append a short commit message inline (without having to use Nano/vi/vim to write a message, save it, and exit).
- If you are using a terminal shell, I advocate for using
git push origin master
: this command publishes (or pushes) the changes performed in the last commit to your remote repository (most often, a GitHub repository).
Diagnostic steps
Using terminal as your Git client:
git status
: What is the state of my repository?Changes not staged for commit:...
What files have changed since the last checkpoint?Untracked files:
Files that are not yet tracked by Git. You may well have files that you never want Git to track (such as.jpeg
or even some scratch script files) but you may want to avoid adding their extensions into the.gitignore
file because sometimes, some of those types of files may have meaningful changes while others may not. For me, this is typical for serialized.rda
files (R data object files); sometimes I want to track these; other times I don't. Note that the default.gitignore
populated by GitHub forR
includes.RData
as a file type to ignore.- If the last checkpoint was the initial
git clone
command, you would see that any new files or files moved into this repository are listed asUntracked files
unless they hae an extension that is excluded by the.gitignore
file (such as.Rhistory
for aR
based.gitignore
).
- If the last checkpoint was the initial
- Note that if you use a graphical client like GitHub Desktop or GitKraken, the view pane effectively shows you the
git status
-- what files have changed, etc. - In terminal, you would navigate to your repository (
$ cd /path/to/my/repository
) and then execute$ git status
.
git diff
: What are the specific changes that have happened since the last checkpoint? (Each checkpoint for the entire repository is associated with a specificgit commit
)- In terminal,
git diff --cached
is how you see changes that are staged (git add
) whereasgit diff
shows you changes to all tracked files that are not yet staged. - In a graphical client, whenever you click on a file that has changed, it should show the changes that have happened.
- In terminal,
FMI:
- Happy Git with R
- Chapter 16 (New Project, GitHub First)
- Chapter 17 (Existing Project, GitHub First).
- We do not recommend the workflow in Chapter 18 (Existing project, GitHub last); as Jenny Bryan notes, "This is less desirable for a novice because there are more opportunities to get confused and make a mistake."
- This is arguably one of the best resources for learning how to use Git and though it is written for R users, many of the suggestions would also port to python and other languages
- Red Badger
- Gousset learning Git on Windows
- Git Tower
- Navigate to https://github.com/EEB504/rmarkdown-website-examples
- Note that steps 1-4 are all performed online on GitHub
- Fork this repository (
rmarkdown-website-examples
) - After forking, rename this repository
- Obtain the Clone link
- Navigate back to your local machine
- In the shell (terminal) or in a Git client such as GitHub Desktop, SourceTree, GitKraken, or any other client, perform the operation:
- In terminal:
- Navigate to the desired parent directory where you would like this repository to live on your local machine. For me, this is often either
/Users/Char/Documents
or/Users/Char/Google Drive/
. To perform this command in the shell, you would do something along the lines of$ cd ~/Documents
or$ cd ~/GoogleDrive
$ git clone https://github.com/YOURUSERNAME/REPONAME
- For instance, for the EEB504/rmarkdown-website-examples repository, this would look like
$ git clone https://github.com/EEB504/rmarkdown-website-examples.git
- Navigate to the desired parent directory where you would like this repository to live on your local machine. For me, this is often either
- In a local Git client, note that the commands will resemble these from GitHub desktop:
- In terminal:
- Using either
Finder
(Mac OS) orFile Explorer
(Windows), navigate to the directory where the repository is located. Navigate to the folder calleddocs
.
If you only have the R Console installed, these files will open but your workflow will need to be different. See Part 3, Version 2.
- Note that the
.Rproj
file will let you open aR Project
inRStudio
where you can edit the website contents.- Before you open the
.Rproj
file, you may want to rename it so that its name matches the name of your repository. For instance, here is an example where I am re-namingrmarkdown-website-examples.Rproj
totest-website.Rproj
inFinder
on my Mac. - It isn't necessary that you do this, but it may be useful for future reference.
- Before you open the
- Open (double click) the file
FILENAME.Rproj
(for example, on my machine, it has been renamedtest-website.Rproj
); this will open RStudio (if it is installed on your machine).- If you renamed the
.Rproj
file, perform these next steps: - In RStudio, you should now see a small, vertical
Git
icon. Click on this as you will need to git stage and commit the fact that you renamed the.Rproj
file. - Perform the equivalent of the
git add
andgit rm
(remove) command in the Git console in RStudio by clicking on the two boxes on the left-hand side. ; this will subsequently produce this image: - Write an informative
git commit
message - This will open a shell that displays what is happening when you click on the
Commit
button
- If you renamed the
- Begin website development!
- Make changes to
about.Rmd
to change yourAbout Me
page.- For example, I added the line
Test, test
to the bottom of theabout.Rmd
file.
- For example, I added the line
- Before we can see any of these changes reflected online, we need to go into the R console (in my version of RStudio, this is in the upper right-hand corner) and use the command
rmarkdown::render_site()
to generate updatedHTML
pages. (This calls the commandrender_site()
from thermarkdown
package). - Perform the
git add
(staging; this is done by clicking the box for each file under the left-handStaged
column) andgit commit
commands in theGit
console in RStudio. - Now that you have completed a set of changes for your website, select
git push
in theRStudio Git
console.
- Begin developing your website!
- Open and edit
about.Rmd
in the text editor of your choice (this may be Atom, Notepad ++, or good old TextEdit or Notepad) - Build your website in either the R Console or in a terminal shell.
- In R Console:
- Use the command
setwd()
to change the working directory to the correct - Enter and run the command
rmarkdown::render_site()
- Use the command
- In a terminal shell: run the command
Rscript build_site.R
- Use Git in GitHub Desktop, GitKraken, or in a terminal shell to
git add
andgit commit
your changes. - In a Git client (GitHub Desktop, GitKraken, SourceTree, etc.):
- Navigate to your repository in the file menu.
- Click on button to stage
docs/about.html
anddocs/about.Rmd
- Write an informative commit message (e.g.
Added test, test to About Me
) and commit.
- In the shell:
- Navigate to the folder where the repository is housed locally
- Perform
$ git add docs/about.html
;$ git add docs/about.Rmd
- Perform
$ git commit -m "INFORMATIVE MESSAGE"
(e.g. "Added test, test to About Me")
- In your GitHub account, go to the repository associated with your website. For instance, this may look like
https://github.com/EEB504/rmarkdown-website-examples
(for you, this will look like:https://github.com/YOURNAME/WebsiteRepoName
) - Navigate to
Settings
- Scroll down to
GitHub Pages
. Initially, it will probably state thatGitHub Pages is currently disabled. Select a source below to enable GitHub Pages for this repository.
- Click on the drop-down menu initially called
None
and selectmaster branch /docs folder
to have the website deployed!
- You will definitely want to change the website name to your name and probably add on more tabs! To change the website name and home page (
index.Rmd
andindex.html
) edit:_site.yml
index.Rmd
- Perform the
git add
,git commit
, andgit push
commands as before.
- To add more tabs: see Nick Strayer and Lucy D'Agostino McGown's tutorial. For adding more content to your website and manipulating the appearance, see the sections following
Show the world who you are.
- Note that their command for
touch style.css
is not necessary; you can directly edit thestyle.css
file in yourdocs
folder. - Moreover, you add a new tab to your website by changing (1)
_site.yml
to include another tab (- text: "New Tab" \cr href: NewTab.html
where\cr
indicates that- text:...
andhref:...
should be separated by a newline (enter/carriage return)) and (2) creating another Rmarkdown file (in this case, you would want to name itNewTab.Rmd
).- Note: Whenever you seek to make a new tab, know that the
FILENAME.html
link in thehref
line in the_site.yml
must be the same name asFILENAME.Rmd
. This is becausermarkdown::render_site()
includes an operation called "knitting" where it converts R Markdown (relatively easy to write) into HTML (relatively painful to write). When the R Markdown file (e.g.Filename.Rmd
orNewTab.Rmd
orabout.Rmd
) is knit to HTML, the new HTML file will have the same base filename (soFilename.html
,NewTab.html
,about.html
). - For this reason, it is best if your filenames for the
.Rmd
files do not contain spaces (avoid a name likeMy Special Tab.Rmd
) or weird special characters (avoid a file name likeI\\love\birds***Wha&&&&.Rmd
). If you do want separation between individual worlds, camel case (e.g.camelCase.Rmd
,mySpecialTab.Rmd
) is a good option as are underscores (e.g.my_Special_Tab.Rmd
,about_me.Rmd
). You can always combine camel case and underscores (e.g.Website_AboutMe.Rmd
).
- Note: Whenever you seek to make a new tab, know that the
- Note that their command for
- If you don't like the default theme or any of the Jekyll Themes (FMI), open
_config.yml
and delete the linetheme: jekyll-theme-slate
and performgit add
,git commit
, andgit push
as usual. - The RStudio RMarkdown Websites site has more information on the tools introduced here. For more information on
RMarkdown
syntax, please see RMarkdown Overview or the Cheet Sheat.