乐闻世界logo
搜索文章和话题

How do i force git pull to overwrite local files

1个答案

1

First, update all origin/<branch> references to the latest:

shell
git fetch --all

Backup the current branch (e.g., master):

shell
git branch backup-master

Reset the current branch to the latest commit origin/master:

shell
git 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:

shell
git 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:

shell
git stash

Later (after git reset), reapply these uncommitted changes:

shell
git stash pop

This may cause merge conflicts.

2024年6月29日 12:07 回复

你的答案