top of page

Undoing Local Changes

Objective: The objective of this assignment is to learn how to undo local changes in a Git repository. You will practice reverting changes made to files, discarding changes, and resetting your working directory to a previous state.


You've made changes to files in your working directory but want to discard them and revert to the last committed state.



Instructions:


Part 1: Repository Setup


1. Create a new directory on your computer for this assignment and name it "GitUndoLocalChangesAssignment."


2. Navigate to the "GitUndoLocalChangesAssignment" directory in your terminal or command prompt.


3. Initialize a new Git repository in this directory.


4. Create a new text file named "README.md" in the repository.


Part 2: Making Changes


1. Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.


2. Add some content to the "README.md" file. You can type anything you like.


3. Save the changes to the "README.md" file and exit the text editor.


Part 3: Undoing Changes


1. Use the following Git commands to undo the changes you made to the "README.md" file:


   - Discard changes in the working directory:


     

     git checkout -- README.md

     


   - Discard changes and unstage the file (if it was staged):


     

     git reset HEAD README.md

     


Part 4: Viewing the Status


1. Use the following Git command to check the status of your repository after undoing changes:


   

   git status

   


   Ensure that the status shows that there are no modifications or untracked files.



Solutions


Part 1: Repository Setup



# Create a directory

mkdir GitUndoLocalChangesAssignment


# Navigate to the directory

cd GitUndoLocalChangesAssignment


# Initialize a Git repository

git init


# Create a text file

touch README.md



Part 2: Making Changes



# Open the file in a text editor

nano README.md  # Use your preferred text editor


# Add content to README.md and save



Part 3: Undoing Changes



# Discard changes in the working directory

git checkout -- README.md


# Discard changes and unstage the file (if it was staged)

git reset HEAD README.md



Part 4: Viewing the Status



# Check the status of the repository

git status

bottom of page