35

The situation is as follows:

We have some project which persists on Github, and deploy it to Heroku, which has its own mini-Git. Some changes were applied directly to Heroku (cloned repository from Heroku, made changes and pushed). In a meanwhile the master branch on Github has gained also some changes.

Now when I want to push the main repository to Heroku, pushing fails because commits on Heroku are out of sync with local commits.

I do not want to merge with changes on Heroku - I just want them to vanish.

Is there some way to clean git repository on Heroku to push then my local repo from the very beginning?

I do not want to destroy application and recreate it again because it has some paid services and I am just an collaborator.

Paul
  • 25,812
  • 38
  • 124
  • 247

5 Answers5

65

Install the Heroku Repo plugin and use it to reset the remote git repo.

First delete the git repo on the server:

> cd /my-project/
> heroku plugins:install heroku-repo
> heroku repo:reset

Then re-initialise your local git repo by deleting and recreating it:

> cd /my-project/
> rm -rf .git
> git init
> heroku git:remote -a <appname>

Where <appname> is name of your app in heroku.

The 2 repos (local and remote) should now be empty and in sync.

Steve Eynon
  • 4,979
  • 2
  • 30
  • 48
57

You can just use the -f flag in git (it's a standard git flag) to force the push. See the side note in the Dev Center Git doc. This will overwrite those changes you've made on Heroku obviously.

Jon Mountjoy
  • 4,498
  • 22
  • 24
6

Try going to https://github.com/heroku/heroku-repo and using that plugin to remotely clean things up.

courtsimas
  • 764
  • 1
  • 13
  • 20
4

If your Heroku push is failing because it's not a fast-forward commit, and you're not trying to do anything funky with your history, it's very simple:

  1. Ensure that your non-Heroku repository is defined as a remote.
  2. Fetch all remotes.
  3. Merge your other repository into your local Heroku clone using the recursive strategy with the "theirs" strategy option.

    git merge -X theirs remotes/origin/master
    
  4. Push with the --force option to make a non-fast-forward commit to your upstream Heroku repository.

On the other hand, if you're really trying to purge history, you have some other options.

  • Remove all but the first commit from your history (easiest).

    git reset --hard $(git log --pretty=oneline | tail -n1 | cut -d ' ' -f1)
    git push --force
    
  • Create an empty master branch.
  • Use git-filter-branch.

Personally, I'd recommend keeping your history and fixing the fast-forward issue as a best-practice, but you certainly have options if you want to modify your history aggressively. Git is very flexible in that regard.

Community
  • 1
  • 1
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
2

You can also simply use the heroku's app versioning system. Going to "Activity" you have the complete history of the project: rollback to the first activity and the repo should be completely cleaned, coming back to its state when the app was originally created on Heroku.

Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • That doesn't work: `! [rejected] master -> master (non-fast-forward)` – mgPePe Apr 10 '16 at 19:04
  • I don't know, this is a problem of Heroku that are they that have to solve. But... are you using the web interface or the command line? Try to use the web interface... – Aerendir Apr 11 '16 at 12:18