Gitting serious about version control
This is a post about a housekeeping activity that keeps your coding projects reproducible and collaboration friendly. Automating version control may be an unglamourous activity, but it will prevent your team from sinking into a swamp of messy code.
The story of connecting to a remote GitHub repository begins with a segue into the Secure Shell (SSH) protocol. SSH is a cryptographic network protocol that enables secure communication, such as file transfers and remote device management, between a local machine and a remote host over an open network.
SSH uses public-private key pairs to authenticate hosts to server communication. This involves:
Note: Using SSH keys allows you to connect to GitHub without having to supply your username and password each time you push to GitHub.
Tools > Global Options > Git/SVN
and check whether you have an existing SSH RSA key.Create RSA key
. RStudio will create an SSH key pair and store your identification and public key in the files ~/.ssh/id_rsa
and ~/.ssh/id_rsa.pub
respectively.Apply
and OK
to close the Global Options
window.Note: You should always protect your private key with a passphrase as an additional layer of security. RStudio will also automatically add your key to the SSH agent, which saves you from entering your passphrase every time you use your SSH key.
Tools > Global Options > Git/SVN
and click View public key
to copy your public key to clipboard (or an open text file). You may need to restart RStudio for your SSH key pair to be detected.Settings > SSH and GPG keys
. Click New SSH key
and paste your previously copied public key into the box.2021-lenovo-lt
and create your new SSH key. Congratulations! You have now connected to your remote GitHub repositories via RStudio.Git
tab in RStudio, which is localised next to the Environment
and History
tabs. New file modifications will be marked by M
or ?
icons to denote modified file changes or newly added i.e. untracked files respectively.Note: New file modifications will only be marked after your file changes are saved.
Commit
. This will take you to a new window where you can review all file changes. Code addition is marked in green and code removal is marked in red. Track your new commit with a message describing your latest changes and press Commit
.Git
tab. To update your remote GitHub repository, press the green Push
arrow to push your new commits into GitHub. Congratulations, you have successfully synced to your remote GitHub repository and made a new update!According to the Happy Git and GitHub guide, the easiest way to sync to a new GitHub repository is to manually create and then clone your new repository from https://GitHub.com/. Doing this also establishes your remote connection, which allows you to immediately push or pull from your GitHub repository.
File > New Project > Version Control > Git
and paste your SSH URL as the Respository URL
. Doing so should automatically populate the name of your project directory (which should be the same name as your GitHub repository). Select a local location for your new project and create your project. I recommend creating a folder which houses all your local git repositories and always using this as your new project location.Note: This step is equivalent to typing git clone <SSH URL>
on the command line. The first time I tried to clone a new Github repository using RStudio, I received a fatal error message that RStudio could not read from the remote repository. I resolved this by cloning the remote repository from the command line, which allowed me to continue even though the authenticity of host ‘github.com’ could not be established. Doing so permanently added ‘github.com’ (RSA) to my list of known hosts and resolved the error.
.Rproj
file stored inside your local repository.