Clue Mediator

Reset a Git branch to a remote repository

πŸ“…October 6, 2021
πŸ—Git

Sometimes you may need to reset or discard all of your changes and reset your local branch to be just like the branch on the remote repository. So today we will show you how to reset a Git branch to a remote repository.

Checkout more articles on Git

Steps to reset a Git branch to a remote repository

  1. Save your current work
  2. Fetch origin branch and set it
  3. Remove untracked files

1. Save your current work

If you want to save the state of your current branch before doing this, you can use the following commands.

git commit -a -m "Project Backup"
git branch my-branch-bak

2. Fetch origin branch and set it

Now, setting your branch to exactly match the remote branch can be done in two steps.

git fetch origin
git reset --hard origin/<branch_name>
</branch_name>

3. Remove untracked files

Next, we have to use the git clean to remove the untracked files.

git clean -d -f
// OR
git clean -df

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! πŸ™‚