Reverting Commits
Objective: The objective of this assignment is to provide hands-on experience in reverting commits in Git. You will learn how to undo changes made by previous commits and practice reverting to a specific commit while preserving the commit history.
Instructions:
Part 1: Repository Setup
1. Task 1: Create a new directory/folder on your computer for this assignment and name it "GitRevertCommitsAssignment."
2. Task 2: Navigate to the "GitRevertCommitsAssignment" directory in your terminal or command prompt.
3. Task 3: Initialize a new Git repository in this directory.
4. Task 4: Create a new text file named "README.md" in the repository.
Part 2: Making Commits
1. Task 1: Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.
nano README.md # Use your preferred text editor
Part 1: Repository Setup
# Task 1
mkdir GitRevertCommitsAssignment
# Task 2
cd GitRevertCommitsAssignment
# Task 3
git init
# Task 4
touch README.md
Part 2: Making Commits
# Task 1
nano README.md # Use your preferred text editor
# Task 2
# Add content to README.md and save
# Task 4
git branch feature/branch-A
# Task 5
git checkout feature/branch-A
# Task 6
nano README.md
# Task 7
# Add content specific to branch A and save
# Task 8
git add README.md
git commit -m "Add branch-A specific content to README"
Part 3: Reverting Commits
# Task 1
# View git log to identify the commit to revert
# Task 2
git revert <commit-hash> # Replace <commit-hash> with the actual hash
# Task 3
# Edit the commit message, save, and exit
# Task 4
# View git log to confirm the revert
2. Task 2: Add some content to the "README.md" file. For example:
markdown
# Git Revert Commits Assignment
This is the main README file.
3. Task 3: Save the changes to the "README.md" file and exit the text editor.
4. Task 4: Create a new branch named "feature/branch-A."
git branch feature/branch-A
5. Task 5: Switch to the new branch.
git checkout feature/branch-A
6. Task 6: Open the "README.md" file again and add some content specific to branch A.
nano README.md
7. Task 7: Save the changes and exit the text editor.
8. Task 8: Commit the changes in "feature/branch-A."
git add README.md
git commit -m "Add branch-A specific content to README"
Part 3: Reverting Commits
1. Task 1: View the commit history to identify the commit you want to revert. Note the commit hash.
git log
2. Task 2: Use the `git revert` command to revert the commit you identified in Task 1.
git revert <commit-hash>
Replace `<commit-hash>` with the actual hash of the commit to revert.
3. Task 3: Edit the commit message in the text editor that appears. Save and exit the text editor.
4. Task 4: View the commit history again to confirm that the commit has been reverted.
git log
Solutions
Part 1: Repository Setup
# Task 1
mkdir GitRevertCommitsAssignment
# Task 2
cd GitRevertCommitsAssignment
# Task 3
git init
# Task 4
touch README.md
Part 2: Making Commits
# Task 1
nano README.md # Use your preferred text editor
# Task 2
# Add content to README.md and save
# Task 4
git branch feature/branch-A
# Task 5
git checkout feature/branch-A
# Task 6
nano README.md
# Task 7
# Add content specific to branch A and save
# Task 8
git add README.md
git commit -m "Add branch-A specific content to README"
Part 3: Reverting Commits
# Task 1
# View git log to identify the commit to revert
# Task 2
git revert <commit-hash> # Replace <commit-hash> with the actual hash
# Task 3
# Edit the commit message, save, and exit
# Task 4
# View git log to confirm the revert