Reputation: 1656
I am trying to undo a really old commit that has already been pushed to the remote.
I looked around and thought that git rebase
was probably the best solution.
so I ran the code:
git rebase -i <commit-id>
then edited:
pick <commit-id> <commit-title>
drop <commit-id> <commit-title> <- the commit I want to delete
pick <commit-id> <commit-title>
Then tried applying this with :wq
. This however triggered a LOT of merge conflicts (the commit is about half a year old).
The reason I want to undo the commit is because I accidentally added a file with credentials on it that I should not share with other collaborators.
So the only thing I want to undo is the "creation" of the file on the remote.
Is git rebase
the best approach?
if so is there a way not to trigger all the merge conflicts (merge them just like before)?
I would rather not accidentally merge a conflict in the wrong way.
Also the commit I am trying to delete should not cause any conflicts with future commits. I tried running git rebase
without dropping the commit but the merge conflicts still popped up.
Upvotes: 3
Views: 1197
Reputation: 1656
Thanks to @ElpieKay!
git filter-repo
managed to do its job
For full details look here.
I first tried to use BFG
, but I had some issues so I moved on to git filter-repo
.
git filter-repo
brew install git-filter-repo
git clone
).git-filter-repo
alerted to run the command in a freshly cloned repo..gitignore
.
Make sure to save the credentials elsewhere if you haven't already.git filter-repo --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "deleted file <file-name> and added to .gitignore"
git remote add origin <your-repo-clone-url>
git push origin --force --all
git checkout <the-branch-cut-from-the-old-main>
git rebase origin main
And that should do the trick! Good Luck!
Upvotes: 2