开发者

Do a Git pull to overwrite local changes

开发者 https://www.devze.com 2023-03-11 21:24 出处:网络
There has certainly been posts around for this, but I actually did a commit because I thought it was the right thing to do.

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 repl开发者_开发问答y

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?


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号