0

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?

Mario
  • 4,784
  • 3
  • 34
  • 50
  • Yes, do what the message says and pull (or rebase onto) the changes from the remote. – melpomene Mar 08 '18 at 14:26
  • This is what I am doing but this create commits that looks like `Merge branch 'master' of https://work.git.beanstalkapp.com/app` and this is what i want to avoid – Mario Mar 08 '18 at 14:30
  • Not if you rebase your work on top of the remote stuff. – melpomene Mar 08 '18 at 14:30
  • I updated the question, in order to highlight the scenario i want to avoid – Mario Mar 08 '18 at 14:33
  • Possible duplicate of [Github "Updates were rejected because the remote contains work that you do not have"](https://stackoverflow.com/questions/18328800/github-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-h) – user1012506 Dec 06 '18 at 07:25

2 Answers2

2

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.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • What causes the need to use rebase? Why I can not do a simple pull ?, or is this his normal behavior? I have worked with git but in a solitary way, this is the first project that I work in collaboration – Mario Mar 08 '18 at 14:47
1

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.

TPouliquen
  • 114
  • 2
  • 14