📝 Exercise 1: Installing Git and Practicing Git Commands
Instructions: Complete the following activity to install and setup Git on your local machine, practice creating, editing and making changes to a file using Git by starting a simple shopping list text file.
🎯 When you've completed the activity, don't forget to click the 'Mark as done' button at the top left of this page. This helps track your course progress and ensures you receive your course badge without delay.
Part 1: Set Up Git on Your Local Machine
- Open a Terminal program on your computer (For more details, reference Lesson 3.1 Git: Installation.)
- Install Git
- Choose one of the following commands to run based on the operating system of your computer.
- Linux
$ sudo apt install git-all
- Note: You may need to refer to the official Git documentation depending on the version of Linux (ie. Ubuntu, Debian) that your machine is running.
- macOS
$ git --version
- Note: Running the command should prompt you to install Git if it is not yet installed.
- Windows
- Install Git on Windows by downloading packages from https://git-scm.com/downloads/win
- Configure Git (One-time setup, if haven't done so already):
$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"
Part 2: Initialize a Repository
- Create a new folder called
my-repo
(or choose your own folder name but make sure to stay consistent) for your project and initialize Git:
$ mkdir my-local-repo
$ cd my-local-repo
~/my-local-repo$ git init
-
- Note: Notice that the terminal prompt changed from just
$
to~/my-local-repo$
after the commandcd my-local-repo
is used to change into this new directory.
- Note: Notice that the terminal prompt changed from just
Part 3: Create and Save Your File
- Create a new file called
shopList.txt
and adding a first item:
~/my-local-repo$ echo "Milk" > shopList.txt
- Stage the file:
~/my-local-repo$ git add shopList.txt
- Commit the file:
~/my-local-repo$ git commit -m "Initial commit: add milk to shopping list"
Part 4: Edit, Stage, and Commit Again
- Add more items (note that
>>
is used to append to the file)~/my-local-repo$
echo "Juice" >> shopList.txt
~/my-local-repo$
echo "Eggs" >> shopList.txt
- Look at the status of your repo and list the changes:
~/my-local-repo$
git status
~/my-local-repo$
git diff
- Stage and commit the file:
~/my-local-repo$
git add shopList.txt
~/my-local-repo$
git commit -m "Add Juice and Eggs to shopping list"
Part 5: Undo a Change
- Add a new item (but pretend you changed your mind):
~/my-local-repo$
echo "Ice cream" >> shopList.txt
- Undo the unsaved change:
~/my-local-repo$
git restore shopList.txt
Tip: Explore more Git commands using git
--help
.
If you're feeling stuck completing this exercise, please feel free to pose questions or consult the Discussion Forum.