Undo or Discard – git commands

Most of the time we need to undo something that we changed in our code. In this tutorial we will learn commands of how to undo something.

Discard all changes in a file

We have a file called index.html with git diff we will see the uncommitted changes,

We just updated the title, in order to discard this we need to type git restore index.html

Undo a Deleted File

Suppose we deleted a file, we need to undo or discard the deletion.

In order to undo this we can write git restore index.html

Discard a Particular Chunk

If we change multiple lines of code inside of a particular file, we need to discard or undo a particular line,

If we need to discard the paragraph not the title, then we need to write git restore -p index.html, this will ask us which chunk to discard

We will move forward with pressing n.

Then we press the y. The second chunk will be discarded.

Discard everything

If we discard everything we need to write git restore .

These are some Discard commands.

Leave a Comment

Your email address will not be published. Required fields are marked *