git-workshop-workflows

Git Workshop: Workflows

Branches, conflicts, and pull requests.

[!IMPORTANT] Instructors: Before beginning, create a new workshop-specific repository by clicking the Use this template button on the caltechlibrary/git-workshop-recipes repository page.

Table of Contents

  1. Setup
  2. Branches
  3. Conflicts
  4. Pull Requests
  5. Acknowledgements

Setup

Git Setup

Check existing configuration.

git config --list

Configure your global name and email.

git config --global user.name "Tommy Keswick"
git config --global user.email "415179+t4k@users.noreply.github.com"

Go to GitHub Profile > Settings > Emails to find your private email alias.

SSH Setup

Check if SSH keys have been set up before.

ls -al ~/.ssh

If not, create a new SSH key pair.

ssh-keygen -t ed25519 -C "tommy@macbook"

Access and copy the public key.

cat ~/.ssh/id_ed25519.pub

Add the public key to GitHub under GitHub Profile > Settings > SSH and GPG keys.

Check that your machine can be authenticated by GitHub.

ssh -T git@github.com

Branches

Create a local Git repository.

cd ~/Desktop
mkdir recipes
cd recipes
git init
touch guacamole.md
git add guacamole.md
git commit -m "begin first recipe"

View the branches in the repository.

git branch

Work on the main branch.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
git commit -am "add guacamole ingredients"

Create a new branch.

git branch spicy

View the branches in the repository again.

git branch

Switch to our new branch.

git checkout spicy

See also git switch.

Note that we are now on our spicy branch.

git branch
# or
git status

Work on the spicy branch.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
- jalapeƱo
git commit -am "add some spice"

View our history on the spicy branch.

git log --oneline

Switch back to the main branch.

git switch main
# or `git checkout main` if that does not work

View our history on the main branch.

git log --oneline

Create and switch to a new branch.

git switch -c mild
# or `git checkout -b mild` if that does not work

Note that we are now on our mild branch.

git branch
# or `git status`

Work on the mild branch.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
- onion
git commit -am "add some flavor"

View our history on the mild branch.

git log --oneline

Switch back to the main branch.

git switch main

View our history on the main branch.

git log --oneline

Get overview of all branches.

git log --oneline --graph --all

Merge the changes from the mild branch into the main branch.

git merge mild

Delete the branch reference for the fast-forwarded branch.

git branch -d mild

Force delete the unmerged spicy branch, which deletes all work in the branch.

git branch -D spicy

Conflicts

Create a new instructions branch from the current main branch.

git branch instructions

Continue working on main branch.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
- onion

# Instructions
TBD
git commit -am "add guacamole instructions placeholder"

Observe the main branch log.

git log --oneline

Switch to the instructions branch.

git switch instructions
# or `git checkout instructions` if that does not work

Work on the instructions branch.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
- onion

# Instructions
Go to the store.
git commit -am "begin guacamole instructions"

Observe the instructions branch log.

git log --oneline --graph --all

Switch to the main branch.

git switch main
# or `git checkout main` if that does not work

Merge the changes from the instructions branch into the main branch.

git merge instructions

Examine conflict.

cat guacamole.md

Edit the file to manually reconcile the changes.

nano guacamole.md

Add the resolved conflict changes and commit them to complete the merge.

git add guacamole.md
git status
git commit -m "resolve merge conflicts with instructions branch"

View the graph.

git log --oneline --graph --all

Switch to the instructions branch and merge with main.

git switch instructions
git merge main
git log --oneline --graph --all

Work on the instructions branch some more.

nano guacamole.md
# Ingredients
- avocado
- lemon
- salt
- onion

# Instructions
1. Cut avocados.
2. Dice onion.
3. Squeeze lemon.
4. Pinch salt.
git commit -am "continue guacamole instructions"

Merge changes into main branch.

git switch main
git merge instructions

View current guacamole.md file.

cat guacamole.md

Pull Requests

Fork the workshop repository provided by the instructor into your personal GitHub account.

Clone the forked repository to your local machine.

cd ~/Desktop
git clone git@github.com:t4k/WORKSHOP-REPOSITORY.git

Add an upstream remote to the local repository.

cd ~/Desktop/git-workshop-recipes-2024
git remote add upstream https://github.com/caltechlibrary/WORKSHOP-REPOSITORY.git

View the remotes for your local repository.

git remote -v

Instructor adds another recipe to the upstream repository.

Pull the latest content into your local repository and push to your remote fork.

git pull upstream main
git push origin main
# alternatively, use the **Sync fork** functionality from the GitHub interface

Everyone creates a local branch to add a new recipe.

git switch -c add-orangejuice
# or `git checkout -b add-orangejuice` if switch does not work

Confirm you are on the correct branch.

git branch

Create a new file for your recipe.

# optionally copy an existing recipe to modify
# `cp guacamole.md orangejuice.md`
nano orangejuice.md

Add and commit the changes.

git add orangejuice.md
git commit -m "add orange juice recipe"

Push changes to remote fork.

git push origin add-orangejuice

Use GitHub to initiate a pull request.

Upstream repository owner reviews pull request and makes suggestions.

Update pull request with suggested changes.

nano orangejuice.md
git commit -am "update orange juice recipe with suggestions"
git push origin add-orangejuice

Note that the pull request has been updated on GitHub.

Meanwhile, the upstream repository owner updates the README table of contents.

Everyone also should add to the README table of contents within their pull request branch.

nano README.md
git commit -am "add orange juice to table of contents"
git push origin add-orangejuice

Note the conflict in the pull request.

Resolve the conflict manually in your local repository and push to the pull request branch.

git pull upstream main
nano README.md
git commit -am "resolve conflict in table of contents"
git push origin add-orangejuice
# or use the **Resolve conflicts** button in GitHub

Upstream repository owner merges pull request.

Acknowledgements

Content has been adapted from the following lessons.