git reset local repository

How to Reset Local Git Repository to Remote

Sometimes you may need to make your local git branch same as its repository. In such cases, you need to reset git repository to remote. Here is how to reset local git repository to remote.


How to Reset Local Git Repository to Remote

Here are the steps to reset your local git branch to its remote counterpart.


1. Save Your Current Work (Optional)

When you reset your local repository to remote one, you will lose all local changes made after the last commit of your remote repository. If you want to save your current branch’s work before you reset it, then run the following commands to save your current work into a separate branch. Update the commit message and branch name in following commands.

$ git commit -a -m "Type message here"
$ git branch <new branch name>


2. Fetch Origin

Run the following command to fetch the remote repository.

$ git fetch origin


3. Reset Local Repository

Run the following command to reset local repository.

$ git reset --hard origin/master

The above command will reset your master. If you want to reset a specific branch run the following command instead. Replace <branch name>. Here we have assumed that you have earlier created a remote branch with name <branch name>. The following command won’t work if you don’t have a remote branch configured.

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


4. Clean up (Optional)

The above command may result in a few untracked files, run the following command to list all untracked files that can be removed without actually deleting them.

$ git clean -n -f

After that, run the following command to actually delete untracked files & folders from your repository.

$ git clean -f

That’s it. In this article, we have learnt how to make local branch same as its remote one.

Also read:

Cannot Access NGINX from Outside
How to Undo Git Rebase
How to Stop Tracking Folder in Git Without Deleting
Bash Loop Through Files in Directory
How to Remove Git Ignore Files from Git Repository

Leave a Reply

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