Day 4 of 100 Days: Git Command Exercise

In today's exercise, we will explore a series of essential Git commands that are crucial for version control and collaboration. I have included most of the commonly used commands in Git to give you a comprehensive understanding of how to work effectively with it. This hands-on practice will help you become more comfortable with version control as you navigate through common tasks. Let’s dive into the steps!

Exercise Steps

1. Clone a Repository

Start by cloning an existing repository into a new directory.

git clone https://github.com/user/sample-repo.git
cd sample-repo

2. Initialize a New Git Repository

Create a new folder for your project and initialize a new Git repository.

mkdir new-project
cd new-project
git init

3. Create and Add Files

Create a new file called example.txt and add some content to it.

echo "This is an example file." > example.txt
git add example.txt

4. Check Status

Check the status of your working directory and staging area.

git status

5. Commit Changes

Commit the changes you made to the example.txt file.

git commit -m "Add example.txt with initial content"

6. Rename a File

Rename example.txt to sample.txt.

git mv example.txt sample.txt

7. Remove a File

Create another file called temp.txt and then remove it.

echo "Temporary file." > temp.txt
git add temp.txt
git commit -m "Add temp.txt"
git rm temp.txt
git commit -m "Remove temp.txt"

8. View Commit History

Show the commit logs to see your history.

git log

9. Create a Branch

Create a new branch called feature-branch and switch to it.

git branch feature-branch
git switch feature-branch

10. Make Changes and Commit on the New Branch

Modify the sample.txt file and commit the changes.

echo "Additional content for sample.txt." >> sample.txt
git add sample.txt
git commit -m "Add additional content to sample.txt"

11. Switch Back to the Main Branch

Switch back to the main branch.

git switch main

12. Merge Changes from the Feature Branch

Merge the changes from feature-branch into main.

git merge feature-branch

13. Tag the Commit

Tag the current commit on the main branch.

git tag -a v1.0 -m "Release version 1.0"

14. Push Changes to Remote Repository

Push your changes to the remote repository.

git push origin main
git push origin feature-branch
git push origin --tags

15. Fetch and Pull Changes from Remote

Simulate collaboration by fetching and pulling changes from the remote repository.

git fetch
git pull

16. Examine Differences

Check the differences between the working directory and the last commit.

git diff

17. View a Specific Commit

Show the details of a specific commit (use the commit hash from git log).

git show <commit-hash>

By completing this exercise, you've gained hands-on experience with a wide range of Git commands that are essential for effective version control and collaboration. Keep practicing, and you'll become more proficient over time!