If you've ever accidentally committed sensitive files like appsettings.json
to GitHub ๐จ โ you're not alone.
Today, I explored two crucial Git concepts:
โ ๐ง๐ต๐ฒ ๐ง๐ต๐ฟ๐ฒ๐ฒ ๐๐ฒ๐ ๐๐ฟ๐ฒ๐ฎ๐ ๐ถ๐ป ๐๐ถ๐ (๐๐ถ๐๐ต ๐๐ถ๐บ๐ฝ๐น๐ฒ ๐ฒ๐ ๐ฎ๐บ๐ฝ๐น๐ฒ๐):
๐ช๐ผ๐ฟ๐ธ๐ถ๐ป๐ด ๐๐ถ๐ฟ๐ฒ๐ฐ๐๐ผ๐ฟ๐
This is where you write your code and make changes.
๐ ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ: You editappsettings.json
or add a new feature in a.cs
file.๐ฆ๐๐ฎ๐ด๐ถ๐ป๐ด ๐๐ฟ๐ฒ๐ฎ (๐๐ป๐ฑ๐ฒ๐ )
This is a temporary area where you tell Git which changes you want to include in your next commit.
๐ ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ: You rungit add appsettings.json
. Now Git is ready to commit it.๐ฅ๐ฒ๐ฝ๐ผ๐๐ถ๐๐ผ๐ฟ๐ (๐๐ผ๐ฐ๐ฎ๐น ๐ฅ๐ฒ๐ฝ๐ผ)
This is where your committed history is stored.
๐ ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ: You rungit commit -m "Added config"
. Now that file is permanently saved in your local repo (and possibly pushed to GitHub).
๐ ๐๐ผ๐ ๐๐ผ ๐จ๐ป๐ฑ๐ผ ๐๐ฐ๐ฐ๐ถ๐ฑ๐ฒ๐ป๐๐ฎ๐น ๐๐ผ๐บ๐บ๐ถ๐๐ (๐๐ถ๐๐ต๐ผ๐๐ ๐น๐ผ๐๐ถ๐ป๐ด ๐น๐ผ๐ฐ๐ฎ๐น ๐๐ผ๐ฟ๐ธ)
Letโs say you accidentally committed a file like appsettings.json
to GitHub. Hereโs how to fix it:
Add the file to
.gitignore
:
appsettings.jsonRemove it from Git tracking (but keep it locally):
git rm --cached appsettings.jsonCommit the removal:
git commit -m "Remove sensitive file from tracking"Push the clean state:
git push origin main --force
๐ก ๐ค๐๐ถ๐ฐ๐ธ ๐ง๐ถ๐ฝ:
Use git reset --soft HEAD~1
to remove the last commit but keep your code changes safe locally.
๐ง๐ต๐ฎ๐ป๐ธ๐ ๐๐ผ ๐๐ต๐ฒ ๐ค๐๐ถ๐ฐ๐ธ ๐ง๐ถ๐ฝ, ๐ ๐ฑ๐ฒ๐น๐ฒ๐๐ฒ๐ฑ ๐ฟ๐ฒ๐ฐ๐ฒ๐ป๐ ๐ฐ๐ผ๐บ๐บ๐ถ๐๐ ๐๐ถ๐๐ต ๐ฒ๐ฎ๐๐ฒ.
โ ๐๐ฎ๐๐ฒ ๐๐ผ๐ ๐ฒ๐๐ฒ๐ฟ ๐ฐ๐ผ๐บ๐บ๐ถ๐๐๐ฒ๐ฑ ๐ฎ ๐๐ฒ๐ฐ๐ฟ๐ฒ๐ ๐ผ๐ฟ ๐ฐ๐ผ๐ป๐ณ๐ถ๐ด ๐ณ๐ถ๐น๐ฒ ๐ฏ๐ ๐บ๐ถ๐๐๐ฎ๐ธ๐ฒ?
๐ช๐ต๐ฎ๐โ๐ ๐๐ผ๐๐ฟ ๐ด๐ผ-๐๐ผ ๐บ๐ฒ๐๐ต๐ผ๐ฑ ๐๐ผ ๐๐ป๐ฑ๐ผ ๐ถ๐ ๐๐ถ๐๐ต๐ผ๐๐ ๐น๐ผ๐๐ถ๐ป๐ด ๐ฝ๐ฟ๐ผ๐ด๐ฟ๐ฒ๐๐?