Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Git: Undo a commit after several good commits have been commited afterwards

I have a beginner question around git and GitHub i was hoping someone could help with.

I have accidentally committed a change in piece of code that contains credentials on my local private repository on my local machine.

My pseudo commit history looks like the below and i am using the desktop application for GitHub to make these commits. I made the mistaken commit at id 3 but then all my other commits from four to seven are a big body of work and I do not wish to lose them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since this is my own repository the quick method might be to simply copy out the files i changed and revert back all my changes to id 3 and then copy in my good changed files. I was wondering if there was a better Git way

commit first change  id 1
commit second change id 2
commit third change  id 3 - here is where the password was committed
commit first change  id 4
commit second change id 5
commit first change  id 6
commit second change id 7

Thank you very much for your time

>Solution :

You either can do squash:

git rebase -i <commit_id or HEAD~5>
# move your cursor on the commit line & delete that commit by pressing double `d`
git push -f origin <branch>

It helps to delete selected commits only, without changing commits after and before that commit.

Or, soft reset and commit:

git reset --soft <commit_id or HEAD~5>
git add .
git commit -m "<message>"
git push -f origin <branch>

It deletes all commits from the commit_id or HEAD~5 and makes a single commit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading