It’s time to start learning how to use Git and GitHub! In the following activities we’re going to learn how to:
Clone: Make a local copy of a GitHub repository you can work in
Pull: Update your local copy with the latest changes
Commit: Save your file changes locally in your Git history
Push: Send your changes to GitHub so others can see them
Branch: Create a parallel version of your repository
Merge: Combine changes across two branches
Stash: Temporarily store changes
In GitHub Desktop: File → Clone repository
(CTRL + SHIFT + O)
Choose the following repo to clone:
WWS-TEST-example-repo
Set the local path to
C:\Users\your username\Documents
Hit Clone
Find the repository you just cloned in file explorer. Check out the folders and files in the repository.
Find a partner.
Partner 1:
Create a new repository branch with the name:
your name-their name
Go to GitHub Desktop
Click the Current branch button
Click New Branch
Publish the branch so your partner can see the branch by hitting
the Publish branch button in the top right.
Partner 2:
Pull the repository using the
Pull origin button in the top right of GitHub desktop to
get the new branch.
Check out the repository by hitting the middle
Current branch button in GitHub Desktop and selecting the
one with your names.
This is where you’ll be working for the rest of the Github Activities. The changes you make to this branch will not affect anyone else’s files unless they are also on this branch.
R Projects make your data/code portable and keep file paths consistent across computers.
Open up code/example-script1.R
Edit line 3 with your username then run lines 4-5 to get the file path to the .csv file.
Notice this is a long file path to have to type, and is unique to the user so this isn’t ideal for sharing code across the project.
Let’s fix this by turning the repo into an R project by running the following code in the R console:
#install.package("usethis") #only need to run the first time
library(usethis)
#the location you of your project folder
repo <- file.path(fs::path_home(), "Documents/WWS-Node1-TEST-example-repo/")
#create an R project from the directory
usethis::create_project(repo)The new R project should open up in a new R window. In this
project go back to code/example-script1.R and change the
file path to data/example-csv.csv. Run the rest of the
lines. What happens?
HINT: In the project on the lower right of R Studio, you will see all your files in the repository, you can use this to open up
example-code1.R.
Because the repository is now an R project, it will automatically
treat WWS-TEST-example-repo as the working directory and
base all file paths from here. Meaning that the file paths will work for
anyone who clones the repository.
We changed some files, let’s go see the changes in GitHub Desktop. On the left it shows the files that have been added, changed, or removed in your repo. You should see four:
.gitignoreexample-code1.Rexample-csv.csvWWS-TEST-example-repo.RprojHINT: If you don’t see example-code1.R, make sure the script is saved.
What is the .gitignore file? It tells Git which
files/folders not to track.
Open up the file via file explorer, right now it only has one line:
There are many of other temporary files we may not want to include in our repository (ie. lock files for Office files). The easiest way to ignore all of these files is to use gitignore.io.
createWe’ve now made some changes to the files in this project, will others see them? Go check out the project on GitHub, do you see the changes you’ve made?
Go back to the project in GitHub Desktop, on the left side you can see the files that have been changed. Select all the files.
Add a commit message:
Commit your changes.
Go back to the repository on GitHub, do you see your changes?
Your changes are tracked locally by Git after you make a commit, but in order to share your commits with others, you need to push those changes to the remote (GitHub).
Partner 1:
In GitHub Desktop, push the Push origin button in
the upper right to send your changes to GitHub.
Go back to the repo on GitHub, do you see your changes now?
Partner 2:
Without pulling your partner’s changes, push your changes.
You’ll get a warning about needing to fetch changes, go ahead and
click Fetch, then Pull origin. This will
attempt to reconcile your changes with the changes you just pulled.
Uh Oh! We’ve got a merge conflict! This means that Git has two different versions of the same file and it doesn’t know which one to keep.
This is why it’s always best to pull changes right before you push changes, to avoid merge conflicts. We’ll practice that again later.
When you have a merge conflict you’ll have to manually tell Git which version of the file you want to keep. You can do that in two ways. We’ll explore using both ways:
Merge in GitHub Desktop:
.Rproj file.Merge Manually:
GitHub Desktop also gives you the option to open in the default
program, click that option on the .csv file.
Take a look at the file, take note of the conflict markers, which show the conflicting file lines:
<<<<<<< HEAD
your local version
=======
remote version
>>>>>>> branch-name
To resolve the conflict we can choose to delete the lines for one version, or keep both. Lets keep both, so we’ll just remove the conflict markers (above) and save the file.
Go back to GitHub Desktop, you should now see that your merge conflicts are all resolved, so now we can continue the merge:
Check out the history of your commits, you should now see your initial commit and a commit from merging the commits.
Push both to GitHub.
GitHub was originally designed for coding and works really well with
file that can be opened in text editor like .txt,
.csv, .R, .py. However, in our
project you may find yourself working with binary files
(i.e. .docx, .pdf, .xlsx).
Let’s see how Git treats these different kinds of files by making some changes:
HINT: We’re about to make changes to files in our repo, pull the current version to avoid merge conflicts!
Open up data/example-code2.R in RStudio.
In your merge conflict, we decided to keep both sets of data, lets use those as groups in our plot by adding the following code after line 5:
data$X <- c(rep("Group 1", 4), rep("Group 2", 4))Then edit line 7 (previously line 6) to look like this:
ggplot(data, aes(x=site, y=value, color=X)) + geom_point() + geom_line()Run the whole code to create a new figure 1. Make sure to save
your changes to data/example-code2.R.
Next, open methods/example-SOP.docx, on a new line
type: Written by <your name>. Then close and save the
file.
Open up GitHub Desktop to look at the how it displays the changes you made. On the left you should see three changed files, click on each and see what is displayed on the right.
example-code2.R: notice that it shows the line by
line changes that were made?
Green and + indicate a line was added
Red and - indicate a line was removed
figure1.png: notice that it shows what the previous
figure looked like and what it looks like now. At the top you can also
choose additional options to compare the two figures.
example-SOP.docx: we just get a message that the
binary file has changed. Git doesn’t natively know how to reconcile
changes in binary files, making them more challenging to work on
collaboratively through Git.
IMPORTANT: Merge conflicts are also more challenging with binary files. If you choose to keep the remote version during a conflict, you’ll lose your local work, so merge carefully!
Great, we’ve made new changes to our files, so let’s share them again.
Partner 2:
Commit your changes, practice writing a descriptive commit message
In GitHub Desktop, push the Push origin button in
the upper right to send your changes to GitHub.
Partner 1:
We’re going to use best practices and pull the changes from the branch before we make a commit so we have the most recent version.
Uh Oh! We get an error, that pulling those changes will overwrite some of our changes.
It gives us the option to stash our changes and continue with the pull. This means that it will temporarily save your current changes in a separate location so we can pull without a merge conflict. Click to stash your changes.
Great, now we have the most recent copy of the repo, but where did our changes go? Notice right above the commit message, there’s a bar that says stashed changes. Clicking this will show all those changes you made, and you can click restore to bring them back to your changes.
Alright, now the copies of our files are up to date with the repo, and we’ve still got our changes. Now we can commit and push to GitHub without merge conflicts!
When you do this, it’s a good idea to check out the new commit messages and file changes to make sure you don’t write over others work.
Git will combine your work on non-binary file, but when you push, your version of the binary files will be the one on the repo.
IMPORTANT: These are the main GitHub workflows you should use. Other Git commands exist, but using them without guidance can cause problems for the whole repository. If you’re unsure or run into an issue, stop and ask Katie, we’ll work through it together.