执行 Git pull 以覆盖本地更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6257539/
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
Do a Git pull to overwrite local changes
提问by Andre
There has certainly been posts around for this, but I actually did a commit because I thought it was the right thing to do.
肯定有相关的帖子,但我实际上做了一个提交,因为我认为这是正确的做法。
So, I have two repositories, one development and one production. I had to edit something in the production because it was an urgent bugfix, and now I have three files that are newer in the production than in the development.
所以,我有两个存储库,一个开发,一个生产。我不得不在生产中编辑一些东西,因为这是一个紧急的错误修复,现在我有三个文件在生产中比在开发中更新。
I committed the three files on the production and tried a pull, but it told me there were merge errors. I tried copying and pasting the new files to the development server and retrying the whole thing and it didn't work. Now I'm sure that what I need is on the development (since I copied and pasted into it) and committed, so how could I pull and overwrite the conflicting files?
我在生产中提交了三个文件并尝试了拉取,但它告诉我存在合并错误。我尝试将新文件复制并粘贴到开发服务器并重试整个过程,但没有成功。现在我确定我需要的是开发(因为我复制并粘贴到其中)并提交,那么我怎么能拉和覆盖冲突的文件呢?
---- Following up to @Seths reply
---- 跟进@Seths 的回复
Ok, I guess I do need to reword my question :)
I have three repositories. One development, one in GitHub and one production.
Usually to update production I just do a push from development to GitHub, git pull origin master
(from GitHub to production), and it works.
好的,我想我确实需要改写我的问题:) 我有三个存储库。一项开发,一项在 GitHub 中,一项生产。通常为了更新生产,我只是从开发到 GitHub git pull origin master
(从 GitHub 到生产)进行推送,并且它有效。
Unfortunately, I changed files on production without stashing. How do I force overwrite instead of merge when trying a pull?
不幸的是,我在没有隐藏的情况下更改了生产文件。尝试拉取时如何强制覆盖而不是合并?
回答by Seth Robertson
If you want to entirely replace your local branch foo with the contents of the remote branch origin/foo:
如果您想将本地分支 foo 完全替换为远程分支 origin/foo 的内容:
git fetch origin
git checkout foo
git reset --hard origin/foo
If you want to do something else, please reword your question. However, I might add the production Git repository as a remote and then merge the live changes in, instead of whatever you tried.
如果您想做其他事情,请改写您的问题。但是,我可能会将生产 Git 存储库添加为远程存储库,然后将实时更改合并到其中,而不是您尝试的任何内容。
回答by Adam Dymitruk
You need to push from production first to GitHub:
您需要先从生产推送到 GitHub:
git push origin yourbranch --force
The force will make sure that GitHub has what production has.
该力量将确保 GitHub 拥有生产所拥有的东西。
Here are the possibilities of what you could do:
以下是您可以执行的操作的可能性:
You will need to fetch the changes into your development repository in the deploy repository. At this point you will see that the history is branching (via git log --all --graph
or gitk --all
).
您需要将更改提取到部署存储库中的开发存储库中。此时,您将看到历史正在分支(通过git log --all --graph
或gitk --all
)。
git fetch origin
You can now rebase or merge to get your latest changes to be subsequent to the ones made on the production repository. This will enable you to push changes to your deploy repository at a later point.
您现在可以 rebase 或 merge 以使您的最新更改紧跟在生产存储库上所做的更改之后。这将使您能够在稍后将更改推送到您的部署存储库。
The conflicts are there for a reason. Look at them and resolve them, add and commit.
冲突是有原因的。查看它们并解决它们,添加并提交。
If you want the conflicts to be resolved by taking what is on the production side you can use the "recursive theirs" strategy:
如果您希望通过生产方面的内容来解决冲突,您可以使用“递归他们的”策略:
git merge -s recursive -Xtheirs production/yourbranch
If you want to take no changes from your side, merge normally, but when stopped at the conflicts, get the other side of the merge, add and commit.
如果您不想从您这边进行任何更改,请正常合并,但是当遇到冲突时,请获取合并的另一端,添加并提交。
git merge production/yourbranch
git checkout production/yourbranch -- .
git submodules update #this is optional and can be skipped if you don't have any submodules
git add -A
git commit
Now subsequent pushes to GitHub from development and pulls from GitHub in production will work.
现在,后续从开发推送到 GitHub 并在生产中从 GitHub 拉取都将起作用。
You could reset the branch, but that assumes that you don't want to keep any changes that you made on the development repository.
您可以重置分支,但前提是您不想保留对开发存储库所做的任何更改。
git reset --hard production/yourbranch