Git

software
version control
Allows you to keep track of changes to documents, and also to collaborate and work together
Author

Alexander Weber

Published

February 13, 2025

Modified

February 13, 2025

Intro

A beginner’s guide: link

What is Version Control?

Git version control is an excellent application that allows you to keep track of changes to documents, and also to collaborate and work together. We use it a lot in the lab to save our code, share our code publicly with the science community, and to collaborate on projects.

Software Carpentry has a good tutorial: Version Control with Git

Install

Install guide

Github

Git and Github are two different things that sometimes get confused for each other. Basically, Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service (owned by Microsoft) that lets you manage Git repositories. Alternatives to Github include: GitLab, Bitbucket, Codebase, and more.

BCCHR’s github is located at https://github.com/BCCH-MRI-Research-Facility/

If you don’t yet have a github.com account, you should go there and create one.

Github often uses Markdown in order to facility writing well organized files. A cheatsheet for Markdown syntax can be found here

Connecting to Github with SSH

If you don't want to have to put your username and password in every time:

No Password Guide

Basically, you will create a SSH key (or use an existing one):

ssh-keygen -t ed25519 -C "your_email@example.com"

Then you will run:

gh ssh-key add ~/.ssh/id_ed25519.pub

Note, you will need to install gh, and run gh auth login

For gh auth login, pick:

  • GitHub.com
  • SSH
  • your key you want to upload
  • Login with a web browser

Useful Git Commands

Start a repository

Create a folder, enter the folder, then type:

git init .

Now, head to your Github account and create a new repo

Name your new repo and click Create New Repository

Now, in the command line type:

echo "# Title" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:WeberLab/Title.git
git push -u origin main

Where Title is the name of your new repo

Update a repository

Check status of remote origin (if any changes have been made on the Github):

git remote update
git pull

Clone a repository

You can clone a remote repository to create a local repository on your computer

For example,

git clone https://github.com/<YourGithubAccountName>/<YourRepoName>

Push to the repository

You can push any changes you make on your local repository to the remote repository. Make sure your current working directory is your local repository, then use the following commands:

git add <filename>
git commit -m "Comment about what you added"
git push origin <branchname>

Branches

You can create branches to add or modify features of the master code.
To create a new branch, use command:

git checkout -b <branchname>

Then you can add, commit and push to that branch as specified above. Note: use <branchname> main if you want to commit to main.

You can switch between branches using the command:

git checkout <branchname>

You can check what branch you are in using the command:

git branch

Since branches are such an important topic, check out this website which is an excellent resource to understand them more intuitively: https://learngitbranching.js.org/

Making a GitHub Website with RStudio and GitHub Pages

https://resources.github.com/whitepapers/github-and-rstudio/

Git Large File Storage

[Git Large File Storage https://git-lfs.github.com/ ] can be used to store and retrieve large files.
If you've pulled a repo and the files seem incredibly small for what they should be, try:

git lfs pull
Back to top