Git

How to Discard Your Local Changes in Git Example

While working on Git, developers make changes to the Git local repository. After committing and saving changes, sometimes, you may realize that incorrect changes are saved accidentally. In this situation, it is required to discard the recent commit. For the specified purpose, the “& git reset –hard <commit-id>“ command can be executed on the Git bash terminal.

This manual will define the procedure to remove or discard your local changes in Git.

How to Discard Your Local Changes in Git?

To discard your local changes in Git, first, move to the Git root directory and create a new Git local repository, and immediately move to it. Initialize the created directory, create new files, and check the commit reference history of the Git repository using the “$ git commit -m <message>” command and copy the commit reference to which we want to revert. Lastly, execute the “& git reset –hard <commit-id>“ command to discard your local changes in Git.

Let’s move forward to implement the above scenario!

Step 1: Open Git Bash

Launch the Git terminal by using the “Startup” menu:

Step 2: Navigate to Git Root Directory

Move to the Git root directory using the “cd” command:

$ cd "C:\Users\nazma\Git"

Step 3: Create Git Local Repository

Execute the below-provided command to create and navigate to the new directory:

$ mkdir demo7 && cd demo7

Step 4: Initialize Git Repository

Now, initialize the newly created Git local repository using the “git init” command:

Step 5: Create and Make Changes

Next, execute the “touch” command with the “echo” command to create a file. Then, add some text to it using the redirect operator:

$ touch file1.txt && echo "It's my first file" > file1.txt

Step 6: Add File to Staging Area

Track the file using the “git add” command:

$ git add file1.txt

Step 7: Commit Changes

Run the “git commit” command to add the changes to the Git local repository with the specified message:

$ git commit -m "file1.txt added"

Step 8: Create and Update File

Next, create a new file named “file2.txt” and add some text to it:

$ touch file1.txt && echo "It's my second file" > file2.txt

Step 9: Add file

Now, add the required file from working area to the staging area:

$ git add file2.txt

Step 10: Commit Changes

Run the provided command to commit made changes with the commit message:

$ git commit -m "file2.txt added"

Step 11: Update File

Now, again open “file2.txt” and update it accordingly:

$ echo "update file2" > file2.txt

Step 12: Check Status

To check the Git local repository run the provided command:

$ git status .

As you can see that, the “file1.txt” file is modified successfully:

Step 13: Check Log

Check the Git local repository reference log history with the provided command:

$ git log --oneline

From the displayed information, copy the commit reference number to discard local changes:

Step 14: Discard Local Changes

Finally, execute the “git reset” command with the “–hard” flag and the copied commit reference to discard the related changes:

$ git reset --hard a4f1974

As you can see, our HEAD position is moved to the stated commit reference, and most recent changes are discarded:

Step 15: Check Status

Next, check the status using the provided command:

$ git status .

Step 16: Check List Content

Lastly, run the “ls” command to view the existing content of the Git repository:

$ ls

It can be seen that the “file1.txt” no longer exists in the repository anymore:

We have offered the procedure to discard your local changes in Git examples.

Conclusion

To discard your local changes in Git, first, move to the Git root directory, create a new Git local repository and navigate to it. Next, create new files, and open and update them. Then, commit changes to the Git repository. After that, again update the file and check the Git commit log reference, copy the commit reference and execute the “$ git reset –hard <commit-ref>” command to discard related commit changes. This manual discussed the method to remove your local changes in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.