Discarding Local Changes in a File

1. Checking out the main branch

git switch main

2. Change index.html

HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h2>Welcome</h2>
<p>This is the paragraph i want to revert !!!</p>
</body>
</html>


how to discard changes in git

3. Check the status

git status

4.Discarding the changes in the working directory

git checkout index.html
git status
cat index.html

how to discard changes in git

How to Discard Changes in Git?

In Git, “discard changes” refers to reverting modifications made to files in your working directory back to their state in the last commit, effectively disregarding any changes you’ve made since then. This action can be useful if you’ve made changes that you no longer want to keep or if you want to start over from the last committed state.

Table of Content

  • Steps to Setup Github
  • Discarding Local Changes in a File
  • Discarding All Local Changes
  • Saving Changes on the Stash

Similar Reads

Steps to Setup Github

1. Open GitHub by typing the following URL into the browser...

Discarding Local Changes in a File

1. Checking out the main branch...

Discarding All Local Changes

1. Modifying local Files...

Saving Changes on the Stash

The “git stash” command can help you to (temporarily but safely) store your uncommitted local changes – and leave you with a clean working copy....