Objective: This exercise aims to teach students how to manage a Git repository, including using git add
, git commit
, git restore
, git mv
, git rm
, git clean
, git revert
, git log
, and how to work with files in Git.
-
Go to a folder where you want to put the repository. Accept the assignment and clone the repository from GitHub by running:
git clone https://github.com/username/yourrepositoryurl.git
-
Navigate into the cloned directory:
cd foldername
-
Create a new file called
instructions.txt
and write a simple welcome message:echo "Welcome to My Git Project" > instructions.txt
-
Stage and commit the file:
git add instructions.txt
git commit -m "Add instructions.txt with welcome message"
-
Push the changes to GitHub:
git push
-
Modify the
instructions.txt
file by adding a section about the project description:echo "\n## Description" >> instructions.txt
echo "This project demonstrates basic Git and GitHub operations." >> instructions.txt
-
Stage and commit the changes:
git add instructions.txt
git commit -m "Update instructions.txt with project description"
-
Accidentally modify another section:
echo "\nAccidental addition" >> instructions.txt
-
Restore the last accidental addition:
git restore instructions.txt
-
Rename
instructions.txt
toguide.txt
:git mv instructions.txt guide.txt
git commit -m "Rename instructions.txt to guide.txt"
-
Accidentally add a temporary file:
touch temp.txt
git add temp.txt
-
Remove
temp.txt
from tracking and delete it:git rm temp.txt
git commit -m "Remove temporary file"
-
Clean up any untracked files (like if
temp.txt
was never added):git clean -f
-
Introduce a new change:
echo "\nThis will be reverted soon." >> guide.txt
git add guide.txt
git commit -m "Add line to be reverted"
-
Use
git log
to view the commit history:git log
-
Revert the last commit:
git revert HEAD --no-edit
-
Make additional changes to
guide.txt
:echo "\nThis project helps to understand Git basics." >> guide.txt
git add guide.txt
git commit -m "Update guide.txt with Git basics info"
-
Push the changes to GitHub:
git push