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 sync with a GitHub repository.
-
Create a new directory called
git-github-exercise
and navigate into it:mkdir git-github-exercise
cd git-github-exercise
-
Initialize a new Git repository:
git init
-
Create a
README.md
file and write a simple welcome message:echo "# Welcome to My Git Project" > README.md
-
Stage and commit the file:
git add README.md
git commit -m "Add README.md with welcome message"
-
Modify the
README.md
file by adding a section about the project description:echo "\n## Description" >> README.md
echo "This project demonstrates basic Git and GitHub operations." >> README.md
-
Stage and commit the changes:
git add README.md
git commit -m "Update README.md with project description"
-
Accidentally modify another section:
echo "\nAccidental addition" >> README.md
-
Restore the last accidental addition:
git restore README.md
-
Rename
README.md
toINTRO.md
:git mv README.md INTRO.md
git commit -m "Rename README.md to INTRO.md"
-
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." >> INTRO.md
git add INTRO.md
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
-
Create a new repository on GitHub named
git-github-exercise
(do not initialize with README, .gitignore, or License). -
Link your local repository to your GitHub repository:
git remote add origin https://github.com/yourusername/git-github-exercise.git
- Replace
yourusername
with your actual GitHub username.
-
Push your local repository to GitHub:
git push -u origin main
-
Make additional changes to
INTRO.md
:echo "\nThis project helps to understand pushing to remote repositories." >> INTRO.md
git add INTRO.md
git commit -m "Update INTRO.md with remote repo info"
-
Push the changes to GitHub:
git push