Git reset to remote: Reset local repository branch to be just like remote repository HEAD

1.2K    Asked by asutos_8102 in Devops , Asked on Apr 20, 2021

 How to reset local branch to remote repository?

I did:
git reset --hard HEAD
But when I run a git status,
On branch master
Changes to be committed:
  (use "git reset HEAD ..." tounstage)
   modified:   java/com/mycompany/TestContacts.java
   modified:   java/com/mycompany/TestParser.java

Can you please tell me why I have these 'modified'? I haven't touched these files? How to remove these modified.

Answered by Asutosh Jha

Before I mention the steps, here's a tip if you want to save your current branch's state:

git commit -a -m "Saving my work in my-saved-work branch"
git branch my-saved-work

Now let us set our branch to exactly match the remote branch:

git fetch origin
git reset --hard origin/master

Here, the remote repository named "origin" and that the branch named "master" in the remote repository matches the currently checked-out branch in your local repository.

Now , If you want to remove those untracked files, use the following commands. But before performing clean take a look at the files that will be cleaned.

  git clean -n -f

Now if you think these files add no value and can be removed, use the following command:

  git clean -f


Your Answer

Interviews

Parent Categories