How do I force git pull to overwrite everything on every pull?

GitGit Pull

Git Problem Overview


I have a CENTRAL bare repository that has three developer repositories pulling and pushing to it normally.

I also have two other repositories that pull from the CENTRAL bare repo: one is the live server, and the other is a test/stage server—each pulling from its own respective branch.

The scenario is this: I have a post-update hook script on the CENTRAL repo that automatically accesses the test and live repos and runs a pull command on each. This updates both test and live servers, all depending on what branch has new commits. This all works great.

The problem is this: there may be times in an emergency that files may be directly updated on the server (via ftp or whatever) and the CENTRAL post-update script will then fail since merge/overwrite conflicts will occur. There is no way to avoid this scenario, and it is inevitable.

What I would like to have happen is this: I want the pull from the live and test sites to always overwrite/merge on pull. Always. These repos will be pull-only as they are not for development.

In all my research, I cannot find a good solution to have a pull always force an overwrite of the local files. Is this at all possible? It would make for a great development scenario if so.

Git Solutions


Solution 1 - Git

Really the ideal way to do this is to not use pull at all, but instead fetch and reset:

git fetch origin master
git reset --hard FETCH_HEAD
git clean -df

(Altering master to whatever branch you want to be following.)

pull is designed around merging changes together in some way, whereas reset is designed around simply making your local copy match a specific commit.

You may want to consider slightly different options to clean depending on your system's needs.

Solution 2 - Git

You could try this:

git reset --hard HEAD
git pull

(from https://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull)

Another idea would be to delete the entire git and make a new clone.

Solution 3 - Git

git reset --hard HEAD
git fetch --all
git reset --hard origin/your_branch

Solution 4 - Git

To pull a copy of the branch and force overwrite of local files from the origin use:

git reset --hard origin/current_branch

All current work will be lost and it will then be the same as the origin branch

Solution 5 - Git

I'm not sure how to do it in one command but you could do something like:

git reset --hard
git pull

or even

git stash
git pull

Solution 6 - Git

You can change the hook to wipe everything clean.

# Danger! Wipes local data!

# Remove all local changes to tracked files
git reset --hard HEAD

# Remove all untracked files and directories
git clean -dfx

git pull ...

Solution 7 - Git

If you haven't commit the local changes yet since the last pull/clone, you can use:

git checkout *
git pull

checkout will clear your local changes with the last local commit, and pull will sincronize it to the remote repository

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbmilespView Question on Stackoverflow
Solution 1 - GitAmberView Answer on Stackoverflow
Solution 2 - Gituser1251007View Answer on Stackoverflow
Solution 3 - GitDzmitryView Answer on Stackoverflow
Solution 4 - GitAndrew AtkinsonView Answer on Stackoverflow
Solution 5 - GitMatt WolfeView Answer on Stackoverflow
Solution 6 - GitDietrich EppView Answer on Stackoverflow
Solution 7 - GitMavilesView Answer on Stackoverflow