Objective: Learn the fundamentals of version control using Git. You will install Git, configure it, create a local repository, make commits, work with branches, merge changes, and resolve conflicts. Finally, you will push your code to a remote repository on GitHub, simulating a real‑world collaborative workflow.
Why are we doing this? Version control is essential for any automation project. It allows you to track changes, collaborate with others, and revert to previous states if something breaks. Git is the most widely used version control system, and platforms like GitHub are integral to CI/CD pipelines and infrastructure as code.
Task 1: Install and Configure Git on Ubuntu‑WS
Step 1: Open a terminal on Ubuntu‑WS (you can do this by clicking the terminal icon or pressing Ctrl+Alt+T).
2. Update the package list and install Git.
sudo apt update
sudo apt install git -y
3. Verify the installation.
git --version
You should see output like git version 2.25.1 (the exact version may differ).
4. Set your global username and email. These will be attached to your commits.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Replace with your actual name and email. Use the same email you will register on GitHub later.
5. (Optional) Set the default branch name to main (GitHub now uses main instead of master).
git config --global init.defaultBranch main
6. View your Git configuration.
git config --list
Task 2: Create a GitHub Account and Set Up SSH Authentication
To push your code to a remote repository, you need a GitHub account and a way to authenticate securely. We will use SSH keys.
Step 1: If you don’t have a GitHub account, go to https://github.com and sign up (free). Verify your email address.
2. Generate an SSH key pair on Ubuntu‑WS. Press Enter to accept the default file location and optionally set a passphrase (you can leave it empty for this lab).
ssh-keygen -t ed25519 -C "your.email@example.com"
If your system doesn’t support ed25519, use ssh-keygen -t rsa -b 4096 -C "your.email@example.com".
3. Display the public key and copy it to your clipboard.
cat ~/.ssh/id_ed25519.pub
Select the output and copy it (right‑click or Ctrl+Shift+C).
4. Log in to GitHub. Click your profile picture (top‑right) → Settings → SSH and GPG keys → New SSH key. Give it a title (e.g., “Ubuntu‑WS”) and paste the public key into the “Key” field. Click Add SSH key.
5. Test the SSH connection.
ssh -T git@github.com
You should see a message like Hi username! You've successfully authenticated... If prompted about authenticity, type yes.
Task 3: Create a Local Git Repository and Make Your First Commit
Step 1: Create a new directory for your project and navigate into it.
mkdir ~/my_automation_project
cd ~/my_automation_project
2. Initialize a new Git repository.
git init
This creates a hidden .git folder that tracks all version control information.
3. Create a simple Python script (or any text file) to simulate code.
nano hello.py
Type the following:
#!/usr/bin/env python3
print("Hello, Git!")
Save (Ctrl+O, Enter) and exit (Ctrl+X).
4. Check the status of your repository. Git tells you there is an untracked file.
git status
5. Add the file to the staging area (prepare it for commit).
git add hello.py
You can also add all files with git add . but for now we add just this file.
6. Commit the file with a descriptive message.
git commit -m "Initial commit: add hello.py"
git branch -M main
7. View the commit history.
git log --oneline
You will see a single commit with a short hash and your message.
Task 4: Create a Remote Repository on GitHub and Push
Step 1: In your browser, go to GitHub and click the + icon (top‑right profile picture) → Click on repositories.
2. Name the repository my_automation_project (you can use any name, but it’s convenient to match your local folder). Leave it public or private (your choice). Rest everything as default. Do not initialize with a README, .gitignore, or license because we already have a local repository. Click Create repository.
3. GitHub will show instructions for pushing an existing repository. Copy the SSH URL (looks like git@github.com:username/my_automation_project.git).
4. Back in the terminal, add the remote repository.
git remote add origin git@github.com:username/my_automation_project.git
Replace username with your GitHub username.
5. Verify the remote.
git remote -v
6. Push your local main branch to GitHub. The -u flag sets the upstream so future pushes can be done with just git push.
git push -u origin main
If successful, refresh your GitHub repository page – you should see hello.py there.
7. Make a change locally and push again. Edit hello.py:
nano hello.py
Change the print line to: print("Hello, Git and GitHub!")
Save and exit.
8. Stage, commit, and push.
git add hello.py
git commit -m "Update greeting message"
git push
Now the remote repository reflects your latest commit.
Task 5: Branching and Merging
Branches allow you to develop features independently without affecting the main codebase.
Step 1: Create a new branch called feature/add-goodbye and switch to it.
git checkout -b feature/add-goodbye
This command creates and switches to the new branch. Verify with git branch (the asterisk shows your current branch).
2. Create a new file goodbye.py.
nano goodbye.py
Type:
#!/usr/bin/env python3
print("Goodbye, Git!")
Save and exit.
3. Stage and commit the new file on the feature branch.
git add goodbye.py
git commit -m "Add goodbye script"
4. Switch back to the main branch.
git checkout main
Notice that goodbye.py disappears because it only exists on the feature branch.
5. Merge the feature branch into main.
git merge feature/add-goodbye
If there are no conflicts, Git performs a fast‑forward merge and goodbye.py now appears in main.
6. Push the updated main to GitHub.
git push
You can also push the feature branch if you want to share it: git push origin feature/add-goodbye.
7. Delete the local feature branch (optional).
git branch -d feature/add-goodbye
Task 6: Simulate and Resolve a Merge Conflict
Conflicts occur when two branches modify the same part of the same file. Here we’ll create one and resolve it.
Step 1: Create a new branch conflict-branch from main.
git checkout -b conflict-branch
2. Modify hello.py on this branch. Change the print line to something else, e.g.:
print("Hello from conflict branch")
Stage and commit.
git add hello.py
git commit -m "Change hello message on conflict-branch"
3. Switch back to main.
git checkout main
4. Modify the same hello.py on main with a different message, e.g.:
print("Hello from main branch")
Stage and commit.
git add hello.py
git commit -m "Change hello message on main"
5. Now try to merge conflict-branch into main.
git merge conflict-branch
Git will report a merge conflict in hello.py. The terminal shows something like:
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py
Automatic merge failed; fix conflicts and then commit the result.
6. Open hello.py in an editor. You’ll see conflict markers:
nano hello.py
It will look like:
<<<<<<< HEAD
print("Hello from main branch")
=======
print("Hello from conflict branch")
>>>>>>> conflict-branch
7. Resolve the conflict by editing the file to keep the desired version. For this lab, let’s combine them or keep one. For example, change it to:
print("Hello from main after resolving conflict")
Remove the conflict markers and save.
8. Stage the resolved file and commit the merge.
git add hello.py
git commit -m "Resolve merge conflict in hello.py"
9. Push the merge to GitHub.
git push
Task 7: Using git diff to See Changes
The git diff command shows differences between commits, branches, or the working directory.
Step 1: View unstaged changes (changes you haven’t added yet).
Make a small change to goodbye.py without staging it:
echo 'print("See you later!")' >> goodbye.py
Now run:
git diff
You’ll see the added line highlighted (green with a +).
2. Stage the change and use git diff --staged to see staged differences.
git add goodbye.py
git diff --staged
3. Compare two branches. For example, see what’s different between main and the now‑deleted conflict-branch (if you still have it locally, otherwise compare with origin/main).
# if branch exists
git diff main conflict-branch
# compare local main with remote main
git diff main origin/main
4. View the difference between two commits. Use git log --oneline to see commit hashes, then:
git diff <hash1> <hash2>
Task 8: Pulling Changes from Remote
If you collaborate with others, you’ll need to pull their changes. We’ll simulate this by making a change directly on GitHub and then pulling it.
Step 1: Go to your GitHub repository, click on hello.py, then click the pencil icon (Edit this file). Add a comment at the top, e.g.:
# This file was edited on GitHub
Scroll down and click Commit changes (leave the default commit message).
2. Back in the terminal, pull the remote changes to your local repository.
git pull origin main
Git will fetch the changes and merge them. Now hello.py locally contains the comment you added on GitHub.
3. Verify with cat hello.py.
These skills are fundamental for managing automation code, collaborating with teams, and integrating with CI/CD pipelines. In the next lab, you will apply these Git skills while learning Python basics.