r/coding • u/fagnerbrack • May 11 '26
Git: How to Undo a Commit Without Undoing Your Career
https://fagnerbrack.com/git-how-to-undo-a-commit-without-undoing-your-career-de6b68694845
31
Upvotes
r/coding • u/fagnerbrack • May 11 '26
18
u/rbobby May 11 '26 edited May 11 '26
That actually made me laugh. Just gone. This sort of thing has happened to me before. Lost 1000 lines of brand new code... whole day of flow just gone in a few keystrokes. Probably 30 years ago and it still bugs me lol
That one gives me ideas of how to annoy a coworker.... except that messing with a man's git is just not done.
7
u/pihkal May 12 '26
Bookmark https://ohshitgit.com/ for emergencies.
Alternatively, you can install git-branchless, which adds a general
git undocommand. The GitUp GUI offers universal undo, though it's a bit long in the tooth now.You could also switch to git-compatible jj, which offers universal undo, and largely fixes most of git's UI problems.
4
u/driftking428 May 11 '26
git refloghas saved my ass before. Good one to know.3
u/ahal May 12 '26
1
u/recycled_ideas May 11 '26
Why on earth would you ne running a command to wipe out the last three commits in the first place?
I can count the number of times I've run reset --hard for any purpose other than wipe out my current changes and get me back to head on one hand and every one of them was exceptional enough that I knew explicitly what I was doing and even then it in most cases I was because my company was obsessed with squash merges, but didn't do merges between dev, test and prod which was already bad practice.
Hell even a reset --soft over multiple commits is extremely rare unless you're explicitly messing with your git history for some reason (I've done it when an agent decided today it wanted to commit when it hadn't previously).
And if you'd already pushed those commits (why are you sitting on a whole bunch of commits locally, that's work you can lose in a whole bunch of ways) you could have reset to the origin and gotten it all back.
The commands you've listed are neat and useful, but your problem is workflow, not git.
Beyond that, someday you will wipe out a data work, that's why you follow step one, to keep it at only a day. It won't end your career. You'll just do it again.
1
u/sleekible May 12 '26
Couldn’t you just git pull to get back the 3 commits you deleted locally? 🤔
1
u/rcls0053 May 13 '26
Git is still the product that was developed by one man over the weekend. It has zero safety features. Oh, you checked out all the files? Too bad, write it again.
1
u/bigskyhunter May 15 '26
There are a few times in my life the IntelliJ IDEs have saved me here. There’s a separately tracked local history that it keeps for every file, directory etc.
Ran a bad rebase, aborted and reset lost my work for the day; just local historied it back in 10 seconds
1
u/Sovairon May 18 '26
I just do reset --soft HEAD^ until I am where I want to be. Sometimes I lose commitmsg but that's fine.