The objective of this training module is to enhance your understanding of Git and version control. After completing this module, you should be able to:
In this module, we will explore Git and GitHub, two essential tools in modern software engineering. Understanding these tools is crucial for effective version control and collaboration in software development.
Before we delve deeper into Git and GitHub, it's important to understand what version control is.
Version control is a system that tracks changes to files over time, allowing you to retrieve specific versions at a later date. A Version Control System (VCS) enables you to:
Common version control models include Local VCS, Centralized VCS, and Distributed VCS. Modern software teams typically use distributed systems such as Git.
Git is a distributed version control system that helps track changes in your code over time.
Think of the changes as a stream of snapshots, each representing the state of your project
at a specific point.
A Git repository contains a hidden .git folder within the project directory,
which stores all the version history and metadata.
GitHub is a website and cloud-based service designed to help you save and manage your code and projects. It is the most commonly used platform for hosting repositories, allowing you to share your code online with others. Additionally, GitHub can serve as a valuable tool for showcasing your work, acting as a developer's resume.
With this foundation, in this module, you will learn a complete workflow: setting up Git, creating and cloning repositories, making meaningful commits, working with branches, resolving basic merge conflicts (self-learn), and collaborating using pull requests.
By mastering Git and GitHub, you will enhance your ability to manage projects effectively and collaborate with others in the software development process.
In this workshop, we will use Git and Visual Studio Code (VSCode) to complete our practice.
Visit github.com and sign up for a free account.
Git is the command-line tool that powers GitHub. To install it on your system:
.exe file and click Next
until you reach the page where you can choose the default editor.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git
Open VSCode and launch the terminal by pressing Ctrl + ` (or use the macOS
terminal directly).
Set up your identity in Git by entering the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
In VSCode, open the terminal using Ctrl + ` and type:
ssh-keygen -t ed25519 -C "your_email@example.com"
Press "Enter" repeatedly until you see a confirmation message. This will create the SSH key in the default directory for later use.
Next, retrieve the SSH key by typing:
cat ~/.ssh/id_ed25519.pub
Copy the displayed key and add it as a new SSH key in your GitHub account.
To test your setup, run the following command in the terminal:
ssh -T git@github.com
When prompted, type "yes" to confirm the connection. If the setup is successful, you will see a success message.
Practice the essential daily Git command workflow.
As mentioned earlier, a Git project contains a hidden .git folder.
If you want to make your folder a Git project, initialize a repository from the project root.
Open terminal in VSCode and run:
git init
The Basic Workflow of a Git Project has three local stages: Modified, Staged, and Committed.
git add, you prepare your changes to be committed. This is the "staging area" or "index"
where you curate which changes you want to commit.
git commit -m "message", your changes create a snapshot in the project's
version history that can be retrieved at any time.
Example: create a hello_world.py file, then add it to staging.
print("Hello world")
hello_world.py
Click the + icon in Source Control to stage changes.
+ button
# Replace hello_world.py with your target file
git add hello_world.py
# Or stage all files
git add .
# Remove from staging area
git reset hello_world.py
Click the - icon to unstage a file.
- button
git reset hello_world.py
After staging files, write a commit message and save the snapshot to local repository.
# Check what changed
git status
# Save a snapshot
git commit -m "commit message"
If you want to learn how to safely undo a commit after it is pushed, you can learn from here.
You can view the commit history using VSCode or the terminal.
Click on the Source Control icon in the left sidebar and look for the GRAPH section. This displays a visual timeline of all your commits with branches, showing:
Use the git log command to view commit history:
# View commit history with details
git log
# View commit history in one-line format (compact)
git log --oneline
# View commit history with author and date
git log --oneline --graph --all --decorate
A commit object stores a pointer to the snapshot, a pointer to the previous commit, author information, and commit message. For example, we make one more commit:
The snapshot stores the data state at that commit.
Always write clear, descriptive commit messages. Future you and your collaborators will thank you.
Sometimes we need to make changes without editing the main line directly. Branches let you work safely without copying the whole repository.
A branch is a pointer to a commit. HEAD tells you which branch you are currently on.
HEAD pointer
In VSCode: Branch → Create Branch → [type branch name]
feature in VSCode
git branch feature # create new branch
git checkout feature # switch to feature branch
# Or create and switch in one command
git checkout -b feature
The new branch points to the same commit as HEAD at creation time.
HEAD
After a commit on feature, the feature pointer moves while
master/main stays where it was.
feature branch in VSCode
feature moved; master/main unchanged
feature to master in VSCode
git checkout main
# or, if your default branch name is master
git checkout master
If you commit on different branches, history diverges into parallel lines.
For example, commit on master/main while
feature stays unchanged.
master branch in VSCode
When feature work is finished, merge it back into your target branch.
master)
feature into master
git checkout master # target branch
git merge feature # source branch
feature into master
If you want to learn how to solve merge conflicts after merging branches, you can learn from here.
Remote repositories are versions of your project hosted on the internet for collaboration and backup.
Remote branches are denoted as:
<remote-name>/<branch-name>
The default remote name is usually origin.
We will use GitHub to host our remote repository.
First, create a repository on GitHub.
Remember: Don't click anything on "initialize repository".
my-first-repo).
Make your repository private for assignment work.
Then copy the repository link:
Remember to set up SSH in Git (from the Installation section).
In VS Code: Remote → Add Remote
Then add the link and press Enter:
Then type remote name (origin) and press Enter:
origin)
# replace <remote-name> with your remote name
# replace <github-link> with your URL
git remote add <remote-name> <github-link>
# Check remotes
git remote -v
In VS Code: Remote → Remove Remote
# replace <remote-name> with your remote name
git remote remove <remote-name>
After setting up remote, push your branch to repository.
In VS Code: Publish Branch
# replace <remote-name> with your remote name
# replace <branch-name> with your branch name
# you don't need --set-upstream on later pushes
git push --set-upstream <remote-name> <branch-name>
# Later push only
git push
When there is a new change on remote branch (for example, editing
hello_world.py on GitHub), sync it to local.
Use fetch to download remote updates:
# replace <remote-name> with your remote name
git fetch <remote-name>
This downloads the updated remote branch:
Then you can sync (merge) origin/master to master:
origin/master to master
You can also use merge from <remote-name>/<branch-name> to
<branch-name>; it does the same sync effect.
git merge <remote-name>/<branch-name> <branch-name>
Or simply use pull, which combines fetch and merge:
git pull <remote-name>
# This does fetch and merge together
First, get the clone URL from GitHub:
Press Command+Shift+P and run Git Clone.
Then paste repository URL:
And select destination folder:
# change <remote-url> to target repository URL
git clone <remote-url>
A fork creates your own copy of someone else's repository under your GitHub account.
A Pull Request (PR) lets you propose your changes, discuss them with reviewers, and merge safely into the target branch after approval.
Pull Request checklist: clear title, concise description, testing notes, and reviewer-friendly commits. After opening the pull request, wait for reviewers to do code review. Once it is approved, a maintainer (or authorized teammate) will merge the pull request.
Always fetch/pull before starting new work to ensure you have the latest changes from collaborators.
Complete the following workshop tasks in sequence and keep evidence for submission:
feature-greeting, modify the file, commit, and merge back to
main.
Verify that your repository has clear commit history, working branches, and synchronized remote updates.
If push/pull fails, check git remote -v, branch name, and authentication (SSH key setup).
git init
git clone <url>
git remote add origin <url>
git status
git add <file>
git commit -m "message"
git push
git pull
git branch
git checkout -b <branch>
git merge <branch>
git branch -d <branch>
git fetch
git pull
git push
git remote -v
Merge conflicts happen when two branches change the same lines differently. A common case is editing the
same file in both master/main and a feature branch.
master branch content
When you merge branches with conflicting edits, Git stops and asks you to resolve:
git checkout master
git merge wrong
Open the conflicted file and choose the final content in the Merge Editor. You can accept current,
incoming, or both changes, then click Complete Merge.
Terminal workflow after editing conflict markers:
# 1) Check conflicted files
git status
# 2) Resolve markers in file: <<<<<<< ======= >>>>>>>
# 3) Stage resolved file(s)
git add <file>
# 4) Complete merge
git commit
If you decide to stop the merge process:
git merge --abort
Use git revert to safely undo changes from an earlier commit. Unlike git reset,
it does not rewrite history; it creates a new commit that reverses the target changes.
# 1) Inspect history and copy target commit hash
git log --oneline
# 2) Revert one commit by hash
git revert <commit-hash>
# 3) Revert the latest commit
git revert HEAD
# 4) Revert a merge commit (choose mainline parent, usually 1)
git revert -m 1 <merge-commit-hash>
After revert, run git status and git log --oneline to verify the new revert
commit, then push as usual.
The training module deadline is to be announced by the instructor.
We will use the online grading system ZINC to grade your lab work.
You are required to upload only the required evidence files to ZINC:
README.md (brief summary of steps completed)git-log.txt (exported commit history)You may submit multiple times before the deadline; only the last submission will be graded. Make sure your submission clearly demonstrates command usage, branching workflow, and repository organization.
Git is the version control tool that runs locally. GitHub is a cloud platform that hosts Git repositories and supports collaboration.
Run git status, open conflicted files, resolve the conflict markers manually, then use
git add and git commit to complete the merge.
As this module is assessed coursework, we cannot complete tasks for you directly.
We can still provide hints and help you reason about the error messages.