0

I want to clear the remote repository and push the new files.

I' am getting

git commit -m "fix"
# HEAD detached from 0027561
nothing to commit, working directory clean

and nothing is pushed to the remote.

I tried to git init, but it didn't work.

How can I do that?

Burak
  • 5,706
  • 20
  • 70
  • 110
  • # HEAD detached from 0027561 nothing to commit, working directory clean – Burak Aug 27 '14 at 12:54
  • You need to create a new branch and push to remote, see @VonC's answer. – gravetii Aug 27 '14 at 12:54
  • 1
    @hjpotter92 Not sure if the OP wants to specifically do a force push... – gravetii Aug 27 '14 at 12:55
  • You can't commit from a detached head. You can either create a new branch or if you are trying to revert commits, you can checkout out master again, run `git revert COMMIT_TO_REVERT` which basically creates a new commit doing the exact opposite of those commits, then make your changes from there. – jamesthollowell Aug 27 '14 at 12:57
  • possible duplicate of [Git: How can I reconcile detached HEAD with master/origin?](http://stackoverflow.com/questions/5772192/git-how-can-i-reconcile-detached-head-with-master-origin) – clami219 Aug 27 '14 at 12:58
  • 1
    @jamesthollowell That is not true. You can commit as much as you want with a detached HEAD. There is nothing in `git` to prevent that. Conversely, you generally *don't want to*, though, at least unless you are aware of what you are doing, and intend to create some sort of ref (branch, tag, etc.) pointing to the new commits. If you don't, then when you do switch to a branch later, your commits become "dangling", which means they could eventually be discarded by the garbage collector. – twalberg Aug 27 '14 at 14:03

2 Answers2

2

You can make a new branch

git checkout -b newBranch
git push -u origin newBranch

If you want to update an existing branch like master, you can rebase that new branch on top of master:

git rebase master
git checkout master
git merge newBranch
git push heroku master

I prefer that to directly checkout master, or to force master to the new branch (git branch -f master newBranch), because it takes into account the case where new commits were done on master concurrently (meaning "while commits where done with the detached HEAD")

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • i dont want to make a new branch. my branch is heroku master, and I want to stay with the same name – Burak Aug 27 '14 at 12:55
  • @Burak no problem, you can checkout master only, unless you want to commit the new changes, in which case you need to rebase your new branch on master. – gravetii Aug 27 '14 at 12:56
  • But if I checkout, will I lose the files that I changed in local? – Burak Aug 27 '14 at 12:57
  • @Burak instead of checkout master directly, create a new branch and make sure everything is added and committed, then rebase it on top of master. I have edited the answer. – VonC Aug 27 '14 at 12:57
  • If you are really worried, you can do `git stash` before you change stuff to temporarily store it and `git stash pop` to load it back in. You don't have to do this, but it will make sure you can't loose your changes. – jamesthollowell Aug 27 '14 at 12:59
0

Clearing the repository (remote or local) is not something you can actually do in git. Not sure why you would want that. Git init creates a new repository locally. It will fail if you run it on an existing repository.

The error you are getting is about your local HEAD being detached, meaning you are not actually on a branch, probably due to stuff that you did earlier that you declined to mention in your question. If you do a git status it will tell you this.

Fix that by checking out a branch: git checkout master should work on most repositories that have a master branch. That gets you back on the master branch.

Commit doesn't actually push to remote. So even if you were not detached that still wouldn't send your commit remotely but it would simply create the commit in your local git repository, which lives in your .git directory.

Once you have an actual branch with an actual commit you can try pushing to the remote repository. I assume you actually have one and that you actually cloned it at some point. If you did, it is typically called 'origin' and you need to push the commits from your local master branch to the master branch in origin.

git push typically does that job. If that fails, a git push --all (all branches) or a git push origin master (just the master branch).

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46