NMFS Openscapes | Wiki | NMFS R User Group |
This week I will cover key GitHub features and a few more Git skills.
A few more Git skills
Key GitHub.com skills.
Getting started collaborating using Git and GitHub
As I mentioned in week 1, you should get familiar with the 4 basic Git skills before you start working with branches. You may never need to use branches, but there is one very common use of branches: development branch. If you are using your GitHub repository to delivery a product like an R package, the main branch needs to stay clean and functional. In this case, you will need a development branch. Unless you use releases.
Show me w GitHub Desktop - Show me from the shell - Show me from RStudio - Show me from VS Code
Show me w GitHub Desktop - Show me from the shell - Show me from RStudio - Show me from VS Code
The skills above are simple, why do branches causes beginners to hate Git?
The default behavior is that any changes you have not committed will follow you when you change branches. This will through you for a loop if you don’t know about this. GitHub Desktop will alert you and default is that these changes do not follow, but in every other GUI, they follow without warning.
Show me in RStudio/shell - Show me in GitHub Desktop
This approach to branches will lead to headaches. Better to be more methodical and start then finish tasks (code changes) and then delete branches.
git checkout branchname filepath
e.g. from main branchgit checkout development ./hello.R
would replace the file hello.R
on the main branch with
the current version of hello.R
on the development branch.
Note the ./
in the path. Confession, the following
achieves the exact same effect and is 5x faster than trying to remember
checkout
syntax: switch to the branch, copy the contents of
hello.R
, switch back to main branch, and paste in what you
just copied.
Say you made a change and you need to get rid of that. The temptation
is to jump onto the Git command line and clobber the repository with
reset
and revert
commands. Don’t do
this. Here are some strategies that will make this let prone to
leaving your code a mess.
No? Easy click on the file in GitHub Desktop, right click and click ‘Discard Changes…’. Note this will take things all the way back to your last commit!! If you have been making a bunch of changes without committing those, then you are out of luck.
Show me in GitHub Desktop - Show me in VS Code - Show me in RStudio/shell
Yes? Go to History in the GitHub Desktop window, click on the commit and click ‘Revert’. This will get rid of all the changes that went with that commit. So if you changed multiple files, all those files will be reverted. If you have pushed the changes to GitHub, then you can push the revert and it’ll show up on GitHub too.
But I need to revert multiple commits! Work backwards one by one.
Why not use git reset HEAD~1
or similar? You can. Search
Google and you will find many ways to to that. But I warn you that
git reset
is the path to many frustrating hours with Git
(see the many StackOverflow questions).
Let’s say you made a big multi-file commit and you want to revert one file.
You can do this at the Git command line, but I find that to be a huge time suck and in my early Git days, I sometimes left my repository with a horrible problem that I could not fix and had to completely rebuild my repo. Since I don’t need to be a Git wizard, this is what I do when I want to go ‘back in time’ for a single file.
Assuming you have already pushed the changes up to GitHub.
< >
to browse your repo at
the state in time where your file was ok.If you have not pushed the changes up to GitHub.
Ok, here’s the Git command to get a single file back. This works whether or not you have pushed to GitHub. The problem with this and why I don’t do it is that I usually need to look at the file. So I am scrolling back through the status of my repo in the past until I find the status that I want. Then I stare a bit and think and think. Then get a coffee and think some more. Then I scroll back through the status of the repo in the past some more and THEN I do the copy and paste. It is rarely the case that I know exactly what commit that I need to get rid of—and even rarer that I want to go completely to a status in the past.
git checkout SHA~1 -- ./<file name>
For examplegit checkout 1d0f8c2eb4e66db0a7123588ae2fad26a6338303~1 -- ./R/test.R
would reset test.R to one before that commit. This part
1d0f8c2eb4e66db0a7123588ae2fad26a6338303
is the bad commit
hash and this part ~1
means what the file was like 1 commit
before that.If you accidentally leave off the file name and Git says you have a
detached head, use git checkout master
or
git checkout main
to reattach your head.
GitHub Desktop makes resolving these pretty easy.
hello.R
and show where
the conflicts are. You then edit hello.R
in RStudio to fix
the conflicts.origin/main
) or on your local
machine (main
).hello.R
are still there.
hello.R
and fix the conflict. Git won’t have
marked it so it might be hard to find.Those using Git in RStudio Merge conflicts are a bit of a disaster in RStudio, and RStudio gives no warning before it mucks up your files. So it you are pushing/pulling from RStudio be sure to practice on some toy merge conflicts before you run into a real one.
git reset
git checkout
git branch
And the commands that will help you
git status
git log
git reflog
Pro tip checkout the state of the repository at the time of a release. From the terminal:
git checkout v1.0
When done:
git switch -
Warning git checkout ...
will change all your
time stamps. Might be better to just do this from GitHub…
I use this to organize collections of repositories.
Example from my work:
We’ll work with project boards more in Week 3 and 4 when we learn about project management with Kanban and Scrum.
You may have heard about them. Let’s see it in action. We make a GitHub Action that will update our Readme file whenever a relevant change happens. We’ll see a bigger example next week with RMarkdown reports.
To set up our action:
render-readme.yml
with the instructions
for what to do to make the Readme.md file.Our yml file has a set of instructions to the server that is going to do the work.
on:
push:
paths:
- README.Rmd
- test.csv
name: Render README
jobs:
render:
name: Render README
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-pandoc@v1
- name: Install packages
run: Rscript -e 'install.packages(c("rmarkdown", "knitr"))'
- name: Render README
run: Rscript -e 'rmarkdown::render("README.Rmd", output_format = "md_document")'
- name: Commit results
run: |
git commit README.md -m 'Re-build README.Rmd' || echo "No changes to commit"
git push origin || echo "No changes to commit"