将 git“生产分支”拉到生产服务器的“正确”方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13135601/
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
"Proper" way to pull git "production branch" to production server
提问by dropson
I'm quite new to Git, and have read the Pro Git book @ http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging. My question is if what I'm doing today is the prefered way to work with production server and a remote Git repo.
我对 Git 很陌生,并且已经阅读了 Pro Git 书 @ http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging。我的问题是我今天所做的是否是使用生产服务器和远程 Git 存储库的首选方式。
I have my repo hosted on GitHub, using three remote branches: developing -> testing -> master.
我的存储库托管在 GitHub 上,使用三个远程分支:开发 -> 测试 -> 母版。
Whenever the 'testing' branch has been verified to be working, I git merge
'master' with 'testing' (fast forward?) locally, and git push
'master' to GitHub.
每当 'testing' 分支被证实可以工作时,我git merge
在本地用 'testing'(快进?)git push
'master' ,然后'master' 到 GitHub。
On the production server (/opt/www/url.com/) I have done:
在生产服务器(/opt/www/url.com/)上我做了:
git init
git remote add origin https://github.com/.....
git pull origin master
Now everytime I want to update the master I issue a git pull
现在每次我想更新主人我都会发出一个 git pull
git pull origin master
Production will never push any changes to remote. And local changes won't happen. Am I doing it correct? If not, what is the prefered way to pull updates to production?
生产永远不会将任何更改推送到远程。并且不会发生本地更改。我做得对吗?如果没有,将更新拉到生产中的首选方法是什么?
Edit #1: I don't want to manage any conflicts on the production server, which I am facing now (somehow I've done something wrong). I simply want to pull down the latest updated master branch, and switch to it.
编辑 #1:我不想管理我现在面临的生产服务器上的任何冲突(不知何故我做错了)。我只是想拉下最新更新的主分支,然后切换到它。
回答by Bob Kerns
Doing 'git pull' is pretty ordinary for this, but there's a more bullet-proof way.
为此,执行 'git pull' 非常普通,但还有一种更防弹的方法。
There wouldn't be any conflict, if somehow you didn't make local changes. But if you just want to get an up-to-date tree, and blow away any local changes, there's a better way, that ignores anything local:
如果不知何故您没有进行本地更改,就不会有任何冲突。但是,如果您只想获得最新的树,并消除任何本地更改,则有更好的方法,忽略本地的任何内容:
First, you need to get the latest from the server. Rather than 'git pull', for this, we'll use:
首先,您需要从服务器获取最新信息。为此,我们将使用:
git fetch origin master
This does the first half of a 'git pull'. (The second is the merge into your local master, which is where you ran into problems).
这是“git pull”的前半部分。(第二个是合并到您的本地 master,这是您遇到问题的地方)。
Git fetch does:
Git fetch 执行以下操作:
- Finds what commit master points to on the remote
- Downloads that commit and everything it references that you don't have locally.
- Updates the origin/master ref to point to that commit, to remember where the remote is on its version of 'master'.
- 查找提交 master 指向远程的内容
- 下载提交的内容及其引用的您本地没有的所有内容。
- 更新 origin/master ref 以指向该提交,以记住遥控器在其“master”版本上的位置。
Next, we'll simply update our local 'master' to point to the same commit, and update our working tree.
接下来,我们将简单地更新我们的本地“master”以指向相同的提交,并更新我们的工作树。
For this, we'll use:
为此,我们将使用:
git reset --hard origin/master
(This is presuming you didn't use a different name for your remote repo, instead of the default 'origin' -- adjust accordingly if you did).
(这是假设您没有为远程存储库使用不同的名称,而不是默认的“来源”——如果您这样做了,请相应地进行调整)。
This actually does three things:
这实际上做了三件事:
- Updates your local master branch to point to the same commit you just fetched from the remote.
- Loads that tree into your index
- Updates your working tree to match the index.
- 更新您的本地 master 分支以指向您刚刚从远程获取的相同提交。
- 将该树加载到您的索引中
- 更新您的工作树以匹配索引。
You now have a local master branch and tree that match the remote.
您现在有一个与远程匹配的本地主分支和树。
But there's one more step, which is to clean up anything left over, that's not being tracked by git.
但是还有一个步骤,那就是清理任何剩余的东西,git 没有跟踪它。
git clean -fdx
Will remove any files created by prior builds and give you a clean environment. You should be copying any build artifacts you want to preserve to some other location in any case.
将删除先前构建创建的任何文件,并为您提供干净的环境。在任何情况下,您都应该将要保留的任何构建工件复制到其他位置。
'git pull' is aimed at developers coordinating their work, so it's really not the ideal tool for this job. 'git fetch' and 'git reset --hard' are more specific, without the protections against accidental overwrite of local work.
'git pull' 旨在让开发人员协调他们的工作,因此它确实不是这项工作的理想工具。'git fetch' 和 'git reset --hard' 更具体,没有防止意外覆盖本地工作的保护。
But that's exactly what you want here.
但这正是您想要的。
Still, if you sometimes make changes to fix problems on the production server, you might want to continue to use 'git pull'. Merges and possible conflicts may be more helpful than harmful.
尽管如此,如果您有时进行更改以修复生产服务器上的问题,您可能希望继续使用“git pull”。合并和可能发生的冲突可能比有害更有帮助。
But sometimes you just want reliable updating as part of your automation, and any local changes are a problem to be eliminated.
但有时您只想将可靠的更新作为自动化的一部分,而任何本地更改都是需要消除的问题。
But you probably want that 'git clean' even with 'git pull'. Left-over files can screw up builds and deployments in mysterious and hard-to-identify ways.
但是你可能想要'git clean',即使是'git pull'。遗留文件可能会以神秘且难以识别的方式搞砸构建和部署。
回答by Andreas Bergstr?m
You should use:
你应该使用:
git init --bare
To initialize a repo that won't have a working tree, which means there will be no conflicts.
初始化一个没有工作树的 repo,这意味着不会有冲突。