0

Note I posted this question on askubuntu, but I wasn't getting an answer.I saw this post on meta, so I thought I would try posting on SO.

So I have initialized a git repository based on an existing repository on Github. The project I am initiating is very different than the latest commit listed for the repository.

When I tried to executer this set of commands

git init
git remote add origin https://github.com/account/repo
git fetch origin
git add -A
git commit -m "message"
git push origin master

I get the following error:

To https://github.com/account/repo
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/account/repo'
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.

The thing is, I know that there a differences between this repository and origin, but I explicitly do not want to pull the latest changes from the repository. I want to override the changes on Github with the ones in this repository.

Is there anyway to do this? Is there anyway to force a push even with the fast-forwarding error?

Thank you for any help or guidance.

EDIT: Would The answer by Adam Dymitruk on this question work, or did the order in which I executed the above commands have screwed up his method?

Community
  • 1
  • 1
Wold
  • 952
  • 1
  • 13
  • 25
  • If your goal is to lose the history of the original repo, and replace it by what you have in your local repo, why are you starting from this original repo in the first place, instead of creating a brand new repo? – JB Nizet Oct 12 '14 at 06:55

1 Answers1

1

Add -f to your git push line to force push

git push -f origin master

This will throw out whatever existing history of master the remote repository had.

If you want to preserve your existing history then

Mothball the existing master branch, and rename your new branch as something else and push that instead. git push origin master:new_proj. Then git checkout new_proj. You can now access the old project history through 'origin/master' and the new project via new_proj

Andrew C
  • 13,845
  • 6
  • 50
  • 57
  • so this will force a push, but not affect the commit history right? I'll still be able to see the old changes to the previous files? – Wold Oct 12 '14 at 06:59
  • "This will throw out", so no. If you want to keep the old history you have a couple of choices. I'll edit. – Andrew C Oct 12 '14 at 07:00
  • Also the projects are related, but its a complicated situation. Regardless I tried method two, but I am getting an usage error. – Wold Oct 12 '14 at 07:08
  • you may need to add '--root' to the rebase line, or redo the steps so that you copy your new project in after you are already checked out to a branch based off of origin/master. Post the error message if you are stuck. – Andrew C Oct 12 '14 at 07:10
  • After adding `--root`, I received the message `fatal: Needed a single revision`. If I want to start over, what order would I execute the commands in? – Wold Oct 12 '14 at 07:11
  • You have a zip or tarball of the new source? After you fetch I would `git checkout -b new_proj origin/master` then `git rm` everything, and extract the source onto that. – Andrew C Oct 12 '14 at 07:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62907/discussion-between-wold-and-andrew-c). – Wold Oct 12 '14 at 07:15