If you've ever accidentally committed sensitive files like appsettings.json to GitHub ๐Ÿ˜จ โ€” you're not alone.

Today, I explored two crucial Git concepts:

โœ… ๐—ง๐—ต๐—ฒ ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฒ ๐—ž๐—ฒ๐˜† ๐—”๐—ฟ๐—ฒ๐—ฎ๐˜€ ๐—ถ๐—ป ๐—š๐—ถ๐˜ (๐˜„๐—ถ๐˜๐—ต ๐˜€๐—ถ๐—บ๐—ฝ๐—น๐—ฒ ๐—ฒ๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ๐˜€):

  1. ๐—ช๐—ผ๐—ฟ๐—ธ๐—ถ๐—ป๐—ด ๐——๐—ถ๐—ฟ๐—ฒ๐—ฐ๐˜๐—ผ๐—ฟ๐˜†

    This is where you write your code and make changes.

    ๐Ÿ›  ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ: You edit appsettings.json or add a new feature in a .cs file.

  2. ๐—ฆ๐˜๐—ฎ๐—ด๐—ถ๐—ป๐—ด ๐—”๐—ฟ๐—ฒ๐—ฎ (๐—œ๐—ป๐—ฑ๐—ฒ๐˜…)

    This is a temporary area where you tell Git which changes you want to include in your next commit.

    ๐Ÿ›  ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ: You run git add appsettings.json. Now Git is ready to commit it.

  3. ๐—ฅ๐—ฒ๐—ฝ๐—ผ๐˜€๐—ถ๐˜๐—ผ๐—ฟ๐˜† (๐—Ÿ๐—ผ๐—ฐ๐—ฎ๐—น ๐—ฅ๐—ฒ๐—ฝ๐—ผ)

    This is where your committed history is stored.

    ๐Ÿ›  ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ: You run git 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:

  1. Add the file to .gitignore:
    appsettings.json

  2. Remove it from Git tracking (but keep it locally):
    git rm --cached appsettings.json

  3. Commit the removal:
    git commit -m "Remove sensitive file from tracking"

  4. 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.

๐—ง๐—ต๐—ฎ๐—ป๐—ธ๐˜€ ๐˜๐—ผ ๐˜๐—ต๐—ฒ ๐—ค๐˜‚๐—ถ๐—ฐ๐—ธ ๐—ง๐—ถ๐—ฝ, ๐—œ ๐—ฑ๐—ฒ๐—น๐—ฒ๐˜๐—ฒ๐—ฑ ๐—ฟ๐—ฒ๐—ฐ๐—ฒ๐—ป๐˜ ๐—ฐ๐—ผ๐—บ๐—บ๐—ถ๐˜๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—ฒ๐—ฎ๐˜€๐—ฒ.

โ“ ๐—›๐—ฎ๐˜ƒ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐—ฒ๐˜ƒ๐—ฒ๐—ฟ ๐—ฐ๐—ผ๐—บ๐—บ๐—ถ๐˜๐˜๐—ฒ๐—ฑ ๐—ฎ ๐˜€๐—ฒ๐—ฐ๐—ฟ๐—ฒ๐˜ ๐—ผ๐—ฟ ๐—ฐ๐—ผ๐—ป๐—ณ๐—ถ๐—ด ๐—ณ๐—ถ๐—น๐—ฒ ๐—ฏ๐˜† ๐—บ๐—ถ๐˜€๐˜๐—ฎ๐—ธ๐—ฒ?

๐—ช๐—ต๐—ฎ๐˜โ€™๐˜€ ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ด๐—ผ-๐˜๐—ผ ๐—บ๐—ฒ๐˜๐—ต๐—ผ๐—ฑ ๐˜๐—ผ ๐˜‚๐—ป๐—ฑ๐—ผ ๐—ถ๐˜ ๐˜„๐—ถ๐˜๐—ต๐—ผ๐˜‚๐˜ ๐—น๐—ผ๐˜€๐—ถ๐—ป๐—ด ๐—ฝ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฒ๐˜€๐˜€?