Appendix B — GitHub
If you’re new to GitHub, the steps below will walk you through setting up Git and GitHub from RStudio using the Git pane. The initial files in the project are below:
B.1 Example Shiny project
Let’s assume we’ve just created projApp
, a new Shiny application without the package structure (it was initially created using the New Project Wizard with a Git repo initialized).
The files in projApp
are below:
projApp/
├── app.R
└── projApp.Rproj
1 directory, 2 files
After creating the project, we’ll head over to GitHub and create am empty repo with the same name. We’ll see the following options:
We’re interested in the second option, “…push an existing repository from the command line”. One option is to copy the Git commands and enter them into the Terminal pane in Posit workbench, but we’re going to use Posit Workbench’s Git pane.
B.2 Committing changes
We’ll commit these initial files to the repo using the Commit icon in the Git pane (each file initially has a yellow question mark icon):
After selecting each file, the icon turns to a blue ‘A’ (which means the file or change has been ‘added’ to the repo)
We’ll add a brief commit message and click Commit. This is the equivalent of entering the following commands in the Terminal:
git add .
git commit -m 'first commit'
Review the output from the commit.
.gitignore
, app.R
, and sap.Rproj
files have been committed to main
The output tells us the contents of projApp
are part of our local main
branch. Now we need to make sure the local branch has a remote on GitHub at the following URL: https://github.com/<username>/sap.git
.
B.3 New branch, add remote
Click the New Branch icon in the Git pane and create a new main
branch. Then click on Add Remote… and name the remote origin
.
The Remote URL is the link from the Quick Setup above.
main
branch to track origin
After clicking Add and Create, you’ll be asked to checkout or overwrite the existing main
branch. In this case, we can select Overwrite (because we’re already on the main
branch).
B.4 Push a local branch to remote (and set branch to track remote branch)
The git push -u origin main
commands tell Git to “push the main
branch to the origin
remote, and also set the local main
branch to track the main
branch on origin
.”
git push
: used to push commits from your local repository to a remote repository.origin
: name of the remote repository you want to push to. When youclone
a repo oradd
a remote usinggit remote add
, it’s common to name themain
remoteorigin
(though it could technically be any name).main
: name of the branch you’re pushing to the remote repository.-u or --set-upstream
: When this option is used, it sets a tracking relationship between the local and upstream remote branches. This means that in the future, usinggit pull
orgit push
doesn’t require specifying the remote or branch (Git will know you’re referring to theorigin
/main
branch).
B.5 Renaming branches
The Git UI above called the git branch -B main
commands, so we’ll break these down below:
git branch
without any arguments would list all the local branches in the current repository. But, with certain options (like-M
), you can perform other branch-related operations.-M
: This option stands for ‘move/rename’ and forcibly renames the branch. If a branch namedmain
already exists, it will be overwritten because of the forceful nature of the-M
option. If you want to avoid accidentally overwriting an existing branch, you could use-m
(lowercase) instead. The lowercase-m
will rename only if the target name doesn’t already exist.main
: This is the new name for the currently checked-out branch.
The complete workflow for setting up Git from the command line is below:
# make and add changes
git add .
# commit changes
git commit 'first commit'
# set remote on GitHub
git remote add origin https://github.com/<username>/<repo>.git
# rename the current branch to main
git branch -M main
# push and set upstream to origin (remote)
git push -u origin main