1

I now that executing this:

git reset --hard shaCommit .

I will take back my code to a specific previous commit, but, How can I push that? Let's say that my 4 last commits are rubbish and I want to go back to a specific commit, but I'm getting this error when I try to push:

$ git push
To ssh://git@uk-gitlab.almuk.corp/warlocks/capexbaar-web.git
 ! [rejected]        development -> development (non-fast-forward)
error: failed to push some refs to 'ssh://git@uk-gitlab.almuk.corp/warlocks/capexbaar-web.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
cape
  • 424
  • 1
  • 3
  • 16

3 Answers3

2

Recommend you to check this answer here Similar Question

Quoting from the original Accepted solution

Just do:

git push origin <your_branch_name> --force

or if you have a specific repo:

git push https://git.... --force
Rhythem Aggarwal
  • 346
  • 2
  • 15
1

You have to force the push, using the command git push -f.

If you pull the changes before force a push, it will download all changes and do a merge commit.

thyago stall
  • 1,654
  • 3
  • 16
  • 30
  • 2
    Please note that this is one of those things that's extremely simple to do, but is also very dangerous if done incorrectly. You should research the force push carefully and know what you're getting into before using it, or you run the risk of making your colleagues very angry at you. – JDB Oct 24 '17 at 14:49
0

If you work with other people in the repo and you already pushed the commits you want to remove, it is usually better to not modify history, so you will not mess up whatever other people are doing.

To do that you can push a new commit to revert the change to that state, instead of just removing the old commits:

git checkout shaCommit -- .
git commit -a

see Revert multiple git commits for more details

Cash Lo
  • 1,052
  • 1
  • 8
  • 20