How to discard all Local Changes/Commit and Pull Latest Data from Repo - GIT

You will learn how to discard all local changes in your git repository in this post. Local changes could be change in file, added directory or commit.

Step 1: Remove all untracked directories and files

 $ git clean -fd

The -f flag forces the clean, while the -d flag applies it to untracked directories, as well.

Step 2: Reset all changes in your project files

 $ git reset --hard origin/<branch name>

This will reset the branch. <branch name> is your local branch name.


My scenario:

I made changes in my local git repository and did two commits in main branch. I only realize my mistake later when I was trying to do the sync.

I switched to my working local branch (Branch-A)and made same changes and sync my code to remote repo. I created pull request and my changes in my branch(Branch-A)got pull to main branch in remote. During pull request I deleted my working branch in remote too.

Again later I got another task for same project so I created another branch (Branch-B)from main branch in remote git repository.

Now I want to update all my local git repositories. How can I do this?

I opened my Terminal/git-bash in vs code

 $ git clean -fd

$ git reset --hard origin/main


Hope this help you. :)





Post a Comment

Thank you for comment, I really appreciate your view.

–>