Reset a Git branch to a remote repository
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
If you want to save the state of your current branch before doing this, you can use the following commands.
1 2 | 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.
1 2 | git fetch origin git reset --hard origin/<branch_name> |
3. Remove untracked files
Next, we have to use the git clean to remove the untracked files.
1 2 3 | git clean -d -f // OR git clean -df |
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂