0

I had accidentally added a wrong to git update (git add wrongfile). I committed this change and started pushing to git server (git push origin master). But then I realized I had committed a wrong file (the wrongfile size is more than 200MB XML file), there for I interrupted the upload. I deleted that file and removed it from git changes (git rm wrongfile). But now the terminal says:

Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)

Does anybody know how to resolve this? I used git reset --hard HEAD but it didn't work. (I have backed up my files if anything goes wrong.)

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Proceso
  • 567
  • 1
  • 6
  • 24
  • what happens when you execute `git status`, why `git reset --hard HEAD` doesn't work? which error are you getting? – iberbeu Oct 15 '13 at 17:37
  • Since I had committed all the previous changes, it only showed the deleted change. But when I use "push origin master" again,it resume from where i interrupted the upload. – Proceso Oct 15 '13 at 17:57

4 Answers4

0

The message you're seeing (your branch is ahead by 1 commit) means that your local repository has one commit that hasn't been pushed yet.

See this post: git - Your branch is ahead of 'origin/master' by 1 commit

If you have committed junk but not pushed,

git reset --soft HEAD~1 HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

If you want to get rid of any changes to tracked files in the working tree since the commit before head use --hard instead. Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

git revert HEAD This will create a new commit that reverses everything introduced by the accidental commit.

(quote taken from http://nakkaya.com/2009/09/24/git-delete-last-commit/)

Community
  • 1
  • 1
Jason Enochs
  • 1,436
  • 1
  • 13
  • 20
  • This command "git reset --soft HEAD~1" did the trick for me. Thanks a lot :) – Proceso Oct 15 '13 at 18:02
  • There was a problem :(. after that command I got reset to the correct head with all the uncommitted changes (git status showed all the changes) but when I add those changes back again and used "git push origin master" then again it resumed from when I interrupted the upload. Anyway I got the project cloned to another folder and put my current working project folders to it and pushed it from that folder. it worked – Proceso Oct 15 '13 at 18:35
0

There isn’t any problem. The commit still exists locally, that’s all that message is telling you (verify with git fetch ; git log origin/master..master). You can undo the commit locally by running

git reset head~

This will not change any files locally (it’s a “soft” reset).

git reset --hard head will have no effect if you don’t have any modified files in your working tree.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
0

Did you try: git reset --soft HEAD^ ? or you can: git reset --soft [commit_hash] to reset your mistaken commit. After this, you can delete those unnecessary files and commit once again, and then push it.

Azamat
  • 435
  • 5
  • 13
  • The "push origin master" command didn't complete (because I interrupted in the middle). So I don't know how to get the [commit_hash] :/ – Proceso Oct 15 '13 at 18:00
0

I got the project cloned to another folder and put my current working project folders to it and pushed the changes it from that folder. Finally deleted the messed up project directory and moved to the newly cloned one. This is a worst solution but I did that since I don't know the correct command to resolve it form the terminal. #timeSaver #atthismoment

Proceso
  • 567
  • 1
  • 6
  • 24