7 Committing and Pushing to GitHub
In this chapter, we will initialize a local Git repository, add and commit the files of our package to the repository, and push our package to GitHub.
7.1 .gitignore File
The .gitignore
file specifies which files and directories should be
ignored by Git. This is useful for excluding files that are not relevant
to your project, such as temporary files or sensitive information. It is
a good practice to add the following files to your .gitignore
:
.DS_Store
.Rhistory
.RData
.Ruserdata
To modify the .gitignore
file, click on it in the bottom-right pane of
RStudio, add the entries listed above, and then click the save icon.
Please see my animated example below:
7.2 Initializing Git
To initialize a Git repository for your package, you can use the
usethis::use_git()
function. This function sets up Git for your
package and creates an initial commit.
Run the following command in your R console:
When prompted for permission to commit files, say “Absolutely not”, as committing at this stage can interfere with the next steps. If asked whether to restart your R session, answer “Yeah”.
Please see my animated example below:
7.3 Committing Files
After initializing Git, you need to commit your files manually. In RStudio, follow these steps:
- In the top-right pane, go to the “Git” tab.
- Select all the files you want to commit by clicking the checkboxes next to them.
- Click the “Commit” button.
- Write a commit message, such as “Initial commit”.
- Click the “Commit” button in the commit dialog box.
This creates an initial commit with all your package files.
Please see my animated example below:
7.4 Changing the Default Branch
In recent years, it has become customary to use “main” instead of “master” as the default branch name for new repositories due to the inclusive language initiatives across the tech community. Additionally, using “main” aligns better with GitHub’s default settings, facilitating smoother integrations.
To change the branch name in your local repository to “main,” you can use the terminal within RStudio. Here’s how:
- Open the Terminal tab next to the Console tab in RStudio.
- Type the following command to rename the branch:
Please see my animated example below:
7.5 Pushing to GitHub
Once you have committed your files, you can push your package to GitHub
using the usethis::use_github()
function. This function creates a new
GitHub repository and pushes your local Git repository to GitHub.
Run the following command in your R console:
Follow the prompts to complete the process. This will create a new repository on GitHub and push your local commits to the remote repository.
If prompted for permission to commit any further files at this point, say “Negative”.
Please see my animated example below:
You have now successfully committed and pushed your package to GitHub!
In the following chapter, we will start working on special files such as DESCRIPTION
, README.md
, and others, which are essential for documenting and detailing your package.
Creating R Packages: A Step-by-Step Guide by Ville Langén is licensed under CC BY-SA 4.0