2017年8月30日 星期三

[ Git 常見問題 ] How do I force git pull to overwrite everything on every pull?

Source From Here 
Question 
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 whateverand 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. 

How-To 
Really the ideal way to do this is to not use pull at all, but instead fetch and reset
# git fetch origin master // Fetch branch master from remote

// --hard: Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.
// FETCH_HEAD is a short-lived ref, to keep track of what has just been fetched from the remote repository.
# git reset --hard FETCH_HEAD

// -d: Remove untracked directories in addition to untracked files.
// -f, --force: Git will refuse to delete directories with .git sub directory or file unless a second -f is given.

# 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. 

Supplement 
FAQ- What does FETCH_HEAD in Git mean? 

沒有留言:

張貼留言

[ Py DS ] Ch1 - IPython: Beyond Normal Python

Source From  Here   Keyboard Shortcuts in the IPython Shell   If you spend any amount of time on the computer, you’ve probably found a u...