Exercises for learning how to use git. This is part of Tech Start UCalgary's git tutorial. You can see our slides here
Need more detailed documentation? Check out the Tech Start Git Guide
Goal: Practice staging and committing changes
Pre-requisites: you have installed Git on your computer, and you have a GitHub account
To install Git, follow this guide
- Fork the repository. This will create a new copy of the repository in your own GitHub account. To do this, follow these instructions.
- Go to the repository
- Click
Fork
on the top right - Uncheck
Copy the main branch only
- Click
Create fork
- Wait for the fork to load
- Clone the forked repository on to your computer. To do this, follow these instructions.
- Once you have cloned it,
cd
in the folder you created (cd Learn-Git
). Typegit status
into your git console. If you are in the right place, then git should print a message like this:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Once you see this output, you're good to continue!
- Edit the file called
song.txt
. There are multiple blank spots - denoted by the brackets with capitalized words like [NOUN]. Fill in these spots with the words of your choosing. Save it. Typegit status
- confirm thatsong.txt
shows up as a "unstaged change" before continuing. - Follow the process we outlined in the slides to add this change and create a commit for it. Name your commit "Fill in the blanks". Type
git status
afterwards - you should see the same "nothing to commit" message as in A1, but this time your file is saved with the changes you made. - Replace the first line of
song.txt
with a bunch of garbage (ex:iadjnigb21ehqpe;aifw3thr2npq;aneflr
). Then, stage your change, but don't commit it. Verify this withgit status
: you should see the file listed under staged changes. - Use the commands we learnt to take away the change from the staging area without getting rid of the change. Use
git status
to verify this worked (it should not be in staged files anymore, but you should still see your nonsense in the file itself). This means if you made other commits now, they would not include your change, but it's still there if you need it! - Use the commands we learnt to get rid of your change entirely, restoring the file to how it was at the most recent commit. Use
git status
to verify this.
- Create a folder called
secrets
in your repository. Create a file in this folder calledsecret
that contains your deepest darkest secret (like what you had for lunch yesterday) - Save this image as
jimmy.jpeg
in the repo's root folder. - Using
git status
, you should see indications of the new folder and files you added under untracked files. Once you do, continue to the next step. - Create a file called
.gitignore
. Your challenge is to add entries to this gitignore to achieve the following goals:- Nothing from the secrets folder should be committed
- No image files that end with the exension .jpeg should be committed
- Hint: you can use flags like *
- Confused? Help is here
- Type
git status
. If you did the previous step correctly, the only file you should see now is your gitignore file. Once this is true, add it, and create a commit with it. Name this commit "Add gitignore".
Goal: Practice branching
- Pick a short alias for yourself. For example, if your name was Dwayne Johnson, your alias could be dwayne, dwajo, or therock.
- Create a new branch called
YOUR_ALIAS_HERE/addFrog
. ex:dwayne/addFrog
- Switch to that branch. Make sure you are on that branch (type
git status
) - Add the following ASCII art at the end of
song.txt
, or add any ASCII art of your choosing:
_ _
(.)_(.)
_ ( _ ) _
/ \/`-----'\/ \
__\ ( ( ) ) /__
) /\ \._./ /\ (
)_/ /|\ /|\ \_(
- Stage and commit your changes.
- Switch back to your main branch. Is your ASCII art still there? Why or why not?
- If you created a new branch from main, would it contain the frog? What about if you created a new branch while you were currently in
YOUR_ALIAS/addFrog
? Unsure? Try it out for yourself. - Create a new branch with a name of your choosing. Then try deleting it. If you deleted it successfully, you should not be able to switch to it with
git checkout
.
Goal: Practice pushing and pulling
- Switch back to the branch where you created your frog
- Using the commands you just learned, try pushing your frog to a branch on origin with the same name.
- Visit your GitHub repo online. If your push was successful, you should see a new branch named
YOUR_ALIAS/addFrog
. If you don't see this, make sure you're looking at your fork. If you still don't see it, ask for help.
- From GitHub, create a pull request to merge your frog branch into the main branch. Make sure you're merging into the main branch of your repository, not the Tech Start one.
- Complete the pull request.
- Does your frog appear on main in the origin repo? Does your frog appear on main on your computer?
- If the answer to the second question is no, then what should command could you do to change that?
Goal: Practice merge conflicts, whole workflow
Scenario: 2 different brilliant individuals decided they would spice up this repository by adding some art of the lovable dog named Cheems (RIP). But since great minds think alike (and since software engineers have poor communication skills), they both did it at the same time without telling each other, and one of them merged to main - creating a merge conflict which prevents the second one from merging.
Using what you learned, your goal is to solve the merge conflict so that the second person, whose code is on the branch named evilcheems
, can merge, and so that the following conditions are true:
- Cheems no longer has his evil eyebrows
- Both authors are credited for their work
- The poem in the evilcheems branch remains
Remember, use git status
to confirm if the merge conflict is gone.
HINT: you will need to use git pull origin main
from within the evilcheems
branch to bring the merge conflict up on your local repo. From there, solve the conflict, and follow what you know about pull requests to merge it in to main.
Using the Git workflow we just taught you (including branching, committing, pushing, pull requests, and pulling), practice a full cycle of making a change of your choice to your fork. Examples of changes you could make include adding a song2.txt, adding images, or stylizing existing files. Go crazy!