13

We are using git for version control and right now we are getting a lot of warnings when trying to upload the most recent version. I am new to git and don't have patience for the restrictions, is there any way to delete everything and upload the current version?

This is what I get now when trying to upload.

$ git push origin master
Username for 'https://code.google.com':
Password for 'https://<removed>@code.google.com':
To https://code.google.com/p/<removed>/
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://code.google.com/p/<removed>/'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
6
  • 2
    It would help to show the sequence of commands you are trying and the output you get
    – Paul Dixon
    Commented Jun 17, 2012 at 12:55
  • "delete everything and upload the current version" sounds very much like you are not using git for version control but are using it as a substitute for a publishing tool. Can you post the warnings that you are getting? Even if you don't have the patience to understand them someone else will have to if they are going to be able to help.
    – CB Bailey
    Commented Jun 17, 2012 at 13:33
  • I pasted the hints I am getting now. I understood what I got before but it seemed needless as the code on my compute worked well and the code on the online version was outdated. I just wanted to start fresh. Why is that so foul I do not know
    – AturSams
    Commented Jun 17, 2012 at 13:44
  • You don't have the current version on your local machine.. Do a git pull before trying again. (and then you can do my solution which I wrote in my answer) Commented Jun 17, 2012 at 14:26
  • @ArthurWulfWhite: The remote version is not outdated in that it is not simply an old version of what you have. It contains commits that aren't in your current local master branch. Who has pushed these commits and why don't you want the changes that they introduce?
    – CB Bailey
    Commented Jun 17, 2012 at 15:22

3 Answers 3

23

1.) Delete everything in your repository folder

2.) run git rm * to tell git to remove all files (check with git status whether there are any unstaged files left)

3.) do a git commit -a -m "commitmessage" a git push origin master to delete all files on the remote git server

4.) check if you have an empty remote repository now

5.) copy all new files to the local repository folder

6.) run git add * to tell git to add all new files (check with git status whether there are any unstaged files left)

7.) commit and push the new files

Now you should have the version on your remote git repository.

4
  • Hi, I pasted the result I am getting now, could you help?
    – AturSams
    Commented Jun 17, 2012 at 14:03
  • 1
    why could not git push -f be used? Commented Jun 18, 2012 at 3:39
  • I wrote the answer before I knew that the problem is the push command. I thought Arthur doesn't know how to "start new". In the edited question, I think git push -f would have worked, even though I'm not at all sure, if the remote git repository would do what you expect. Commented Jun 18, 2012 at 16:07
  • The original problem was different than the one I faced after attempting the instructions Michael provided me, therefore his answer perfectly suits the situation. The original issue before deleting the code and starting a-new was that there were about 20 different files between my version and the online version, some of them did not even need to exist. I did not have time or patience to go and merge file by file so I just cleared up the git. It is my project and even if it is ill advised / bad practice, that is the road I saw fit.
    – AturSams
    Commented Jun 18, 2012 at 18:46
16

You are unable to push because there are commit on the remote that aren't merge in to the commit that you are trying to push. If you want to discard all of the changes that are in the currently in the remote you can do something like this.

git fetch
git merge -s ours origin/master
git push origin master

Note that this throws away changes that have been introduced in the remote that you don't have in your local repository.

1
  • 1
    just for future reference, add --allow-unrelated-histories to the merge command in newer git versions. Commented Apr 17, 2017 at 23:31
2

If you want to permanently delete the files from the remote repository and in retrospect as if they weren't pushed in the first place to start fresh, then base on this answer you could do the following

Run the following locally (Notice the * deletes every folder/file):

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch *" -- --all

Now it's time to update git references locally to enforce it to forget all the history it had of those files

rm -rf .git/refs/original/

git reflog expire --expire=now --all

git gc --prune=now

git gc --aggressive --prune=now

Now push all the changes to the remote repository with a FORCE to ensure the remote repo also clears all those files

git push --all --force

This would clean up the remote repository as if no file ever existed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.