Sometimes you need to discard all your local commits and reset your branch to match the remote repository. Here are the most common methods to achieve this.
Method 1: Reset to Remote Branch (Recommended)
This will discard all local commits and reset your branch to match the remote:
git reset --hard origin/main
Replace main
with your branch name (e.g., master
, develop
, etc.).
Method 2: Fetch Latest and Reset
If you want to ensure you have the latest remote changes first:
git fetch origin
git reset --hard origin/main
git clean -fd
Method 3: Reset to Specific Remote Branch
If you're working on a different branch:
git reset --hard origin/your-branch-name
Method 4: Soft Reset (Keeps Changes as Unstaged)
If you want to keep your changes but remove the commits:
git reset --soft origin/main
Method 5: Mixed Reset (Default)
Removes commits but keeps changes as unstaged:
git reset origin/main