3 ms·
>the single command that can take you out of any weird situation is `git reflog` which shows you all the commits that HEAD has pointed to. Can you elaborate on
by nvy 3y ago
>the single command that can take you out of any weird situation is `git reflog` which shows you all the commits that HEAD has pointed to.
Can you elaborate on why that's helpful? I rarely get into a weird state with git but when I do it's almost always faster/easier to just delete the repository, re-clone, and re-apply my changes manually.
- CorrectHorseBat 3y agoWhen you accidentally delete a local branch or mess up a rebase you can use the reflog to retrieve your lost commits
- olddustytrail 3y agoIf you accidentally symlink the wrong file, would you find it easier to delete the whole directory and restore from backup, or just learn how to change a symlink?
- metabagel 3y agoGit is many orders of magnitude more difficult than symlinks.
- olddustytrail 3y agoIt isn't really. Spend a day learning it and you'll be like a superhero for your team.
- nvy 3y agoI just delete the symlink and create a new one.
- olddustytrail 3y agoAnd, likewise, just delete the reference and create a new one.
- sakisv 3y agoA recent scenario had to do with me rebasing something to squash out some intermediate commits, and then I opened a PR, only to realise that some of the things I squashed I actually needed them. With reflog I was able to go back before my rebase and redo it again, more carefully this time. (Unfortunately I couldn't just cherry-pick the thing I wanted) In general it saves you from having to do what you described with delete/re-clone/re-apply things.
- hatthew 3y agoWhenever you get into an undesirable state, you usually want to undo whatever you just did. Sometimes it's very easy, e.g. if you created an object, to undo that you just delete the object. Sometimes git explicitly tells you what to do, e.g. if you are in the middle of a merge and you want to cancel it, it tells you to run `git merge --abort`. Sometimes it's not quite as easy, but still straightforward to google what to do, e.g. if you merged or committed to a branch, you just have to look up (or remember) the command to manually set what commit a branch points to. However, in my experience the most difficult "weird state" to get out of is when you do something that removes/rewrites history. For example: deleting branches, rebasing, squashing, or accidentally getting rid of a reference while attempting to solve some other problem. The root issue is that you want to find a commit that seems like it no longer exists. If you rebase, the branch now points to a new commit that has ansestors you don't want, but the old commit is gone. The "secret" is that all commits that ever existed still exist, you just can't find them in `git log` because the pointers to them are gone. `git reflog` helps solve this by giving you a list of all commits that HEAD has ever pointed to. git checkout cool-branch # HEAD points to commit abc123 git rebase main # HEAD and cool-branch point to commit def456, but you realize you don't want that rebase git reflog # reflog tells you that the commit you were just on is abc123 git reset --hard abc123 # HEAD and cool-branch now point to abc123. You've Ctrl+Z'd the rebase
- metabagel 3y ago> Sometimes it's very easy LOL, that hasn’t typically been my experience, but I appreciate the positive attitude.
- nvy 3y agoThis is very helpful, thank you.
- javier_e06 3y agoI just copy paste this and put it in my "break glass in case of emergency" folder. I have many snippets there. This is a good one. I usually refer to the log but reflog is really the "undo" for anything.