I've had juniors who didn't believe this, so just to say it: If you know what you're doing, practically any Git problem is recoverable.
The one major exception is if you delete your local changes before committing them.
I've had juniors who didn't believe this, so just to say it: If you know what you're doing, practically any Git problem is recoverable.
The one major exception is if you delete your local changes before committing them.
git reflog
git re-flog is what you do with those idiots who mess up the repo so that someone else has to come in and fix it again.
Fuck, you beat me to it
Rebase the comment chain and force push!
I hear people praise git reflog a lot, but when do y'all use this? Most I've ever needed is git log --graph for history checking. Maybe if I was in a situation where I knew the code on local branch X was working on Friday, but not anymore on Monday, then I could use git reflog to go back to Friday for that local branch right? Is that the idea?
Power users rebase with squashes and fixups multiple times a day. Especially if the job’s integration process isn’t enforcing long living branches.
Reflog is useful then, because you literally rewrite history every rebase.
It’s not often, but a bad git reset, or a messed up rebase of that branch that’s 8 months old that you’ve picked up again.
I only use it when I've royally messed up and the commit I need to get back is no longer referenced anywhere. Accidentally deleted a branch, finished a merge or rebase before realizing I messed up, that kind of thing, just use the reflog to find it again, get a branch pointing to it, then try again.
git log will only show you commits in your history. If you're only ever working forwards, this will contain all the stuff you'll ever need.
But if you're rewriting history, like with a rebase or squash or something, or you're deleting branches without merging them, then you can run into a situation where the official history of your branch doesn't contain some of the commits that used to exist, and in fact still exist but are unlinked from anywhere. So reflog is the log of where you've been, even if where you've been isn't in the official history anymore, so you can find your way back to previous states even if there isn't otherwise a name for them.
If all you care about is your current history, git can use the dates of commits just fine to see where you were on Thursday without needing the reflog.
When I'm not using git reflog, I'm not making big enough git screw ups.
Seriously, once you commited something to the repo it's hard to lose it. Unless you delete .git. But a this point frequent pushing has your back.
I know git can be hard to grasp in the beginning. It was hard for me too. I highly encourage everyone to put in the effort to understand it. But if you don't want to do that right now just use it. Just commit and push. It will pay off.
Our company "architects" decided we needed 80% coverage minimum. On a mostly .Net 4.8 codebase. Hundreds of programs used in prod, with barely any tests in sight.
It's a fucking nightmare.
Ah, the classical "just introduce tests in a legacy codebase", what can go wrong?
My condolences, it's always a BITCH to handle
Why would you care about code coverage requirements on a branch that is currently in development? My work in progress commits might not build because they don't even compile, let alone have passing tests. The only time I care about the build passing and meeting requirements is when I'm ready to create the pull request.
Also code coverage is a useless metric, but that's a different story.
3 is not related to using git in any way. I'm not really sure what you mean in 4. I didn't mean making a lot of changes, I meant that you should not wait with committing until you have a finished feature / fix / whatever. Commit after each refactor, commit after adding a new testable unit. It's always better to have more checkpoints. If your team does code review, they will appreciate atomic commits too.
Special shout out to the person who committed a gigabyte memory dump a few years ago. Even with a shallow clone, it's pretty darn slow now.
We can't rewrite history to remove it since other things rely on the commit IDs not changing.
Oh well.
Sounds like a flawed workflow, if this didn't go through at least code review. Was it committed directly to master?
Curious to know what kind of system relies on hashed not changing? Technically the hashes don't change, but a new set of commits is made. The history diverges, and you can still keep the old master if you need it for some time, even cherry pick patches to it..
The guy at work who managed git before me, well didn't quite have the knowledge I do and was not using LFS. In one of the main repos a 200mb binary was pushed 80+ times. This is not the only file that this happened to. Even if you do a shallow clone, you still need to add the commit depth eventually. It's a nightmare.
Special shout out to the person who committed a gigabyte memory dump a few years ago. Even with a shallow clone, it's pretty darn slow now.
If you want to be the team's hero, I've had good luck removing old commits using git filter repo.
https://hibbard.eu/erase-sensitive-files-git-history-filter-repo/
We can't rewrite history to remove it since other things rely on the commit IDs not changing.
Oh right. Oof.
I would be working to arrange an accident for those other things. Those other things probably need to be retired.
You know... A version control system... That class of software that makes it possible for you to recover from any error you commit.
With Jujutsu (which is compatible with git), you can just
jj undo
Been using it for over a year now and not being scared of trying operations is such a boon. It helps so much with learning when you know you can just roll back to an earlier state.
I've had zero issues with it so far and no one at work noticed anything different, other than there being a bit more rebase spam on PRs.
That is so cool. Why doesn't git have this already?
Can't ya just git reset?
Anything you can do in Jujitsu you can do in git.. The big difference is a paradime change:
-instead of a working directory that has pending changes you need to add than commit, all changes are in a commit that is lacking metadata.
The system has better "editing" of local history to set that meta data. But once you push to a shared repo you run the usual risks of force pushing.
I'm not sold, rather git not do anything until asked and just run git status constantly but I don't have first hand experience.. I would theory it would be more likely to add a file you didn't mean to.. Unlike those who use windows guis for git and forget to add new files.
Isn't it the exact opposite?
I learned that you can never make a mistake if you aren't using git, or any other way for having access to old versions.
With git it is really easy to get back to an old version, or bisect commits to figure out what exact change was the mistake.
The only way I understand this joke is more about not wanting to be caught making a mistake, because that is pretty easy. In other methods figuring out who did the mistake might be impossible.
This is not about mistakes in the Git-managed code. This is about mistakes in the Git commands themselves. Anything that involves merging/rebasing/conflict resolution can potentially be botched. These mistakes are usually fixable, but:
Went to tech elevator boot camp, was a decent experience even if I don't find myself doing exactly what I was expecting to do. Life is that way though.
Anyways, my first week I fucked some git stuff up so bad I became the go to guy when anyone had any git issues because I had to learn so much to undo my egregious error. I don't remember now exactly what it was but it took some serious git magic to sort.
Just saying that point 1 is very true. And yeah don't make mistakes in git.
Like accidentally committing multi-GB files and years down some poor mf (me in this case) has to figure out how to clear the files from history because the server is running out of space.
Short of manually deleting .git you can always find any commit, you can walk backwards through your reference lof if it comes to it, the only real risk is throwing out unstaged changes.
Hmm... I am using git for maybe 15 years... Maybe I'm just too familiar with it... and have forgotten my initial struggles... To me using git comes natural... And I normally pay a lot of attention to every single commit, since I started working on patches for the Linux kernel. I often rebase and reorder commits many times, before pushing/merging them into a branch where continuity matters.
You're confusing errors in your code, and errors while doing some git command you thought you knew. Lucky you, it's clearly never happened to you because you don't mess around with it.
Sure, I sometimes messed up with git, but a git reset , checkout, rebase or filter-branch (In the extreme cases) normally fixes it, but real issues are very rare. And I use git a lot... But only the CLI, maybe people have issues with GUIs?
Funny those are commands I avoid.. They all have to do with editing history which I know there is a vocal group here that loves "clean" history but that isn't what happened.
sure merge full features so you can roll back a feature.. And if something is really off I might start from a snapshot commit and cherry pick/merge a bunch in but usually history is histoy.. If submitting to a public project I may make a new branch with the cleaned version but why edit in line. That is risking issues.
Git repository operations are (almost?) always recoverable. git reflog is your friend.
The filesystem operations are another story. Handle with care.
Wait, what does reflog do?
Think of it like your browser history but for Git. It’s a list of the SHAs related to your recent operations.
And because Git is a content-addressable data store, a SHA is basically like a URL. Even if a branch no longer exists, if you know the SHA it pointed to then you can still check out the exact contents of that branch. The reflog helps you find that.
Goddamit... It's ref log, not re flog. I thought this was related to blame and never touched it lmao
Re flog is when your devs don't follow your guides and mess uo git history again.
I legit thought it was to publicly call out a user to be (metaphorically) flogged
And while it's not how I do things, I totally understand the impulse. Tell me that doesn't sound like something that could exist
I don't think you should be scared of git.
Unless by "scared" you mean "I pasted from ChatGPT and now I don't know what to do," in which case, please be more scared.
We need version control for the version control.
git reflog is your friend
The worst thing you could do is delete your local git repo.
top 95 comments