3

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 https://github.com/xxx/xxx.git (added a repository where I'm a collaborator)

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

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.
From https://github.com/xxx/xxx
* 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

AnaF
  • 63
  • 2
  • 2
  • 9
  • 1
    *"warning: no common commits"* - did you both start from an empty repo? Sure you've got the right remotes? – jonrsharpe Nov 19 '16 at 20:12
  • We all have the same code on our laptops. Then, my temmember created a repo and commited all the code into it. I made a little change to my part of code and now I cant commit it(( – AnaF Nov 19 '16 at 20:14
  • Did you pull from that repo before trying to add in your changes? If you started from a pre-repo version of the code, you'll need to clone it *then* add in your changes. – jonrsharpe Nov 19 '16 at 20:15
  • I did some changes and pull after that. What can I do now? How can I clone it in a right way? thank you, I spent all this day trying to find an answer – AnaF Nov 19 '16 at 20:17
  • 1. Clone the repo to a fresh directory. 2. Copy your code into that directory, either file by file or sub-directory by sub-directory. Don't try to replace the whole root. 3. Add and commit those changes, then push them. Before or after, read an intro to Git tutorial. – jonrsharpe Nov 19 '16 at 20:18

1 Answers1

4

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

Edit

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
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • glad to hear it! git can be a really powerful tool, and I suggest you read up on it before trying to use it in a team environment. If you are unsure what you are doing, as you can see, it will get pretty messy pretty fast... happy coding :D – Matt Clark Nov 19 '16 at 20:26