in Technology by
How can we force “git pull” to overwrite local files?

1 Answer

0 votes
by
 
Best answer

Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven't been pushed will be lost.[*]

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.


First, run a fetch to update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch:

git checkout -b backup-master

Then, you have two options:

git reset --hard origin/master

OR If you are on some other branch:

git reset --hard origin/<branch_name>

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master


Maintain current local commits

[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits.

Uncommitted changes

Uncommitted changes, however (even staged), will be lost. Make sure to stash and commit anything you need. For that you can run the following:

git stash

And then to reapply these uncommitted changes:

git stash pop

Related questions

0 votes
    I have an old, out-of-sync project code in the master branch. Several files have been deleted, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    My git workspace is dirty, there are some local modifications. When I use the command git pull origin master it ... Ansible git module? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How can we rename a local Git branch?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to remove local (untracked) files from the current Git working tree?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How do you delete untracked local files from your current working tree? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    We have a script running to disable guest access. Will this new default setting overwrite our script?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    How will you differentiate between git pull and git fetch?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    How will you differentiate between git pull and git fetch?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    I have a project consisting of a huge amount of data. Because of its size, I can't use a remote GIT repo and push/pull ... -ProjectGit | | |-objects |-project1 | |-refs | |-.git...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    What is the difference between 'git pull' and 'git fetch'?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    What is the difference between git pull and git fetch?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    Explain the difference between git fetch and git pull....
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    I pulled a project from GitHub a few days ago. I've since discovered that there are several forks on GitHub, ... those forks I pulled? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I have created a branch named dev. I have done a pull request to send dev code to master, when I do ... to resolve these conflicts. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I have noticed that some browsers (in particular, Firefox and Opera) are very zealous in using cached ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...