-1

I'm a collaborator on a project. The owner usually works on different files. How can I avoid that the master diverges?

The situation on github

fileA # modified by owner
fileB
fileC

Locally:

fileA
fileB
fileC # modified by me

I noted that fileA was changed. Thus I tried git fetch, since I aimed to updated fileA locally. Then I committed my fileC and executed git push, which now yields

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to ...

Then it suggests git pull and I get some merging stuff. How can I avoid those problems (and branches)? I see git push, git rebase, git fetch.

  • 1
    You got "some merging stuff"? You mean a merge conflict? Did you research how to resolve merge conflicts in Git? Except in some extreme cases (like binary or auto generated XML files) it's pretty simple to resolve. Seems like you would benefit from a basic Git tutorial that teaches you what it means to fetch, merge, push, or pull. – mason Jun 15 '20 at 18:26
  • Looked many times for this. I just want to avoid the situation in advance. I always have to look it up and I always loose the overview. – Friedrich -- Слава Україні Jun 15 '20 at 18:31
  • 3
    You can't avoid it. If two people are working on the same files at the same time, there will be conflicts. Learn to embrace it. It's just part of being a developer. It will happen often enough that you'll figure it out quickly. Until then, just bookmark whatever help you find and/or take notes and refer back to them. – mason Jun 15 '20 at 18:33

1 Answers1

-1

Merging can be avoided with (see https://stackoverflow.com/a/43262939/11769765)

git pull --rebase --autostash

This downloads the latest files (here fileA), while it does not affect the local changes.

In case the local changes were already committed (! [rejected] master -> master (fetch first)), one can do in advance git reset HEAD. Thus the full recipe is

git reset HEAD                 # undo commit, if needed, keeps local changes
git pull --rebase --autostash  # keeps local changes
git commit -m "...." fileC
git push