Do a Git pull to overwrite local changes

眉间皱痕 提交于 2020-01-11 15:41:11

问题


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

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.

Unfortunately, I changed files on production without stashing. How do I force overwrite instead of merge when trying a pull?


回答1:


If you want to entirely replace your local branch foo with the contents of the remote branch 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.




回答2:


You need to push from production first to GitHub:

git push origin yourbranch --force

The force will make sure that GitHub has what production has.

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

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.

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


来源:https://stackoverflow.com/questions/6257539/do-a-git-pull-to-overwrite-local-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!