ETC5513: Collaborative and Reproducible Practices
Workshop 4
🎯 Objectives
- Create repositories and syncrhonize them between your local and remote repos
- Create a Quarto report with a tabell, figures and sections that are labelled and referenced
- Create branches in local and remote repositories
- Deal with merge conflicts
Creating a new project
Let’s practice creating a new project from scratch. We will start by making the remote repository in GitHub.
Go to https://github.com and log in.
Click the + icon in the top-right corner → choose “New repository”.
Fill out the form:
- Repository name: e.g.,
my-new-project - (Optional) Add a short description
- Choose Public (or Private if preferred)
- ✅ Check “Add a README file”
- Repository name: e.g.,
Click the “Create repository” button at the bottom.
Your repository is now created and ready to use!
Now, we need to clone the repository.
💻 Step 2: Clone the Repository Using the Terminal
On your new repository page, click the green “Code” button.
Choose the SSH tab (⚠️ not HTTPS) and copy the URL.
It will look like this:git@github.com:your-username/my-new-project.gitOpen your terminal.
Navigate to the folder where you want to store your project (e.g.,
Documents):cd ~/DocumentsUse the
git clonecommand followed by the URL:git clone git@github.com:your-username/my-new-project.gitMove into the project folder:
cd my-new-project
🌿 Step 3: Create a Branch, Make Changes, and Push
Now that your repository is cloned, you’re ready to create a branch, make some changes, and push it to GitHub.
Branching
🔀 1. Create a New Branch
In the terminal, make sure you’re inside your project folder:
cd my-new-projectRemember you can always check where you are by typing
pwdCreate and switch to a new branch (you can name it anything — here we’ll use feature):
git branch feature
git switch featureYou can also do this in one command with
git switch -c feature✅ You are now working in a new branch called feature.
✍️ 2. Make a Change to a File
Open the README.md file in RStudio.
Add a new line to the bottom, such as:
This is a change I made on the feature branch.Save the file.
💾 3. Stage and Commit the Change
Check which files were changed. You can do this in the Source Control pane in Positron, or using the terminal:
git statusStage the file:
git add README.mdCommit the change:
git commit -m "Added a line to README on feature branch"✅ Your change is now saved locally in your branch.
🚀 4. Push the Branch to GitHub
Now push your new branch to the remote (GitHub):
git push -u origin feature✅ You should now see the feature branch appear on GitHub. Go to your repository in the browser and click the “branches” dropdown to check!
We need the -u flag on the push command to tell git to link together the remote (origin) and our local branch. From now on, we can just use git push and git will remember the link.
🔁 Merge Your Branch Using the Terminal
Now that you’ve made and committed changes on your feature branch, let’s merge those changes into the main branch — all from the terminal.
🔄 1. Switch Back to the main Branch
First, make sure you’re back on the main branch. You can do this using the branch switcher in Positron, or using the terminal:
git switch main🔃 2. Pull the Latest Changes from GitHub
Before merging, make sure your local main branch is up to date with the remote:
git pull origin main🔀 3. Merge the Feature Branch into Main
Now merge your feature branch into main. For example, if your branch is called feature:
git merge feature✅ This combines the changes from feature into main.
🚀 4. Push the Merged Changes to GitHub
Now push the updated main branch to GitHub:
git push origin main✅ Your main branch on GitHub now includes the changes from your feature branch!
🧹 5. (Optional) Delete the Feature Branch
You can now delete the feature branch locally:
git branch -d featureIf you also want to delete it on GitHub:
git push origin --delete feature⚔️ Create and Resolve a Merge Conflict
Now that you know how merging works, let’s deliberately cause a merge conflict and walk through how to fix it.
Merge conflicts happen when two branches change the same part of a file in different ways — Git doesn’t know which version to keep.
🔧 1. Start Fresh in main
Make sure you’re on the main branch:
git switch mainCreate a new branch but do not switch to it:
git branch conflict-branchOpen the README.md file and change the first line to:
This line was edited on main.Save the file, then stage and commit the change:
git add README.md
git commit -m "Edit first line on main"🌿 2. Create a New Branch and Make a Conflicting Change
Switch to the new branch:
git switch conflict-branchOpen README.md again, but this time change the same first line to:
This line was edited on conflict-branch.Save it, then stage and commit:
git add README.md
git commit -m "Edit first line on conflict-branch"🔁 3. Merge the Branch into main
Switch back to main:
git switch mainTry to merge the branch:
git merge conflict-branch🛑 Uh-oh! You’ll see a merge conflict message. Git can’t automatically merge the file.
🧠 4. Resolve the Conflict
Open README.md. You’ll see something like this:
<<<<<<< HEAD
This line was edited on main.
=======
This line was edited on conflict-branch.
>>>>>>> conflict-branch
This means:
- Everything between
<<<<<<< HEADand=======is frommain - Everything after
=======is fromconflict-branch
Edit the file to keep only one version — or combine them. For example:
This line includes changes from both branches.Make sure you delete the conflict markers (<<<<<<< HEAD, ======= and >>>>>>> conflict-branch) as part of your changes.
Save the file.
✅ 5. Finalize the Merge
After resolving the conflict:
- Stage the fixed file:
git add README.md- Complete the merge with a commit:
git commit -m "Resolved merge conflict in README.md"- Push the updated
mainbranch to GitHub:
git push origin main🎉 You’ve resolved a merge conflict like a pro!