Reputation: 61
While building a project I was using a private repo and for faster testing, I was using the database and other credentials as variables inside the code.
Now project relies on env. variables so no secret stuff is inside the repo. I am thinking to make it public.
I don't want people to see parts in commit history which include these credentials.
Is it possible to remove these lines or make all commit history private?
Upvotes: 3
Views: 10197
Reputation: 21
Both answers are outdated. It is NOW possible to change the history.
Consider the following as an example, you have the following:
0c39034 commit you want removed
f7fde4a commit that you want to be in
DO NOT use revert as reverting will cause you to have:
e499d89 commit that supposedly reverts 0c39034
Then, you need to rebase and squash the commits that you want removed as in the following:
git rebase -i HEAD~3
pick f7fde4a commit that you want to be in
squash 0c39034 commit you want removed
squash e499d89 commit that supposedly reverts 0c39034
# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Do a git log to confirm.
git log
Push your changes:
git push --force
Doing this will totally remove 0c39034 and e499d89 in your history. They can still be accessed in web ui if (and only if) you have the complete commit-id. It will say something like:
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I want to end this by saying that what @instantepiphany said holds true as best practice and make sure never to hard-code credentials over at github.
Upvotes: 2
Reputation: 179
You need to consider any credentials uploaded to github as no longer secret. Even if you rebase, remove them, and then force push as others suggested, the commits will still be accessible by url (which has a simple format based on the commit id) once you make the repository public.
There are 2 ways to make the code public without giving people access to your credentials.
Change your credentials, make the repository public and let others learn from your mistake (I myself have uploaded secrets before, and have seen other experienced developers do it too, so don't feel alone!).
Leave the repository private, make a local copy, rebase (make sure to remove all secrets!), and then push to a new, public repo.
I would do option 1, but both are safe.
Upvotes: 8
Reputation: 2680
Yes, its possible , here are some resources below:
Changing previous Commits messages with sourceTree [Usefull if you want to change many commit messages]
Changing Commit messages at github documentation
and lastly you can edit the last commit with:
git commit --amend
and after that PUSH it with:
git push --force example-branch
Upvotes: 0