AnaF AnaF - 10 months ago 111
Git Question

Git: Push rejected

I have a team and we are working on a project. One of our teammembers created a repository on GitHub and add others as collaborators. My team member committed our code to this repository. I did changes in my part and when I try to commit it, I have an error. How can I commit changes to a repository in which I'm a collaborator?

That is what I did:

git remote add origin (added a repository where I'm a collaborator)

`git push origin master
! [rejected] master -> master (fetch first)
error: failed to push some refs to ''
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.

git pull origin master
warning: no common commits
remote: Counting objects: 145, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 145 (delta 67), reused 145 (delta 67), pack-reused 0
Receiving objects: 100% (145/145), 55.90 KiB | 0 bytes/s, done.
Resolving deltas: 100% (67/67), done.
* branch master -> FETCH_HEAD
* [new branch] master -> or/master
fatal: refusing to merge unrelated histories

I just want to update my part and commit it to the repository. I dont have my own repository.

Thank you

Answer Source

Looks like your two master branches have diverged.

# HEAD at your master
$ git checkout master

# your master on a new branch
$ git checkout -b diverged.master

# get origin's master
$ git checkout master
$ git fetch origin master
$ git reset --hard origin/master

# Create a new branch for the diverged feature
$ git checkout -b feature.branch

# Merge your diverged changes into the new feature branch
$ git merge diverged.master

# Do any conflict resolutions

# Merge feature branch to master
$ git checkout master
$ git merge feature.branch

# Push to remote
$ git push origin master


I missed the part about this being a brand new repo.. makes this slightly easier

# HEAD at your master
$ git checkout master

# your master on a new branch
$ git checkout -b diverged.master

# delete master branch
# git branch -D master

# pull master from origin
$ git pull origin master

# HEAD at origin's master
$ git checkout origin master
$ git pull # for good measure

# merge your changes
$ git merge diverged.master

# push your changes
$ git push origin master