Join Our Community!

Connect with fellow tech enthusiasts on "The Weaver Birds" community. Share ideas, ask questions, and collaborate with others passionate about technology!

Join Now

Bring Us to Your Campus!

Interested in hosting a workshop? Contact us if your university wants to invite Engineeous to conduct a session on this topic or any other tech subject!

Request a Workshop

Let’s Work Together!

Looking for expert help? If you want to hire Engineeous for your next project, reach out to us today to discuss how we can collaborate!

Get in Touch

Undoing Work

Sometimes when you’re working on a project, you might make a mistake and want to undo it. Just like using the "undo" command in MS Word, Git lets you undo changes in your project.

Undoing Local Changes Before Staging

Let’s say you accidentally added something wrong to your file. You can undo it before you save or "commit" it to the repository.

First, make sure you’re on the latest version of the project (on the master branch).

  1. Make a Mistake: Let’s say you accidentally add this line to your index.html file:

    
    echo "<p>This line is a mistake, let's fix it.</p>" >> index.html
    
  2. Check the Status: To see the changes, run:

    
    git status
    

    You will see something like this:

    
    On branch master
    Changes not staged for commit:
        (use "git add ..." to update what will be committed)
        (use "git restore ..." to discard changes in working directory)
        modified:   index.html
    

    This tells you that index.html has been changed but hasn’t been staged yet.

  3. Undo the Change: To undo the change and go back to the previous version, use the git restore command:

    
    git restore index.html
    
  4. Check the Status Again: Now, if you run:

    
    git status
    

    You’ll see:

    
    On branch master
    nothing to commit, working tree clean
    

    The file is back to its original state, and your bad line is gone.

Undoing Staged Changes Before Committing

If you’ve already staged a change but haven’t committed it yet, you can still undo it.

  1. Make a Mistake and Stage It: Let’s add a bad line again:

    
    echo "<p>This line is a mistake, let's fix it.</p>" >> index.html
    

    Now stage it:

    
    git add index.html
    
  2. Check the Status: Run:

    
    git status
    

    You’ll see:

    
    On branch master
    Changes to be committed:
        (use "git restore --staged ..." to unstage)
            modified:   index.html
    

    This means the change is ready to be committed.

  3. Unstage the Change: If you don’t want to commit this change, you can "unstage" it with this command:

    
    git restore --staged index.html
    

    Now, the change is no longer staged, but it still exists in your working directory.

  4. Undo the Change: To completely remove the change from the file, run:

    
    git restore index.html
    
  5. Check the Status Again: After that, you can run:

    
    git status
    

    You’ll see:

    
    On branch master
    nothing to commit, working tree clean
    

    Your working directory is clean, and the line with mistake is gone.

Git branches

Subscribe to our newsletter

We will make sure that you never miss anything again! Stay rest assured about our emails as we do not spam your inbox.