gaurav singh
gaurav singh

Reputation: 79

How do i permanently delete a commit in git in both local and remote?

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

Upvotes: 1

Views: 17492

Answers (3)

yannis_t
yannis_t

Reputation: 29

In case someone uses GitHub Desktop (current version 3.3.3) :

If you want to revert your commit and not delete it, thus not altering history:

  1. In Github Desktop select the branch that contains the offending commit

  2. Select the History tab

  3. Right click on the commit you want to revert and select "Revert changes in commit"

enter image description here

As a result a new commit will be created that reverts the changes.

  1. You push the new commit to the remote branch.

  2. Both the local and remote branch have changed and the History tab in Github Desktop has a new commit with a prefix "Revert" and the name of the commit that was reverted. enter image description here

Upvotes: -1

dgarg
dgarg

Reputation: 76

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.

Upvotes: 5

Rohan
Rohan

Reputation: 2727

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.

Upvotes: 0

Related Questions