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.
- Open Terminal/Command Prompt
- Type 'mkdir git-practice' and hit enter
- Type 'cd git-practice' and hit enter
- Type 'git init' and hit enter to initialize Git
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'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.
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
This action will create an invisible .git file within the `git-practice` folder, where versions and metadata will be stored.
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.
- Open Terminal/Command Prompt
- Type 'cd git-practice' and hit enter
- Type `echo "# This Is A Test" > README.md` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Initial commit with README"` and hit enter
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
`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”.
`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.
`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 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.
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `echo "## My First Subheading" >> README.md` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Added a subheading"` and hit enter
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
`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”.
`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.
`git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.
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.
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `git checkout -b branch-readme`
- Type `echo "- This is a practice branch" >> README.md` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Added note about practice branch"` and hit enter
- Type `git checkout main` and hit enter
- Type `git diff main branch-readme`
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
`git checkout` is used to switch branches while `-b` tells Git to create a new branch named "branch-readme".
`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.
`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.
`git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.
This will switch back to main branch.
Note: if `main` returns an error, try `master` instead.
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 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.
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `git checkout main` and hit enter
- Type `git merge branch-readme` and hit enter
- Type `cat README.md` to see your changes in Git
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
This will switch back to main branch.
Note: if `main` returns an error, try `master` instead.
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”.
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
- Open your README.md file in a markdown editor
- On a new line, type `## Save vs Commit`, then save and close the file
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `git status` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Made changes in markdown editor"` and hit enter
This new line has been saved to your README.md file, but is not being tracked by Git.
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
This will show that Git sees the change, but the change is not being tracked.
`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.
`git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.
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.
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `git log --oneline` and hit enter
- Type `git log` and hit enter
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
This will show a compact list of commits, with minimal details.
This will show a complete list of commits, with full details
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.
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `echo "This line is a mistake" >> README.md` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Oops"` and hit enter
- Type `git log --oneline` and hit enter
- Type `git reset --soft HEAD~1` and hit enter
- Type `git status` and hit enter
- Type `git commit -m "Revised commit message"`
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
`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”.
`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.
`git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.
This will show the "oops" commit and the HEAD of the list.
This will reset the commit.
Here you will see that previous changes are still ready to recommit.
`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 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
- Open Terminal/Command Prompt
- Type `cd git-practice` and hit enter
- Type `echo "This line is a mistake" >> README.md` and hit enter
- Type `git add README.md` and hit enter
- Type `git commit -m "Oops"` and hit enter
- Type `git log --oneline` and hit enter
- Type `git reset --hard HEAD~1` and hit enter
- Type `cat README.md` and hit enter to view your Git versions
Mac: open Terminal from Applications/Utility folder or by using Spotlight Search
Windows: open Command Prompt by typing "cmd" into Windows search bar
'cd' is a basic command-line tool, which stands for "change directory". This instructs Terminal/Command Prompt to work inside only this new folder.
`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.
`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.
`git commit` saves a snapshot of the file and `-m` allows you to add a description of the change.
This will show the "oops" commit and the HEAD of the list.
This will delete the commit. Your markdown file will no longer include the content "This line is a mistake".
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