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.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AturSams
  • 7,568
  • 18
  • 64
  • 98
  • 2
    It would help to show the sequence of commands you are trying and the output you get – Paul Dixon Jun 17 '12 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 Jun 17 '12 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 Jun 17 '12 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) – Michael Kohler Jun 17 '12 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 Jun 17 '12 at 15:22
  • @Charles Bailey: What happened is that I have made a lot of minor changes and never felt any of them were worth committing, then by the time I wanted to commit, git asked me to check about 25 files.. I downloaded a clean current version(clone) and used beyond compare to merge but git did not like that, I don't know why(I am new to git) but next time I encounter problems, I will ask a proper question explaining the difficulty I am facing before asking for a short cut. – AturSams Jun 17 '12 at 21:22

3 Answers3

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.

Michael Kohler
  • 753
  • 10
  • 22
  • Hi, I pasted the result I am getting now, could you help? – AturSams Jun 17 '12 at 14:03
  • 1
    why could not `git push -f` be used? – vincent mathew Jun 18 '12 at 03: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. – Michael Kohler Jun 18 '12 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 Jun 18 '12 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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
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.

Korayem
  • 12,108
  • 5
  • 69
  • 56