Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I edit past git commits to remove my password from the commit logs?

My problem: cygwin git doesn't seem to correctly prompt for credentials when using https:// URLs, so I used username and password in the URL. Unfortunately when I did a "get pull" it auto-commited a message with the full URL including password. I didn't notice this until after I had pushed the changes.

How do I edit old commit messages to eradicate the password in the URL?

My shared git repo is on my own server. I can do surgery on the repo if necessary.

Instructions on how to change my configuration (i.e. don't use Cygwin, don't use https) are unnecessary -- I'm trying to deal with what is already done.

Yes, I can and will burn the password but I'd still like to fix it.

like image 481
Dewey Sasser Avatar asked Dec 06 '11 01:12

Dewey Sasser


People also ask

How do I edit past commits?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.

How do I remove a commit from git log?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

How do I remove sensitive data from git history?

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history. To entirely remove unwanted files from a repository's history you can use either the git filter-repo tool or the BFG Repo-Cleaner open source tool.


1 Answers

To completely remove a file from a git repository and its history, use these commands.

# Check out the remote repo
git clone git://host/path/repo.git
cd repo

# Clobber the file in your checkout
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch file-to-purge.txt' --prune empty --tag-name-filter cat -- --all

# Make sure you don't accidentally commit the file again
echo file-to-purge.txt >> .gitignore
git add .gitignore
git commit -m "Prevent accidentally committing this again" .gitignore

# Push the edited repo. This will break other people's clones, if any.
git push origin master --force

For more information, remove sensitive data guide at GitHub will help you.

like image 109
Alan Haggai Alavi Avatar answered Nov 15 '22 06:11

Alan Haggai Alavi