Ad

Git: Undo A Commit After Several Good Commits Have Been Commited Afterwards

- 1 answer

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.

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

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad