11

I have a remote git repository and a local one that i work with. Whenever i do any changes locally, i push them to the remote. Then i sometime do a "git commit" on the remote one to store the changes on the remote files.

I do not edit the remote repository directly at all. I just commit the changes. And i'm a single developer, no one else works on that repos.

Why do i get an error that, from what i know, means that i have to pull first ?

I don't want to pull because the remote repos files are outdated and it will lose my local changes. This is really annoying, why does this happen ? And how can i fix without having to pull or recreate the repository ? (as you can see, this is sort of like a subversion type of version control style here)

EDIT - The error :

To ssh://...
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://...'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
Spyros
  • 46,820
  • 25
  • 86
  • 129
  • What do you mean you do a "git commit" on the remote repository? Is the remote repository a bare repo? You should generally only be pushing to a bare repo, but you can't commit to a bare repo, so I'm a bit confused about what you are trying to do. Can you show an example of the actual commands that you are running and the actual output you are getting? – Brian Campbell Apr 14 '11 at 20:34
  • well, i was a bit suspicious about that part, that is why i mention it as well. The repos is created with 'git init'. I do a commit on the remote repository, because i don't see the changes in that repository otherwise. I'm adding code to show the error – Spyros Apr 14 '11 at 20:37

5 Answers5

11

What you should be doing is creating the remote repository as a bare repository. A bare repository is just the git repo, without a current checkout (that is, it is like just the contents of the .git dir in a regular Git repo, so it contains objects and refs, but it doesn't have an index or working copy of the file hierarchy). If you try pushing to a non-bare repository, the working copy will get out of sync with what is committed, and cause the kinds of problems you are seeing here.

You can create a bare repository using git init --bare repo.git. Or you can clone an existing repository as a bare repo using git clone --bare original-repo new-repo.git.

If you want to have a checked out copy of the repository on your server, you will need to create a new, non-bare repo on the server, and then pull into that repo from the bare repo that you push to.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • So, if i understand correctly, the idea is that the bare repository is like an index that has an internal way of representing the saved data and then you just clone a working (or backup) copy. – Spyros Apr 14 '11 at 20:50
  • 1
    @SpyrosP Basically. A bare repository stores just the history, branches, and tags, but doesn't have any sort of working copy of one particular version, the way a normal repo does. You can clone a bare repo to create a normal repo which has a working copy, that you do your work in. – Brian Campbell Apr 14 '11 at 20:53
  • 1
    Thanx for clarifying, that saves the day for sure :) – Spyros Apr 14 '11 at 20:54
8

this always means that you didn't synchronize the remote repository with local repo,so first you should synchronize them by using command git pull as following:

git checkout master    
git pull origin master 

after this process ,you will synchronize them ,and then you can push changes to remote repo by followings:

git add [filename/directory]  
git commit -m"input your message"   
git remote add origin https://github.com//yourname.git     
git push origin master
UserASR
  • 2,015
  • 4
  • 24
  • 47
yuyonghang
  • 91
  • 1
  • 2
8

Here is another option.

git reset --mixed origin/master
git add .
git commit -m "Your message"
git push origin master
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
1

I have also faced same error and wasted lots of time, Here is final solution.

  1. when use git push origin master then we get error so solution is to force push. so use second option. it worked for me.

  2. git push origin master --force

siddhartha shankar
  • 1,450
  • 13
  • 16
0

I faced similar problem, and the following command worked. git push --set-upstream origin master

Satish
  • 26
  • 1