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.
CodePudding user response:
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
.
- I made sure I had pushed all local commits.
- I installed
git filter-repo
brew install git-filter-repo
- I cloned a new local repository (
git clone
).
This step was becausegit-filter-repo
alerted to run the command in a freshly cloned repo. - Delete the credentials file and add it to
.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"
- newly cloned repo will not have an origin. Add it.
git remote add origin <your-repo-clone-url>
- force push to origin
git push origin --force --all
- Lastly ask to run a rebase on all other old branches in other local repositories.
git checkout <the-branch-cut-from-the-old-main>
git rebase origin main
And that should do the trick! Good Luck!