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
- Remove the last commit from the remote git repository
- How to rename a local and remote Git branch
- Undo commit before push in Git
- Using branches in Git
- Using tags in 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.
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..!! π