如何强制 git pull 在每次拉动时覆盖所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9589814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force git pull to overwrite everything on every pull?
提问by bmilesp
I have a CENTRAL bare repository that has three developer repositories pulling and pushing to it normally.
我有一个 CENTRAL 裸存储库,它有三个开发人员存储库,可以正常拉动和推送到它。
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.
我还有另外两个从 CENTRAL 裸仓库中提取的存储库:一个是实时服务器,另一个是测试/阶段服务器——每个都从各自的分支中提取。
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.
场景是这样的:我post-update
在 CENTRAL 存储库上有一个钩子脚本,它可以自动访问测试和实时存储库并在每个存储库上运行拉取命令。这会更新测试服务器和实时服务器,这一切都取决于哪个分支有新的提交。这一切都很好。
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.
问题是这样的:有时可能会在紧急情况下直接在服务器上更新文件(通过 ftp 或其他方式),然后 CENTRAL 更新后脚本将失败,因为会发生合并/覆盖冲突。这种情况没有办法避免,而且不可避免。
What I would like to have happen is this: I want the pull from the live and test sites to alwaysoverwrite/merge on pull. Always.These repos will be pull-only as they are not for development.
我希望发生的是:我希望来自实时站点和测试站点的拉取总是在拉取时覆盖/合并。总是。这些 repos 将是 pull-only,因为它们不用于开发。
In all my research, I cannot find a good solution to have a pull alwaysforce an overwrite of the local files. Is this at all possible? It would make for a great development scenario if so.
在我所有的研究中,我找不到一个好的解决方案来让拉总是强制覆盖本地文件。这是可能吗?如果是这样,那将是一个很好的发展场景。
回答by Amber
Really the ideal way to do this is to not use pull
at all, but instead fetch
and reset
:
真正做到这一点的理想方法是根本不使用pull
,而是使用fetch
和reset
:
git fetch origin master
git reset --hard FETCH_HEAD
git clean -df
(Altering master
to whatever branch you want to be following.)
(更改master
到您想要关注的任何分支。)
pull
is designed around merging changes together in some way, whereas reset
is designed around simply making your local copy match a specific commit.
pull
旨在以某种方式将更改合并在一起,而reset
旨在简单地使您的本地副本与特定提交匹配。
You may want to consider slightly different options to clean
depending on your system's needs.
您可能需要clean
根据系统需要考虑略有不同的选项。
回答by user1251007
You could try this:
你可以试试这个:
git reset --hard HEAD
git pull
(from How do I force "git pull" to overwrite local files?)
Another idea would be to delete the entire git and make a new clone.
另一个想法是删除整个 git 并创建一个新的克隆。
回答by Matt Wolfe
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
回答by Andrew Atkinson
To pull a copy of the branch and force overwrite of local filesfrom 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
所有当前工作都将丢失,然后它将与原始分支相同
回答by Dzmitry
git reset --hard HEAD
git fetch --all
git reset --hard origin/your_branch
回答by Maviles
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
checkout
将使用最后一次本地提交清除您的本地更改,
pull
并将其同步到远程存储库
回答by Dietrich Epp
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 ...