Suggestions for rebasing a branch, skipping a commit in the middle which deleted files

Hi, I’d like to hear out some suggestions about a Git problem I faced some days ago, how you would have handled it, and why that would’ve been better.

I was in a situation where a commit deleted some files in a remote repo, and other people worked on it and committed after.

<font color="#000000">$ git log -4 --graph --oneline</font>
* 6e8a23b (HEAD -> master, origin/master) Added MergeSort algorithm in KotlinScript and Swift
<font color="#FF0000">* 755bca9 purged all files excluded by .gitignore</font>
* f2d6d7c (before-purge) Merge branch 'master' of https://github.com/...
|\

The red commit deleted a lot files that needed to be restored, lots of them, spreaded all around the repository, so,

I wanted to restore all the deleted files while mantaining the successive commits’ changes.

A _ rebase --onto before-purge 755bca9 _ from master was what immediately came to mind, but that broke the repo’s history, so I had to do a  reset --soft origin/master , an _ add -A _, and finally a commit.

So I basically used Git’s tools but at the end I had to “cheat” to be able to push something correctly, and I spent almost an hour trying different things to achieve what I wanted to do but I ended up having to resort on reset.

What could have been a better way to handle the issue? I tried searching the internet but I wasn’t able to find similar situations. Let me know!

In my opinion, the best way to handle that would have been to use the git revert command. In other words, in your specific situation:

git revert 755bca9

This would have created a commit that specifically undid the changes in that commit and only that commit. That would have the benefit of not breaking the repository’s history and it would allow you to specify why you were undeleting those files (rather than using a “Thanos snap” of making the commit disappear).

Let us know if you have more questions.

2 Likes

That looks exactly like what I was looking for. Very useful, thank you!

2 Likes