Version control with Git and GitHub¶
Version control is crucial for collaborative coding and tracking changes in your projects. This page covers how to set up and use Git and GitHub, including how to work with branches and pull requests for effective collaboration.
New to Git?
If you've never used Git before, start with the Software Carpentry Git lesson or the beginner-friendly GitHub guide.
1. Install Git¶
- Download the installer from the Git website.
- Follow the installation wizard, using default options.
- Install via Homebrew:
- Alternatively, download the Git installer.
2. Configure Git¶
Set up your Git identity using the following commands:
3. Getting started with GitHub¶
- Download GitHub Desktop.
- Sign in with your GitHub account.
- Clone a Repository:
- Go to
File > Clone Repositoryand enter the repository URL.
- Go to
- Commit Changes:
- Make changes to files, then click
Committo save a snapshot of your changes.
- Make changes to files, then click
- Push to GitHub:
- After committing, click
Pushto sync changes with GitHub.
- After committing, click
4. Working with branches¶
Why use branches?¶
The main branch contains the stable, "official" version of a project. You should never commit directly to main. Instead, create a branch — an independent copy of the codebase where you can make changes without affecting anyone else's work. This way:
- You can experiment freely without breaking the main version.
- Multiple people can work on different features at the same time.
- Changes are reviewed before they are merged, catching mistakes early.
Creating and switching branches¶
- Create a new branch: Click the
Current Branchdropdown at the top → clickNew Branch→ give it a descriptive name (e.g.,fix/update-fmri-docs) → clickCreate Branch. - Switch between branches: Click the
Current Branchdropdown and select the branch you want to work on. GitHub Desktop will update all the files on your computer to match that branch. - Publish the branch: The first time you switch to a new branch, click
Publish branchto push it to GitHub so others can see it.
Branch naming
Use descriptive names that indicate what the branch is for. Common prefixes:
fix/— for bug fixes (e.g.,fix/broken-link-fmri)feature/orfeat/— for new features (e.g.,feature/add-eeg-tutorial)improve/— for improvements (e.g.,improve/restructure-coding-page)docs/— for documentation changes (e.g.,docs/update-ethics-info)
Working on a branch¶
Once you are on your branch, the workflow is the same as usual — edit files, stage, commit, and push:
- Make your edits to files as normal.
- In GitHub Desktop, you will see the changed files listed on the left.
- Write a commit message at the bottom-left and click
Commit to <branch-name>. - Click
Push originto send your commits to GitHub.
Keeping your branch up to date¶
If others have made changes to main while you were working on your branch, you should pull those changes into your branch to stay up to date and avoid conflicts later:
- Switch to
main(clickCurrent Branch→ selectmain). - Click
Fetch originand thenPull originto get the latest changes. - Switch back to your branch.
- Go to
Branch > Update from main(orBranch > Merge into current branch→ selectmain). This brings the latestmainchanges into your branch.
Collaborating on a branch¶
Multiple people can work on the same branch. To pick up a colleague's branch that already exists on GitHub:
Before starting work on a shared branch, always pull first to get your colleague's latest changes:
5. Pull requests¶
What is a pull request?¶
A pull request (PR) is a request to merge the changes from your branch into main. It is the standard way to propose changes in a collaborative project. A PR lets others:
- See exactly what you changed (added, modified, or deleted).
- Review your work — leave comments, suggest edits, or approve.
- Discuss any questions before the changes go live.
Once the PR is approved, the branch is merged into main and typically deleted.
Opening a pull request¶
After pushing your branch, GitHub Desktop will show a banner: Create Pull Request. Click it, and it will open the PR form on GitHub in your browser.
- Go to the repository on GitHub.
- If you recently pushed a branch, you will see a yellow banner:
Compare & pull request. Click it. - Alternatively, go to the
Pull requeststab →New pull request→ select your branch.
If you have the GitHub CLI installed:
When filling in the PR form:
- Title: Write a short, clear summary of what the PR does (e.g., "Update fMRI scanning procedure").
- Description: Explain what you changed and why. If the PR resolves a GitHub Issue, write
Closes #123in the description — this will automatically close the Issue when the PR is merged. - Reviewer: Assign a reviewer (someone who will check your work before it is merged).
Resolving a GitHub Issue with a PR¶
GitHub Issues are used to track tasks, bugs, and suggestions. To resolve an issue:
- Open the Issue on GitHub and read what needs to be done.
- Create a branch (see above) with a name that references the issue (e.g.,
fix/issue-42-broken-links). - Make your changes on that branch and push them.
- Open a PR and include
Closes #42(orFixes #42) in the PR description. - When the PR is merged, the Issue is automatically closed.
Reviewing a pull request¶
If you are asked to review a PR:
- Go to the
Pull requeststab on GitHub and open the PR. - Click on the
Files changedtab to see all modifications. - Leave comments on specific lines by clicking the
+icon next to a line. - When done, click
Review changesand choose:- Approve — if everything looks good.
- Request changes — if something needs to be fixed before merging.
After the PR is merged¶
Once a PR is approved and merged:
- The branch is typically deleted on GitHub (you can do this from the PR page).
- Switch back to
mainand pull the latest changes to get the merged updates locally:
6. General tips¶
- Pull before you start working to avoid conflicts.
- Commit often, but meaningfully — each commit should represent a logical unit of work.
- Never commit directly to
main— always use a branch and a PR. - Write clear commit messages that explain what changed and why.
- Keep PRs focused — one PR per feature or fix. Avoid bundling unrelated changes.