Git Security Best Practices
In this page:
# .gitignore
.env
secrets_file
Secrets को Git से बाहर रखना
Git में committed एक password या API key push होते ही effectively public हो जाती है, क्योंकि Git पूरी history preserve करता है — repo clone करने वाला कोई भी बाद में इसे "remove" करने के बाद भी एक पुरानी commit से निकाल सकता है। secrets को एक untracked .env file में रखें (.gitignore में listed) और इसके बजाय runtime पर load करें।
उदाहरण: Keeping Secrets Out of Git
echo ".env" >> .gitignore
Committed Secrets के लिए History Scrub करना
एक नई commit में एक secret delete करना इसे हर पहली commit में पूरी तरह intact छोड़ देता है — history default रूप से overwrite नहीं होती। इसे actually हटाने के लिए git filter-repo या BFG Repo-Cleaner जैसे tool से history rewrite करनी होगी, और तब भी आपको leaked credential को compromised मानकर rotate करना चाहिए, क्योंकि पुराने clones में यह अभी भी हो सकता है।
उदाहरण: Scrubbing History for Comitted Secrets
git filter-repo --path secrets.env --invert-paths
GPG के साथ Commits Sign करना
एक GPG-signed commit आपकी key से जुड़ा एक cryptographic signature carry करता है, जिसे Git (और hosting platforms) verify कर सकते हैं यह prove करने के लिए कि commit really आपसे आई और tampered नहीं हुई — एक protection जो एक plain commit, जो सिर्फ एक name और email record करता है जिसे आप खुद type कर सकते हैं, नहीं देता।
उदाहरण: Signing Commits with GPG
git commit -S -m "Signed commit"
Remote Access Keys Audit करना
SSH keys और personal access tokens default रूप से खुद expire नहीं होते, इसलिए एक ऐसे laptop की key जो अब इस्तेमाल नहीं होता, या सालों पहले एक one-off script के लिए generated एक token, एक live, भुला हुआ access point बना रह सकता है। unused वालों को regularly review और revoke करना उस gap को बंद करता है।
उदाहरण: Auditing Remote Access Keys
ssh-add -l
git config --global --get user.signingkey
Branch Access Restrict करना
Branch protection rules (GitHub, GitLab, वगैरह पर available) main जैसी branch में push की अनुमति देने से पहले CI checks pass करना और कम से कम एक approving review की माँग कर सकते हैं, code review को एक social convention से किसी ऐसी चीज़ में बदलते हुए जो platform actually enforce करता है।
उदाहरण: Restricting Branch Access
# Configured on the hosting platform: require CI checks and reviews before merging to main
.envcommit करना और फिर इसे.gitignoreमें add करना, जबकि secret पहली commits में रहता है और rotate किया जाना ज़रूरी है।- एक नई commit में एक secret हटाना और यह सोचना कि यह गया, जबकि history में अभी भी यह तब तक रहता है जब तक आप
git filter-repoजैसे tool से इसे rewrite न करें। - यह मान लेना कि एक private repository secrets के लिए safe है, जबकि access पाने वाला कोई भी, या बाद में एक leak, history पढ़ सकता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: