Toskan
Toskan

Reputation: 14941

Git undo pushed commits (deleted folder by mistake) that have other commits on top

Ok so I deleted a folder by mistake and pushed that change

I then reverted, and committed again, andpushed
then I continued to work for 2 months

Now I noticed, that all my annotates basically read "whops delete by mistake"

is there a way to delete just the delete folder plus revert of the delete, and keep everything else?

I work by myself, no team members, no worries about having it checked out or whatever

Upvotes: 1

Views: 61

Answers (2)

Romain Valeri
Romain Valeri

Reputation: 22047

Since you're the master of your ship here (history rewrites allowed as you mentioned), I'd suggest interactive rebasing here*. (doc)

And before starting, since this is a touchy operation, as reminded by Lasse Vågsæther Karlsen right below, it is strongly advised that you backup your branch (with git branch <backupBranch> <originalBranch> for example).

First search for the two commits' SHA-1 hashes (deletion and undeletion) through git log. (I'll assume you can do that but feel free to ask for details on that part if needed.)

Then with

git checkout <yourBranch>
git rebase -i <deletionCommit>^

(mind the caret to start interactive rebasing from the commit just before the deletion commit)

you'll get an open editor with a list of the commits from the specified point to HEAD, preceded by a keyword :

pick 295ea4a Some commit message
pick 9cb2b54 Some other commit message
pick fe91df1 Yet another commit message

Now change pick to drop for the two commits to drop, and close/save the editor.

And finish the rebase with

git rebase --continue

(if anything goes sideways, use git rebase --abort to restore things just before rebasing started)

And finally don't forget that you'll need to git push -f <remote> <branch> next time to update the old ref, since every commit down the line will be different, be it only by the ref of their parent(s).


* (because after 2 monthes you may have a fair number of commits, so git reset --hard <deletionCommit>^ && git cherry-pick <nextGoodCommit-1> <nextGoodCommit-2> <nextGoodCommit-3> could be very tedious, and interactive rebase basically automates just that)

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142402

is there a way to delete just the delete folder plus revert of the delete, and keep everything else?

  1. Not clear what do you mean "delete just the delete folder"

  2. revert of the delete

    # Undo any change based upon the commit SHA1 (or multiple)
    git revert SHA-1 SHA-2 ...  SHA-N
    

Now I noticed, that all my annotates basically read "whops delete by mistake"

How to fund out the last commit of a file

git log -n 1 -- <file path>

Now use this SHA to checkout the desired content

git checkout <SHA-1> -- <path>

Upvotes: 1

Related Questions