Git Tips - Reset a branch to remote status | Git in-use

Posted by Phong To on Sat, Feb 4, 2023

TL;DR: Simply use

1git reset --hard origin/branch_name

Resetting a branch to a corresponding remote branch in Git can be useful if you want to start fresh or discard complicated changes with the latest changes from the remote repository.

Here are the steps that might help you:

Fetch latest updates

Firstly, you need to ensure you have the latest changes from the remote repository. You can do this by using the following command:

1git fetch <remote_name>
2
3# For example
4git fetch origin

Checkout the Local Branch

Checkout the local branch you want to reset using the following command (can be ignored it if you are already in this branch):

1git checkout <branch_name>

Execute reset branch

Now, reset the branch to the corresponding remote branch using the following command:

1git reset --hard origin/<branch_name>

The –hard option discards any changes you made in the local branch and replaces them with the contents of the remote branch.

All done - Final note

And that’s it! You got the branch reset to its corresponding remote branch. Please be aware that this operation is not reversible, so make sure you know exactly what you are about to do, or getting a backup before doing it.

Thanks for reading!



comments powered by Disqus