Git Exercises

Quick Links

While GitHub is a hosting platform for Git repositories, Git is a that can be installed locally to track changes to files, can be used offline, and does not require hosting platforms.

Git is not an organizational convention or folder structure. Git does not rely on manual file naming or back-ups. Rather, Git is a command-line tool or hosting platform that creates or stores a hidden .git folder in your project directory and automatically stores version history, branches, and metadata.

Exercise 1: Create a Practice Folder

In this first exercise, we will create a practice folder on your computer’s hard drive. This is where your file and .git folder will be stored. Follow along, watching both the command line and new folder.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type 'mkdir git-practice' and hit enter

  4. 'mkdir' is a basic command-line tool, which stands for "make directory." This will create a new folder (repository). On your harddrive, you will see a new git-practice folder.

  5. Type 'cd git-practice' and hit enter

  6. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  7. Type 'git init' and hit enter to initialize Git

  8. This action will create an invisible .git file within the `git-practice` folder, where versions and metadata will be stored.

    Exercise 1 image displaying code that mkdir git-practice was initialized, changed directory to git-practice, and initialized empty Git repository.

Exercise 2: Create a Practice File

Now that we have a folder, we will create a file to go inside. This can be done entirely by command line. We will be creating a markdown file (.md).

! - If you're starting from the last exercise, skip to step 3.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type 'cd git-practice' and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `echo "# This Is A Test" > README.md` and hit enter

  6. `echo` is used to output text, in this case writing the content "This Is A Test" as a title header (#) into a file named README.md.

    You will now see a markdown file named README.md in your git-practice folder, with the content “# This Is A Test”.

  7. Type `git add README.md` and hit enter

  8. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  9. Type `git commit -m "Initial commit with README"` and hit enter

  10. `git commit` saves a snapshot of the file and `-m` lets you add a description of the change. In this case, the message is "Initial commit with README".

    Exercise 2 image displaying code that a heading was added to the README.md file and was committed.

Exercise 3: Changing a File

Next, our new file will get its first content. This can also be done via the command line.

! - If you're starting from the last exercise, skip to step 3.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `echo "## My First Subheading" >> README.md` and hit enter

  6. `echo` is used to output text, in this case writing the content "This Is A Test" as a title header (#) into a file named README.md.

    `>>` serves to amend the file, as opposed to `>`, which will overwrite.

    Your markdown file will now include the subheading “My First Subheading”.

  7. Type `git add README.md` and hit enter

  8. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  9. Type `git commit -m "Added a subheading"` and hit enter

  10. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.

    Exercise 3 image displaying code that a subheading was added to the READEME.md file.

Exercise 4: Create a Branch

An important feature of Git is branching. Branches allow us to make edits to existing repositories without overwriting any previous work.

! - If you're starting from the last exercise, skip to step 3.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `git checkout -b branch-readme`

  6. `git checkout` is used to switch branches while `-b` tells Git to create a new branch named "branch-readme".

  7. Type `echo "- This is a practice branch" >> README.md` and hit enter

  8. `echo` is used to output text, in this case writing the content "This Is A Test" as a title header (#) into a file named README.md.

    At this point, you will see the new line in your markdown file.

  9. Type `git add README.md` and hit enter

  10. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  11. Type `git commit -m "Added note about practice branch"` and hit enter

  12. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.

  13. Type `git checkout main` and hit enter

  14. This will switch back to main branch.

    Note: if `main` returns an error, try `master` instead.

  15. Type `git diff main branch-readme`

  16. git diff` compares the differences in branches.

    Note: if `main` returns an error, try `master` instead.

    The new line from your branch will no longer be visible in your markdown file.

    Exercise 4 image displaying code that a branch was added and committed. Git diff is also initialized and used to check differences between main and practice branches.

Exercise 5: Merging Branches

Once content has been finalized on the alternate branch, it is time to merge the branches together. This allows the main branch to remain updated with the latest version of the content.

! - If you're starting from the last exercise, skip to step 4.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `git checkout main` and hit enter

  6. This will switch back to main branch.

    Note: if `main` returns an error, try `master` instead.

  7. Type `git merge branch-readme` and hit enter

  8. This will combine changes from the branch file into the main file.

    If you’re following from the last exercise, your markdown file will now include the line “ - This is a practice branch”.

  9. Type `cat README.md` to see your changes in Git

  10. Exercise 5 image displaying that changed have been made to the file on the main branch.

Exercise 6: Revise in Markdown Editor

Drafting a document entirely in the command line would be an onerous task. That doesn’t mean you have to give up the version control power of Git. In this next exercise we will revise our markdown file in a markdown editor. Once the file has been saved, we can tell Git to track these changes as well.

! - If you’re starting from the last exercise, skip steps 3 and 4

  1. Open your README.md file in a markdown editor

  2. On a new line, type `## Save vs Commit`, then save and close the file

  3. This new line has been saved to your README.md file, but is not being tracked by Git.

  4. Open Terminal/Command Prompt

  5. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  6. Type `cd git-practice` and hit enter

  7. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  8. Type `git status` and hit enter

  9. This will show that Git sees the change, but the change is not being tracked.

  10. Type `git add README.md` and hit enter

  11. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  12. Type `git commit -m "Made changes in markdown editor"` and hit enter

  13. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.

    Exercise 6 image displaying that a snapshot of the file has been saved. And that the changes were made in a markdown editor.

Exercise 7: View Commit History

By now you have some project history, but how do you see it? Try the exercise below to find track the project from start to finish.

! - If you're starting from the last exercise, skip to step 3.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `git log --oneline` and hit enter

  6. This will show a compact list of commits, with minimal details.

    Exercise 7 image, step 3: terminal displays a compact list of commits.
  7. Type `git log` and hit enter

  8. This will show a complete list of commits, with full details

    Exercise 7 image, step 4: terminal displays a complete list of edits made to the file.

Exercise 8: Revising a Commit Message

If you were to mislabel your commit message, perhaps naming it something too vague, there’s still time to change it.

! - If you're starting from the last exercise, skip to step 3.

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `echo "This line is a mistake" >> README.md` and hit enter

  6. `echo` is used to output text, in this case writing the content "This line is a mistake" into a file named README.md.

    `>>` serves to amend the file, as opposed to `>`, which will overwrite

    Your markdown file will now include the content “This line is a mistake”.

  7. Type `git add README.md` and hit enter

  8. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  9. Type `git commit -m "Oops"` and hit enter

  10. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.

  11. Type `git log --oneline` and hit enter

  12. This will show the "oops" commit and the HEAD of the list.

  13. Type `git reset --soft HEAD~1` and hit enter

  14. This will reset the commit.

  15. Type `git status` and hit enter

  16. Here you will see that previous changes are still ready to recommit.

  17. Type `git commit -m "Revised commit message"`

  18. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change. The message on your Git commit has now been changed. The content "This line is a mistake" is still in the markdown file.

    Exercise 8 image displaying that a commit was made, the change was unstaged, and the commit message was revised.

Exercise 9: Delete a Commit

It is also possible to delete a revision from your .git history entirely.

! - If you’re starting from the last exercise, skip to step 6

  1. Open Terminal/Command Prompt

  2. Mac: open Terminal from Applications/Utility folder or by using Spotlight Search

    Windows: open Command Prompt by typing "cmd" into Windows search bar

  3. Type `cd git-practice` and hit enter

  4. 'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.

  5. Type `echo "This line is a mistake" >> README.md` and hit enter

  6. `echo` is used to output text, in this case writing the content "This line is a mistake" into a file named README.md.

    `>>` serves to amend the file, as opposed to `>`, which will overwrite.

  7. Type `git add README.md` and hit enter

  8. `git add` tells Git to prepare for a change you want to save, but does not save the changes yet. `README.md` is the specific file being prepared. There will be no visible change to the markdown file.

  9. Type `git commit -m "Oops"` and hit enter

  10. `git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.

  11. Type `git log --oneline` and hit enter

  12. This will show the "oops" commit and the HEAD of the list.

  13. Type `git reset --hard HEAD~1` and hit enter

  14. This will delete the commit. Your markdown file will no longer include the content "This line is a mistake".

  15. Type `cat README.md` and hit enter to view your Git versions

  16. Exercise 9 image displaying that a commit was deleted.

Note: after completing any or all of these Git exercises on your computer, you can get rid of the folder, markdown file, and hidden .git file by deleting the git-practice folder.

Next, Resources