Wold
Wold

Reputation: 972

git, syncing a new repository

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?

Upvotes: 0

Views: 116

Answers (1)

Andrew C
Andrew C

Reputation: 14883

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

Upvotes: 1

Related Questions