0

Suppose we have two branches, master and local_branch and I have some commits in my_branch. I push all of my commits to repository and we assume when I use:

git log --pretty=oneline

These lines will be showed to me:

c682f01 (origin/local_branch) add final changes.
1bad4de add some local changes 2.
ef7d35e add some local changes 1.
f719c75 temp commit!
f0dcf28 some local changes.
adc5be1 (origin/master, origin/HEAD, master) Add design decisions.
baf2ec8 Add some feature 1.
edd3e66 initial commit

Suppose that I want to delete a commit (temporary commit) from local_branch.

How can I do it without corrupting other commits?

Which is the best way? Can I do it with this instruction:

git rebase -i

Stefan Neacsu
  • 653
  • 3
  • 12
Saeed
  • 159
  • 3
  • 13
  • The "normal" thing to do is revert that commit, not rebase it out - but it depends on precisely why you currently want to delete that commit - what is the reason? – AD7six Nov 25 '19 at 11:41
  • Possible duplicate of [How to use Git Revert](https://stackoverflow.com/questions/19032296/how-to-use-git-revert) – AD7six Nov 25 '19 at 11:41
  • Or this one: [Preferred Github workflow for updating a pull request after code review](https://stackoverflow.com/q/7947322/761202) – AD7six Nov 25 '19 at 11:42
  • @AD7six because a lot of people can see details of that branch in git repo and that commit which I want to delete it permanently, is deceptive and I don't want anyone to see it in future. – Saeed Nov 25 '19 at 11:44
  • Saeed - have you pushed something that is supposed to be secret? – AD7six Nov 25 '19 at 11:45
  • @AD7six no. just a bad way to implement and a wrong file. – Saeed Nov 25 '19 at 11:46
  • See aforementioned duplicate questions - probably this one https://stackoverflow.com/a/15055649/761202. TLDR - rebase it out and force push. – AD7six Nov 25 '19 at 11:47
  • If you are the only person working on this branch, `git rebase -i f0dcf28` is your best solution to remove your commit – Ahmed HENTETI Nov 25 '19 at 12:53

2 Answers2

2

I would reset my local Branch to f0dcf28, cherrypick ef7d35e - c682f01, commit them and then force push them over to the remote. (Many repos are setup that you cannot force push to branches or at least not to the master branch. In that case there is nothing you can really do about the temporary commit)

git reset --hard f0dcf28
git cherry-pick my_branch~2 my_branch
git push -f

(Please be careful with the forcepush - I haven`t really tested this: Forcepush will overwrite your remote branch!)

David Albrecht
  • 634
  • 1
  • 4
  • 15
  • Thank you for your example. can I do this work without using cherrypick? just with git rebase -i ? – Saeed Nov 25 '19 at 11:49
  • 1
    I don`t see how rebase can help you in your situation – David Albrecht Nov 25 '19 at 11:50
  • I don't see this working. The master branch (remote and local) does not include the commit to be deleted `adc5be1 (origin/master, origin/HEAD, master)`. This looks like it'll either a) push f0dcf28 to `origin/master` or b) delete the last 4 commits from `origin/local_branch` depending on which branch is checked out when the commands are run. – AD7six Nov 25 '19 at 17:43
  • I think your right. I got confused with his setup. I will edit my answer. If the remote branch he is pushing to is called `my_branch`, then master needs to be replaced with that branch name – David Albrecht Nov 26 '19 at 10:36
2

You could do git rebase -i f0dcf28. You should see something like the following in open up in your editor:

pick c682f01 (origin/local_branch) add final changes.
pick 1bad4de add some local changes 2.
pick ef7d35e add some local changes 1.
pick f719c75 temp commit!
pick f0dcf28 some local changes.

Edit the pick in front of f719c75 temp commit! and replace it with d, i.e.

pick c682f01 (origin/local_branch) add final changes.
pick 1bad4de add some local changes 2.
pick ef7d35e add some local changes 1.
d f719c75 temp commit!
pick f0dcf28 some local changes.

Save this in your editor and exit. Your commit should be dropped now. You'll need to force push after doing this action.

unixia
  • 4,102
  • 1
  • 19
  • 23
  • @AD7six I think every way other than revert would require a force push. Since the OP asked specifically for fix with `git rebase -i`, I thought this should fit. – unixia Nov 26 '19 at 02:07
  • @unixia Thank you for your response, at the end I do your instruction but instead of using "pick" I used "drop" and finally everything was ok. – Saeed Nov 26 '19 at 07:03
  • 1
    @AD7six correct. I edited the answer to say force push would be required. Thanks! – unixia Nov 26 '19 at 14:52