maybe you already know other ways to share code?
the result is that git comes with a large resource base and is compatible with all the major operating systems.
github’s mascot and logo is the octocat, a creature with five octopus like arms. the octocat character was designed by the same graphic designer, simon oxley, who designed the bird logo that twitter uses.
read more about github: https://en.wikipedia.org/wiki/github
in RStudio you can open up a terminal in a window next to the R Console:
sign up at https://github.com/login
some advice from happy git with r:
the form for creating an account on github
we recommend setting up ssh (secure shell) key based authentication with github.
this allows your computer to be automatically authenticated when you communicate with github.
follow the instructions here:
https://happygitwithr.com/ssh-keys.html
nativate to file → new project
select “create a git repository” which informs rstudio you want to use git.
with the new project wizard window in rstudio, you can specify the directory dame for your project, create your project as a subdirectory of another folder, and use the provided checkbox to indicate that you’d like to initialize the project as a git repository.
rstudio adds a git tab in your environment/history panel.
this panel is a point-and-click interface to:
the git panel in rstudio shows changes you’ve made and lets you see the changes you’ve made, commit them, and push them to github in a user-friendly interface.
navigate to github.com → login → new repository
and fill out the form.
# let your local repository know about the remote repository
git remote add origin git@github.com:ctesta01/examplerepository.git
git branch -m main # use "main" as the default branch
git add .gitignore # add a file to the staging area
git commit -m "initial commit" # name your commit
git push -u origin main # push your commit to the remote repository
these are the above commands being run in the terminal, along with the output produced. you can see that git
will report on how many objects and how many bytes are being uploaded.
a readme serves as an introduction to and documentation for your repository.
like any documentation, feel free to start small and document as you develop!
you can learn more about reamdes from github here: docs.github.com/…
read the section about readmes in the r packages book here: https://r-pkgs.org/other-markdown.html#sec-readme
the basic workflow for making updates to a git repository is done in three steps:
this figure from happy git with r shows examples of commits made in a sequence. each commit is accompanied by an id, a message, and the differences between two commits are referred to as a “diff”.
git status
is a basic command that displays the current state of the working directory.
it’s a good idea to always run git status
before changing your code because there may be something you want to commit or address first.
the rstudio git panel displays most of what is displayed in the output of git status
.
git status
from the terminal
the git panel in rstudio showing new, untracked files
terminal version:
git add readme.md
adds any changes to the readme.md
(including its creation) to the staging area. git status
shows us what changes are staged to be included in the next commit.
one can add files to the git staging area in the rstudio git panel just by checking the checkbox next to each file in the staged column.
terminal version:
git commit -m 'add new readme'
creates a commit message (or basically a name) for the set of changes that were on the staging area and bundles them up together as a commit.
if you need to write a longer, multi-line commit, git commit
will open your default terminal based text-editor (often one of nano, vi, or vim) where you can write a longer commit message and save it.
with the changes you want to make staged, click the commit button. you’ll have a chance then to view what changes you’ve made. when you’re sure you want to commit, you can write a commit message, click commit, and then push.
to develop thoughtful commits, consider the following:
see more advice here:
https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/
a changelog is a file that contains a curated, informative history of your project’s updates.
terminal version:
Calling git push
on the terminal sends the commits from our local computer to the remote server.
After you click Commit, a window will open showing you the git
command that RStudio ran to create your commit. If there are no errors, you can close the window and then click Push.
git diff
shows how files differ between their current state and a different version.terminal version:
git diff
on the terminal will print (in highlighted colors) what has been added or removed from the referenced file since the last commit.
in the rstudio git panel you can click on the diff button and it will pop open a window showing the changes that have been made to the file since the last commit.
terminal version:
calling git pull
on the terminal will download any commits from the remote server and apply them to your codebase. you may get hints from git
that you can be more explicit about your git
configuration as to how you’d like to apply commits if there are conflicts.
in the rstudio git panel you can click on the pull button and it will pop open a window showing the changes that have been made to the file as a result of pulling any commits from the remote server.
you can use the git clone
command to copy a repository from a remote server (like GitHub) onto your computer.
git clone git@github.com:ctesta01/ExampleRepository.git
or
git clone https://github.com/ctesta01/ExampleRepository.git
In the centralized workflow depicted, all developers synchronize their work through a main shared repository (like one on GitHub).
terminal version:
git checkout -b new_branch
creates a new branch named new_branch
that diverges from the most recent commit on whichever branch one was on, in this case the main
branch.
In the RStudio Git panel you can click on the New Branch button (which sometimes appears as just an icon to the left of the current branch name) and it will pop open a window allowing you to specify what you’d like to name your branch.
More helpful advice:
https://github.blog/2015-01-21-how-to-write-the-perfect-pull-request/
GitHub automatically will create a Compare & Pull Request button when it sees that you’ve pushed changes onto a new branch. If you click that, it will open up a form where you can document why you’re submitting a pull request.
I made a Git cheat sheet!đź’ś pic.twitter.com/CfHVLmrxhw
— Elmira 💜 (@elmiracodes) November 15, 2021
git add
)git commit
)git push
), and continue making commits until the project is finished.Some helpful resources:
image from https://www.youtube.com/watch?v=wcx6SH8Oqhs