1

i accidently updated a bad commit to the server and when i reset it to the working commit it shows your branch master is behind 1 commit use git pull to fast forward but i don't want to fast forward.i just want to delete that particular commit from remote .....

please Help

gaurav singh
  • 69
  • 1
  • 2
  • 5
  • use git reset, then use git push force. more [details](https://stackoverflow.com/a/10510482/4648586). do note that you had to ensure no body else pulled the commit you want to remove.. – Bagus Tesa Aug 25 '18 at 06:49
  • 3
    Possible duplicate of [Delete commits from a branch in Git](https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – Dietrich Epp Aug 25 '18 at 06:51
  • @DietrichEpp Must be said, the question is old, and *a lot* of answers are very bad. How can a question get so many votes despite showing so few research? – Romain Valeri Aug 25 '18 at 09:24
  • @gaurav singh Are you working alone on this repo? If not, you probably should refrain from any solution rewriting history on the remote. – Romain Valeri Aug 25 '18 at 09:30
  • @RomainVALERI: Why does it matter if the question is old? The question is the same question being asked here, you should not be scared of “old” questions. Why does it matter if a lot of answers are very bad? There are many good answers, and the best answers are at the top. Why does a question need to show research? A question needs to be useful or interesting to many people in order to get votes. The linked question is useful and clear, and the top-voted answer is up to date and correct. – Dietrich Epp Aug 25 '18 at 09:38
  • @DietrichEpp Sorry if I somehow offensed you with my comment, I didn't want to start a controversy. I'm not convinced but this should be discussed on meta. – Romain Valeri Aug 25 '18 at 09:44
  • @RomainVALERI: This is not actually controversial, I'm not offended, and it has been discussed on meta before. Questions are not only upvoted for being well-researched. The primary reason to upvote a question is because it is useful. As the site has changed, the general character of useful questions has changed. See: https://meta.stackoverflow.com/questions/255843/how-is-this-question-well-researched – Dietrich Epp Aug 25 '18 at 09:46
  • Possible duplicate of [Rolling back local and remote git repository by 1 commit](https://stackoverflow.com/questions/4647301/rolling-back-local-and-remote-git-repository-by-1-commit) – phd Aug 25 '18 at 10:44

2 Answers2

3

For starters, as @RomainVALERI pointed out, I'd be really careful resetting history on a remote branch that is shared with others. For instance, if it's a dev or master branch, it's never a good idea to reset history in any way.

There are two common ways to undo a commit in git.

1. Revert the bad commit (no history alteration)

This is the safest route to go for shared branches like dev, master, etc. since someone might have already pulled in the bad commit you made. You just create a new commit that undoes the bad commit and push.

git revert <commit_hash>
git push

Since your question tells me that it's the most recent commit (the one HEAD is pointing to), you can also do git revert HEAD and push.

2. Reset old commit and force push (rewrite history)

You can take this route if it's a branch that you're working with. It might be a feature or bugfix branch. Since it's just you working on it, rewriting history won't matter much. Again, I'll assume here that it's the most recent commit that you want removed. There are more involved ways to deal with an older commit (see: git rebase)

As @Rohan put in his answer, just reset your HEAD to the commit before the bad commit and force push.

git reset HEAD
git push --force

Update 1-

Hosting providers (viz. VSTS, GitHub) also allow admins to put branch policies that everyone needs to adhere to. A very common policy for dev/master branches is to disable "force push" for the very reason I mentioned above. If it's a shared repo, there's a good chance that you might not have "force push" privileges and you'll have to take the "revert" route.

Community
  • 1
  • 1
dgarg
  • 56
  • 5
  • i pushed force the branch but when i do ssh my remote branch still says i am 2 commits behind – gaurav singh Sep 05 '18 at 06:47
  • Could you clarify by what you mean by "_when I do ssh_"? – dgarg Sep 07 '18 at 15:35
  • force push is denied by the remote server – gaurav singh Sep 08 '18 at 12:07
  • That's a policy defined by the repository/branch admins and you don't have "_force push_" privileges. For instance, if you host your repo in VSTS, you have the ability to define policies for a particular branch. I'd advise not to go down this route and just revert the bad commit. – dgarg Sep 09 '18 at 14:58
  • I updated my answer to explain branch policies since it's a common to thing to run into when working with shared repos. – dgarg Sep 09 '18 at 15:32
0

As per the descrition mentioned in the post:

If it is the last commit in history then following command will work

 git reset HEAD
 git push remote_name branch_name -f

If it is not the last commit then

Step 1: Find the commit before the commit you want to remove git log. Step 2: Checkout that commit git checkout

Hope it answers your question.

Rohan
  • 2,681
  • 1
  • 12
  • 18
  • You didn't even ask if rewriting history is acceptable in OP's context. This could be a very dangerous mistake. Or not. Nobody knows. Scary. – Romain Valeri Aug 25 '18 at 09:25
  • Right, need to make sure to consider whether other people may have already committed too, before rolling back history via a re-write. – matanster Dec 21 '19 at 10:56