0

I have made wrong modifications in the code, then commited them, then pushed them to remote.

Now local mybranch and origin/mybranch are in sync.

The I found that my code if wrong. I would like to step back one comming. I did reset current branch to previous commit and got correct state in local repository, but unable to push this to remote.

If I do normal push, it merges and I stay on wrong code again.

After I did force push, I got my extra commits logically disappeared, but I still had the wrong code.

Now I am to revert my changes in old-fashion style manually.

Was it better way to perform?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • Have you done `--soft` or `--hard` reset ? – Sajib Khan Dec 01 '16 at 10:21
  • @sajibkhan it won't work with already pushed stuff, will it? – Phate01 Dec 01 '16 at 10:22
  • Possible duplicate of [Undoing a 'git push'](http://stackoverflow.com/questions/1270514/undoing-a-git-push) – Naman Dec 01 '16 at 10:23
  • @Dims, no matter if you pushed or not. When we do force (-f) push `remote myBranch` history will be replaced with `local myBrance`. – Sajib Khan Dec 01 '16 at 10:26
  • @sajibkhan I haven't actually tried yet but he's saying that he forced the push with no results. I'd try a `git revert` – Phate01 Dec 01 '16 at 10:32
  • I think that main errors comes from the fact that you want to have the "perfect" branch from scratch. Do a first dev in a branch, then do another dev in a second branche etc... then do again another branch where you will make clean commit by doing cherry-pik. Then when it's really and tested (and maybe after a couple weeks) clean the extra branches. You might have your own remote push all intermediate branch to you remote in order not to pollute the common-dev remote-repo – jo_ Jul 17 '17 at 07:11

4 Answers4

0

after reset to correct code

git push origin mybranch -f

-f means force

Duyet Nguyen
  • 543
  • 3
  • 11
0

You can revert the commit:

git revert <commit-hash>

Git will create a commit that undoes any changes in the commit provided

Docs: https://git-scm.com/docs/git-revert

Also see: https://stackoverflow.com/a/22683231/4499267

Community
  • 1
  • 1
Phate01
  • 2,499
  • 2
  • 30
  • 55
0

I would like to step back one comming[sic].

You should've

git rebase -i origin/mybranch

And select which commits you want to keep.

Anton Shmelev
  • 75
  • 3
  • 7
0

If you want to delete last commit with changes then need to do --hard reset.

$ git checkout mybranch
$ git reset --hard HEAD~1            # undo last commit with changes
$ git push -f origin HEAD
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73