How to avoid repetitive message `Updates were rejected because the remote contains work that you do…`?

让人想犯罪 __ 提交于 2019-12-23 05:38:10

问题


I am working in a project with three more collaborators, my case is:

Every time I try to add a new commit and there is some change in the remote (even though it is a file that I have not worked in local), I get the following message that forces me to create a merge with following default message:

error: failed to push some refs to 'https://work.git.beanstalkapp.com/app.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This scenario is only avoided if there are no changes in the remote.

This causes many commits that look like Merge branch 'master' of https://work.git.beanstalkapp.com/app in the commit history, and I want to avoid that.

I found a related question, for some people using git push -f origin master is working but using --force worry me. I do not want to damage the project.

How can I achieve that?


回答1:


You want to perform a rebase of your local work onto the remote branch. You can do this by adding the --rebase flag to git pull:

git pull --rebase origin branch

Alternatively you can fetch the remote and then rebase explicitely:

git fetch origin
git rebase origin/branch

Note that this flattens any merges you explicitely did locally. You may need to add --preserve-merges / --rebase=preserve to avoid that (read the man pages for detailed explanation).

Also keep in mind that rebase rewrites history! After a rebase is done the commit IDs of the rebased commits will have changes.




回答2:


If you work on really distinct files, it can make sense to separate your work on different Git branches.

With each one working on its own you won't have this message.

And you can merge the new branches in a common one when you need it.



来源:https://stackoverflow.com/questions/49175460/how-to-avoid-repetitive-message-updates-were-rejected-because-the-remote-contai

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