r/git • • 3d ago

support Committed a secrets file in my first internship week and realized I know about 6 git commands

first week of my internship and i committed an env file with sandbox keys to our team repo. nobody panicked, my mentor walked me through rotating them and rewriting the history, but watching him i realized i know maybe 6 git commands and understand none of them. my team will reimburse a course and theres 10 weeks left here. what would you have an intern learn first so they stop being scared of git??

70 Upvotes

50 comments sorted by

27

u/cneakysunt 3d ago

That's nothing to do with your command of git.

It's mostly a security hygiene mentality that will prevent you from making mistakes where no guard rails exist. It will also make you begin to think seriously about working towards putting guard rails up.

9

u/ProsodySpeaks 3d ago

I mean knowing how to use gitignore, or potential gh actions makes this footgun harder to fire 

8

u/Temporary_Pie2733 2d ago

What you do is stop using the git working directory as the location you run your code from. You can’t commit what isn’t there in the first place. Get used to having a distinct deployment step that can be as minimal as copying the necessary artifacts to a separate folder for execution. The keys still need to be stored somewhere and copied to the installation, but that “somewhere” is never the git working directory. 

5

u/nebulous_gaze 2d ago

This is the way we do it. You cannot commit what is not there.

5

u/ProsodySpeaks 2d ago

Keys can (should) be provided at runtime, encrypted at rest. 

1

u/cneakysunt 2d ago

Sometimes that lesson requires pain.

4

u/ProsodySpeaks 2d ago

This is very true. But i work in construction and like to think we teach people how not to lose a leg before they lose a leg. 

That said i remember having to scrub a history once, it wasn't fun, but luckily just a personal project so who gives a fuck lol. I do think that anyone being employed in code should already have been taught this stuff tho. 

The last 12 months have made me quite happy I'm not actually responsible for any cyber security! 

3

u/cneakysunt 2d ago

I think if the consequences of bad workflows was loss of life or limb all teams would be security first minded. But truth is not all projects etc require 100% effort all of the time.

Tbh though this is why the mentality matters. You're not always going to have guard rails.

26

u/ProsodySpeaks 3d ago

You might want to look at secrets managers. Plain text env files are looking increasingly sketchy with ai in the mix. 

Plus tbh it just seems dumb having plain text docs holding all our keys.

11

u/Old_Dig5389 3d ago

Gotta ease into it as a young intern though. Start with gitignore and plain env file and secrets dir with pre-commit check in case you missed something (new host, new repo, etc, with mistakes), move into whatever your base distro supports in this realm (gpg in my case), then try a few secrets managers. I've only tried Bitwarden, and peeked at online public cert/key services. Personally don't like requiring Internet or host-dependent solutions, and yubikey ... Well I suppose I should actually do that.

1

u/ProsodySpeaks 3d ago edited 3d ago

I dunno. Im a hobbyist. Was about to try and career-change into the industry precisely when the layoff apocalypse happened a few years ago so I reevaluated and swerved swe entirely and just make things for fun now. 

Anyway, my point is that i think an intern at an actual company (presumably following college education?) should be at least as competent as my self-taught self was when i started thinking about applying for roles.

With the security situation atm i think secure credential management should be a pre-requisite for anyone touching code that other people use. 

Env files need to go away asap. 

9

u/Kautsu-Gamer 3d ago

Learn ".gitignore". This is the first step.

Losing sandbox keys is a good lesson. It includes learning to remove files from git, and generation of new keys. A very good lesson.

After gitignore, learning fetch, pull, and checkout.

Theb commit, branch, push.

Then merge, rm, and rebase, and the command to cancel ongoing merge/rebase.

Then comes submodules.

1

u/Cintax 4h ago

Nah man fuck submodules. I have never seen a problem where a submodule was a good solution for it. You're almost always better off using package management of sort variety.

-1

u/edgmnt_net 3d ago

Gitignore is useful, but relying too much on that sort of stuff is what gets people into such messes, especially with a habit to "add everything" without even checking what's going to be staged.

3

u/Kautsu-Gamer 2d ago

The teaching of gitignore also teaches what should not added.

The problem is that f. ex. Visual Studio 2026 adds all unignored files to created repository without asking user.

Due thay checking gitignore is essential.

9

u/atarivcs 3d ago

Env file names should be in .gitignore, so they can't be committed.

7

u/Gurnug 3d ago

You still can commit what is in gitignore

3

u/EatRunCodeSleep 2d ago

Once if you force it, which a normal user or an intern doesn't do/know how to do it.

9

u/Temporary_Pie2733 3d ago

Just to point out, rewriting history is pointless. You just need to make sure the old keys are no longer accepted, because whether you rewrite history or not, you have to assume someone has their own copy, not just relying on the ability to get them from history whenever they want. 

4

u/yawaramin 2d ago

This. And just to add, don't store plaintext secrets in .env files. Either store and inject them from a vault like 1Password or encrypt and inject them using sops.

3

u/jarofgreen 2d ago

Its pointless for security, but maybe they were in a work branch and wanted clean commits. It's a good lesson to learn, anyway.

3

u/Adalcar 2d ago

Isn't that what he means by "rotating them"?

1

u/Temporary_Pie2733 2d ago

Rotating usually refers to a scheduled replacement of old keys with new keys. It has nothing to do expunging the presence of the old keys from the Git repository. 

2

u/Adalcar 2d ago

Well yes, but you said "you need to make sure the keys are no longer accepted".

If they rotated the keys as a result of this mistake, I would assume that means the old keys are no longer accepted, or there's a serious problem in the security systems.

1

u/Temporary_Pie2733 2d ago

Yes, that’s the only necessary step. I was pointing out that once you do that, it makes no difference whether you purge the repository or not: nobody who has the old keys now or gets them in the future can make any use of them. 

1

u/Adalcar 2d ago

Oh sorry I misunderstood your first comment, I would assume they removed them from history so it does not show up on an audit or a pentesting report, as even if the key has been rotated, it's still a bad look

1

u/hawkprime 2d ago

Still worth doing, at my company we use repo scanners that will flag theses files and put us in the naughty list until we fix it. Just double check and repo hygiene

1

u/slindenau 1d ago

But isnt just deleting the file in a new commit good enough to get rid of the flag? Does that scanner really look through ever (historic) commit every time?

Because that sounds pretty weird and resource/time intensive.
Yes in theory the keys are still in there, and fine if you get a flag once on it from the scan…but then after rotating the keys that file is useless and should not show up on a next scan.

1

u/Cintax 4h ago

The problem is that if a key was committed, and then deleted, but never rotated, you now have an active key in the git history anyone with access to the repo can recover to escalate privilege. So yes you do have to scan the commit history at least once to confirm that hasn't happened. Otherwise you're just trusting that a deleted key was also rotated, and you can't really trust that to be true.

4

u/FlipperBumperKickout 2d ago

Why is that environment file not ignored by the gitignore file?

3

u/J_Galang 3d ago

If you're a visual learner and the concept of branching is still blurry try this:

https://learngitbranching.js.org/

It has interactive guides and challenges. Similar to Flexbox Froggy and Grid Garden.

2

u/CpnStumpy 3d ago

Got status constantly. Check every time before, after, and between every thing you do.

1

u/Broad-Promise6954 ancient 2d ago

This (minus typo, or autoincorrect). git status is your friend here. Use it lots.

2

u/Galvarcus 3d ago

The most important thing I teach an intern or new employee is where to teach themselves. From there it is up to the individual's curiosity and willingness that determines if they are of use to me. In this case, the official git website. Don't be afraid to ask questions, even of the boss.

1

u/Ok_Specialist413 3d ago

https://www.gitualize.com Choose the command you wanna know about. Or a scenario (as a beginner try scenarios and then deep dive). Tutorials are modern visuals step by step and u'll learn better than any other source.

If it helped u i'd be glad

1

u/duane11583 3d ago

You will do this again

Document the procedure for them

Some one else will do this too

Wait till you smoke 1of 12 unique dev boards that cost $20k and you cannot order another fir 6 months

1

u/bratzlaff 2d ago

Sounds like you should not use ‘git add .’

I tell my new hires to avoid using that and to actually look at what they are committing, but they still do and it’s obvious from what I have to review regularly.

1

u/ReddyKiloWit 2d ago

I'd start with learning how git works to the point you can visualize the processes, and add some basic best practices study. You should pick up some basic commands while doing that, then fill in the details once you have the context of how it all comes together.

And don't feel too bad. I had to patch up repositories more than once when a user goofed.

1

u/otheyhigh 2d ago

I would recommend covering commonly used git commands. Search specifically for commands used in production environments.

Fork a public repo and explore these commands on your personal setup. Create a markdown as you use each command and explain what you are trying to do in that context (helps remembering things).

Don’t be disheartened by mistakes and know that you are not the first one to make them. Git will almost always have workflow capabilities to assist. In this case, git secrets is a great hook to act as a failsafe. Always explore standards or best practices that a system (or its users) recommends to handle these nuances in your processes/workflows.

1

u/Chippors 2d ago

The usual go-to is the O'Reilly books. git is the bat one.

1

u/Downtown_Cat_7425 2d ago

First, rotate every secret in that file immediately - assume it is compromised the moment it was committed. Then remove it from history, not just the working tree: `git filter-repo --path secrets.env --invert-paths` is the usual tool. Force-push only after coordinating with anyone else using the repo. Add the file pattern to .gitignore, and put a secret scanner (gitleaks or detect-secrets) in a pre-commit hook and CI. Most importantly, tell your team lead the day it happens - that is a normal intern mistake, and hiding it is the part that becomes a problem.

1

u/IndividualLimitBlue 1d ago

Rewriting history was not necessary at all after revokation/rotation (eleven to remove the env file only)

1

u/Pipetboy2001 1d ago

Git tiene muchísimos comandos, pero al final uno termina memorizando los típicos: pull, push, checkout, commit, add y, a veces, commit --amend o revert. cherry-pick también, aunque ese lo uso muy rara vez
A veces más es el aprender que no hacer y cómo evitar hacerlo, a palos se aprende

1

u/Nice-Management9711 10h ago

well every senior engineer do always have a secret git story and the best ones tells it in standups

1

u/Klaxxigyerek 1h ago

Put a gitgnore with your env file in it on every repo before the first commit and you've already beaten half the industry. after that its reps, and bootdev or kodekloud both have git courses past the basics with hands on labs not only lectures

1

u/Ath-ropos 3d ago

https://git-scm.com/book/en/v2

You don't need anything else, it's really the definitive guide.

1

u/tiller_luna 3d ago edited 3d ago

Do you have a grasp on Git data model? If not, you really need it; https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain is a decent source, just don't dive deep and skip over the convoluted plumbing incantations. If yes, you might be better off with some GUI for Git for better visualization of its state, there are many shells for that.