Working with GitHub in VS Code

GitHub Repository extension open repository dropdown

Git vs. GitHub: What’s the Difference?

To better understand these two keywords, let’s first look at a term you will always come across while working with Git –Version Control. Think of version control as a savings program for your project. When you are developing something, say a webpage, the version control program will log all the changes you make to your code and even enables you to restore the code to earlier versions in case of an error. Take a look at the image below:

version control

We have the current code on the right side, and the left side is a previous version of the same code. I can decide whether I want to remain with the current or previous version or even go ahead and merge the changes. Now that you have a clear understanding of version control let’s look at Git and Github.

Git is one of the most popular version control systems used by most developers today to manage their projects. What makes it much more reliable it is installed in your local system rather than using the cloud. It is available for Windows, Linux, and macOS. One more exciting feature about Git that sets it apart from other version control systems is the support for Branching. With branching, you can create independent local branches for your project, enabling you to test new ideas and a production branch. You can have as many branches as you want.

Github is different from Git, but they work together to achieve the same thing. While Git is installed in your local system, Github works on the cloud. It’s an online database that keeps track of your version-controlled projects and allows sharing outside your local computer. Github also offers you a web interface to manage your projects if you are not a ‘commands person.’ With that said, let’s dive in and get started with Git on Raspberry Pi.

Requirements

These items are enough if you are planning to access your Pi remotely via SSH or VNC server. You can check out our post, which will give you a detailed guide on How to SSH into Raspberry Pi or How to Set up VNC Server on Raspberry Pi. If you want to have a full desktop setup with a monitor, you will require the items below:

Editor integration

Hovers

User Hover

Issue Hover

Suggestions

User suggestions are triggered by the “@” character and issue suggestions are triggered by the “#” character. Suggestions are available in the editor and in the Source Control view’s input box.

You can also configure which files show these suggestions using the settings GitHub Issues: Ignore Completion Trigger ( githubIssues.ignoreCompletionTrigger ) and GitHub Issues: Ignore User Completion Trigger ( githubIssues.ignoreUserCompletionTrigger ). These settings take an array of language identifiers to specify the file types.

Pull requests

Pull Request View

Creating Pull Requests

Once you have committed changes to your fork or branch, you can use the GitHub Pull Requests: Create Pull Request command or the Create Pull Request button in the Pull Requests view to create a pull request.

A new Create Pull Request view will be displayed where you can select the repository and branch you’d like your pull request to target as well as fill in details such as the title, description, and whether it is a draft PR. If your repository has a pull request template, this will automatically be used for the description.

Create Pull Request view

Once you select Create, if you have not already pushed your branch to a GitHub remote, the extension will ask if you’d like to publish the branch and provides a dropdown to select the specific remote.

The Create Pull Request view now enters Review Mode, where you can review the details of the PR, add comments, reviewers, and labels, and merge the PR once it’s ready.

Reviewing

Pull requests can be reviewed from the Pull Requests view. You can assign reviewers and labels, add comments, approve, close, and merge all from the pull request Description.

Pull Request Description editor

From the Description page, you can also easily checkout the pull request locally using the Checkout button. This will switch VS Code to open the fork and branch of the pull request (visible in the Status bar) in Review Mode and add a new Changes in Pull Request view from which you can view diffs of the current changes as well as all commits and the changes within these commits. Files that have been commented on are decorated with a diamond icon. To view the file on disk, you can use the Open File inline action.

Changes in Pull Request view

The diff editors from this view use the local file, so file navigation, IntelliSense, and editing work as normal. You can add comments within the editor on these diffs. Both adding single comments and creating a whole review is supported.

Connect RStudio and GitHub

Sign up for GitHub

Create a Personal Access Token (PAT) on GitHub

Once you’ve signed up, you’ll need to enable RStudio to talk to GitHub. The process for doing so has recently changed (this is where I see the largest major difference from Happy Git with R). The best way to connect RStudio and GitHub is using your username and a Personal Access Token (PAT). To generate a personal access token, use the create_github_token() function from usethis . This will take you to the appropriate page on the GitHub website, where you’ll give your token a name and copy it (don’t lose it because it will never appear again!).

Store Personal Access Token to Connect RStudio and GitHub

Now that you’ve created a Personal Access Token, we need to store it so that RStudio can access it and know to connect to your GitHub account. The gitcreds_set() function from the gitcreds package will help you here. You’ll enter your GitHub username and the Personal Access Token as your password (NOT your GitHub password, as I initially thought). Once you’ve done all of this, you have connected RStudio to GitHub!

How to Connect RStudio Projects with GitHub Repositories

Now that we’ve connected RStudio and GitHub, let’s discuss how to make the two work together. The basic idea is that you’ll set up projects you create in RStudio with associated GitHub repositories. Each RStudio project lives in a single GitHub repo.

RStudio First

Sometimes you already have a project locally and you want to get it on GitHub. To do this, you’ll need to first use the use_git() function from usethis , as we did above. Then, you can use the use_github() function, which will create a GitHub repo and connect it to your current RStudio project.

GitHub First

The most straightforward way to use RStudio and GitHub together is to create a repo on GitHub first. Create the repo, then when you start a new project in RStudio, use the version control option, enter your repo URL, and you’re good to go.

Resource:

https://singleboardbytes.com/649/use-github-raspberry-pi.htm
https://code.visualstudio.com/docs/editor/github
https://rfortherestofus.com/2021/02/how-to-use-git-github-with-r/

Leave a Comment