Managing SSH Keys in .git Directory for Repository Portability and Account Isolation
By
dansjots
5mo ago· 3 min readen
75/100
Toasty
Bagelometer↗
Warm and crisp on the edges. A bagel with a bit of bite.
Score75Typehow-toSentimentpositive
Summary
The article describes a technical solution for managing SSH keys in Git repositories to prevent accidental pushes from the wrong account. The author explains how they store SSH keys directly in the .git directory of each repository, making repositories portable across different machines via USB drives. This approach uses deploy keys exclusively and avoids credential confusion by keeping authentication credentials repository-specific rather than machine-wide.
Key quotes
· 3 pulledMy repository remotes are set up to use deploy keys exclusively, as I don't want to inadvertently push changes from the wrong account due to forgetting to change the local machine user's credentials.
I used to do so by keeping several subdirectories inside my .ssh directory for per-account keys and manually pasting them outside whenever I want to push a specific repo, but it's cumbersome and still quite error-prone.
Eventually I came across this SuperUser answer and adapted it with my slight twist leveraging the fact that the .git directory's contents are well-behaved.
My repository remotes are set up to use deploy keys exclusively, as I don’t want to inadvertently push changes from the wrong account due to forgetting to change the local machine user’s credentials.
