First, update all origin/<branch> references to the latest:
shellgit fetch --all
Backup the current branch (e.g., master):
shellgit branch backup-master
Reset the current branch to the latest commit origin/master:
shellgit reset --hard origin/master
Explanation:
git fetch downloads the latest version from the remote without merging or resetting anything.
git reset resets the master branch to the content you just fetched. The --hard option changes all files in the working tree to match origin/master.
Maintaining Current Local Commits
[*] Note: You can maintain current local commits by creating a branch before resetting master:
shellgit checkout master git branch new-branch-to-save-current-commits git fetch --all git reset --hard origin/master
After this, all previous commits will remain in new-branch-to-save-current-commits.
Uncommitted Changes
Uncommitted changes, even if staged (using git add), will be lost. Ensure you stash or commit any necessary changes. For example, run the following command:
shellgit stash
Later (after git reset), reapply these uncommitted changes:
shellgit stash pop
This may cause merge conflicts.