CCC-Web Worksheet

Uni of Portsmouth Logo
Revision Control
Example of revision control through copying a file and changing the filename.
How not to do it! An example of revision control through copying a file and changing the filename.

What happens in software development?

  • 👩🏻‍💻 Write a program
  • 👩🏻‍💻 Work on it a bit more
  • 🎉 It's ready for use
  • 😊 You use it
  • 💡 You think of a new feature
  • 😎 You add the feature
  • ⚠️ Eek a bug
  • 🆒 Fix the bug
  • 😊 Use the program some more
  • ⚡️ Start work on big new feature
  • 🚨 Eek! Another bug!
  • 😕 Now what?
  • 🍝 Big feature not finished yet. Code threads are everywhere like spaghetti. You can't add a new feature because nothing is working right now.
  • 🤯 Oh dear

Why do we need git?

When there are multiple developers, someone might ask:

  • what was done?
  • why was it done?
  • who did it?
  • when did they do it?
  • how can we undo it?

Helpfully git

  • tracks every code change
  • records commit messages
  • keeps an audit trail
  • facilitates collaboration
  • enables rollback

Git Glossary

Repository (Repo):
A folder where Git tracks your project.
Working Tree:
All the files and folders that Git is tracking, plus any new files you add.
Branch:
An independent line of development within your project.
Stage:
Tell Git which changes you want to include in the next snapshot.
Commit:
Make a snapshot of your project at a specific point in time.
Clone:
A copy of a repository from a remote server to your local machine.
Push:
Uploading your local commits to a remote repository.
Pull:
Downloading changes from a remote repository to your local machine.

Core git commands

  • git init: Create a new Git repository in the current directory.
  • git status: Show the status of changes in the working tree.
  • git add <file>: Stage changes to a specific file for the next commit.
  • git commit -m "message": Create a new commit with a descriptive message.
  • git push: Upload local commits to a remote repository.

Task 0: Preparation

  1. Install git on your machine.
  2. Configure your git username, email and the default repo name. git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global init.defaultBranch main

Task 1: Install git

  1. Go to https://git-scm.com/downloads and download the appropriate version of git for your operating system (Windows, macOS, or Linux).
  2. Run the installer and follow the on-screen instructions to complete the installation. You can generally accept the default settings.
  3. After installation, open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type: git --version This command should display the installed version of git, confirming that the installation was successful.

Task 2: Make a website and use git to manage it

  1. To create a project folder, on Windows open Command Prompt (on Mac or Linux open Terminal).
  2. Navigate to the location where you want to create your project folder. For example, to go to your Documents folder, you would type: cd Documents
  3. Create a new folder for your project. You can do this on windows by typing: md example or on Mac/Linux by typing: mkdir example
  4. Change directory to the new project folder by typing: cd example
  5. Initialise git in that folder by typing: git init
  6. Create a home page by… you already know how to do this! :-)
  7. Stage the changes by typing: git add index.html
  8. Commit the changes with a message by typing: git commit -m "my new website"

Task 3: Make changes and track them with git

  1. Edit your index.html file to add a new section.
  2. Stage the changes by typing: git add index.html
  3. Commit the changes with a message by typing: git commit -m "added a new section"
  4. View the commit history by typing: git log

Task 4: Add a remote GitHub Repo

  1. For this next bit, keep in mind that potential employers may look at your GitHub profile so choose a sensible GitHub username.
  2. If you have not done so previously, register for a GitHub account.
  3. Login to GitHub.
  4. Create a new repository on GitHub without any files by visiting https://github.com/new
  5. Back in your terminal, link the local repo to GitHub - substituting your user name and the repository name you just created: git remote add origin https://github.com/USERNAME/REPONAME.git
  6. Push your current branch (for example, main) to the remote: git push -u origin main

Task 5: Make changes and push them to GitHub

  1. Edit your index.html file to add another new section.
  2. Stage the changes by typing: git add index.html
  3. Commit the changes with a message by typing: git commit -m "added another new section"
  4. Push the changes to GitHub by typing: git push
  5. Go to your GitHub repository page and refresh it to see the changes reflected there.

Task 6: Pull changes from GitHub

  1. Make a change directly on GitHub by editing the index.html file in your repository and committing the change.
  2. Back in your terminal, pull the changes from GitHub by typing: git pull
  3. Open your local index.html file to see the changes that were made on GitHub.

Task 7: Create and work on a new branch

  1. Create a feature branch locally: git checkout -b feature/new-section
  2. Edit your index.html file to add a new section for the feature you are working on.
  3. Stage the changes by typing: git add index.html
  4. Push the branch to GitHub so others can see it: git push -u origin feature/new-section

Task 8: Merge the feature branch back into main

  1. Switch back to the main branch: git checkout main
  2. Merge the feature branch into main: git merge feature/new-section
  3. Push the updated main branch to GitHub: git push
  4. Optionally, delete the feature branch both locally and on GitHub: git branch -d feature/new-section

Summary

In this worksheet, you have learned the basics of using git for version control and how to collaborate using GitHub. You have created a local repository, made changes to your project, committed those changes, and pushed them to a remote repository on GitHub. You have also learned how to pull changes made on GitHub back to your local machine. These skills are essential for effective software development and collaboration in modern programming environments.

Further Learning

To learn more about git and GitHub, consider exploring the following resources: