git 如何强制从远程服务器签出最新版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1373304/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:49:14  来源:igfitidea点击:

How to force the checkout of the latest revision from a remote server?

git

提问by Christoph Schiessl

I have repository on GitHub to which I commit regularly from my local computer. On the other side I have server pulling from the repository. The web server just executes a git pullin order to get the latest changes from the GitHub repository. This is completely automated and should stay that way (solutions like the Ruby Tool Capistrano are out).

我在 GitHub 上有一个存储库,我定期从本地计算机提交。另一方面,我从存储库中提取服务器。Web 服务器只执行 agit pull以便从 GitHub 存储库中获取最新更改。这是完全自动化的,应该保持这种方式(像 Ruby 工具 Capistrano 这样的解决方案已经过时了)。

A simple git pullusually works just fine. However, sometimes I change the last commit (git commit --amend) and git pushthe changes twice to GitHub. If the server auto-updated it's code between the two pushes to GitHub, the next server side git pullfails because there is merge conflict.

一个简单的git pull通常工作得很好。但是,有时我会将最后一次提交 ( git commit --amend) 和git push两次更改更改为 GitHub。如果服务器在两次推送到 GitHub 之间自动更新它的代码,则下一个服务器端会git pull因为合并冲突而失败。

To solve this problem I need the following behavior: The server should continue to git pull(or something equivalent) the GitHub repository but in case of a merge conflict, the GitHub repository should just take precedence over the local repository on the server. So, I want a git command that behaves like git clone, but doesn't copy the whole repository every single time.

为了解决这个问题,我需要以下行为:服务器应该继续git pull(或等效的东西)GitHub 存储库,但在合并冲突的情况下,GitHub 存储库应该优先于服务器上的本地存储库。所以,我想要一个 git 命令,它的行为类似于git clone,但不会每次都复制整个存储库。

回答by Michael Krelin - hacker

You can of course do the forced git checkout if you don't do any local modifications there, but the right answer would be to never ever amend commits that have left your local repository.

如果您不在那里进行任何本地修改,您当然可以进行强制 git checkout,但正确的答案是永远不要修改已经离开本地存储库的提交

回答by Dan Moulding

JB's right, but I'm going to expand a bit on his answer.

JB 是对的,但我将扩展他的回答。

Instead of having the server automatically run git pull, you should have it run git fetch origin; git checkout origin/master. Of course, this assumes that you've got the "origin" remote set up to refer to your local computer's repo.

与其让服务器自动运行git pull,不如让它运行git fetch origin; git checkout origin/master。当然,这假设您已将“源”远程设置为引用本地计算机的存储库。

If you ever need to modify the server sources directly (for a quick fix, or whatever) and want to commit those changes, you can git branch -f master origin/masterbefore you do the commit to get the server's master branch set to the same commit as the local computer's branch.

如果您需要直接修改服务器源(为了快速修复或其他目的)并希望提交这些更改,您可以git branch -f master origin/master在提交之前将服务器的 master 分支设置为与本地计算机的分支相同的提交。

回答by JB.

I you never modify the sources you pull, what you probably want to do is just fetchthen checkout.

我你从不修改你的来源pull,你可能想做的就是fetch那时checkout