0

In a somewhat long-winded sequence of events caused by local, test and development repositories going out of sync, I ended up making some commits which now need to be deleted.

In particular, the latest commit, call it A is the one that needs to be deleted. Commit that precedes it, call it B is the one on which the HEAD is located.

When I run git status I get

Not currently on any branch. nothing to commit (working directory clean)

How can I get rid of the commit A? Also, will getting rid of it automatically make Git realize that it is on the master branch?

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 1
    Can't you checkout the branch for which A is the tip and just revert it? – Martin G Aug 17 '16 at 19:04
  • No because it is the same branch. – MadPhysicist Aug 17 '16 at 19:58
  • If the commits you want to keep are all part of the history on some branch, checkout that branch. If you want master to be at a certain commit you can checkout the master branch, and git reset to it (you probably need --hard, but since you're in a bit of a mess make sure you don't leave any commits you want stranded without any branch). – gauteh Aug 17 '16 at 20:22

3 Answers3

1

If you want to delete any commit execute below commands

step1: git revert commit_id

step:2 git push origin branch_name

sanjeevjha
  • 1,439
  • 14
  • 18
  • What is the `commit_id` in this case? Is it the one to be removed? – MadPhysicist Aug 17 '16 at 20:21
  • 1st you need to check the branch `git branch` and after that checkout the branch where you need to delete the commit `git checkout branch_name` and after that `git revert commit_id`, here commit_id is A's commit it looks like 3e199a274018ae067fa1cd6d87c9cad3fa741053 – sanjeevjha Aug 17 '16 at 20:26
  • I am confused. Could you edit your answer with a step-by-step process? – MadPhysicist Aug 18 '16 at 16:25
  • In particular, I am confused if you are talking of CREATING a new branch or CHECKING OUT and existing one. – MadPhysicist Aug 18 '16 at 16:25
1

step 1: you need to list down all the git branch you have

git branch

step 2: you need to checkout to particular branch from the list and make sure you should checkout to that branch only where you want to delete particular commit

git checkout branch_name

step 3: now you need to list down all the commits to that branch

git log

setp 4: now you need to select the commit id that you want to delete from the list

step 5: now you need to execute the command to delete particular commit. here below branch_name is same where currently you are

git revert commit_id

git push origin branch_name

step 6: now if you execute git log then you find there is new commit happen and in this commit code is delete that you want to do. Now if you go to code base then find unwanted code is removed

I hope it will help you out.

Thanks.

sanjeevjha
  • 1,439
  • 14
  • 18
1

Easy method:

git branch -f master

Correct method:

git checkout master
git reset --hard HEAD~1

See How to revert Git repository to a previous commit?

Also: If head is on the previous commit, then your master branch is not checked out. Your head is detached.

Explanation:

D <- master
C <- head
B
A

If you have checked out C, you just force master to C.

If you have checked out D, you just revert master to C.

Community
  • 1
  • 1
Bryce Drew
  • 5,777
  • 1
  • 15
  • 27