News for hackers. Stuff that matters.
Re:Confusing Git Terminology by nvy on Thursday November 02, 2023 @02:31PM (#38117988)
>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.

Re:Confusing Git Terminology by CorrectHorseBat on Thursday November 02, 2023 @02:49PM (#38118240)
When you accidentally delete a local branch or mess up a rebase you can use the reflog to retrieve your lost commits
Re:Confusing Git Terminology by olddustytrail on Thursday November 02, 2023 @04:16PM (#38119468)
If 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?
Re:Confusing Git Terminology by metabagel on Thursday November 02, 2023 @10:10PM (#38123525)
Git is many orders of magnitude more difficult than symlinks.
Re:Confusing Git Terminology by olddustytrail on Friday November 03, 2023 @03:13PM (#38133680)
It isn't really. Spend a day learning it and you'll be like a superhero for your team.
Re:Confusing Git Terminology by nvy on Friday November 03, 2023 @12:48AM (#38124648)
I just delete the symlink and create a new one.
Re:Confusing Git Terminology by olddustytrail on Friday November 03, 2023 @03:12PM (#38133650)
And, likewise, just delete the reference and create a new one.
Re:Confusing Git Terminology by sakisv on Thursday November 02, 2023 @04:36PM (#38119781)
A 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.

Re:Confusing Git Terminology by hatthew on Thursday November 02, 2023 @08:27PM (#38122535)
Whenever 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
Re:Confusing Git Terminology by metabagel on Thursday November 02, 2023 @10:02PM (#38123454)
> Sometimes it's very easy

LOL, that hasn’t typically been my experience, but I appreciate the positive attitude.

Re:Confusing Git Terminology by nvy on Friday November 03, 2023 @12:45AM (#38124630)
This is very helpful, thank you.
Re:Confusing Git Terminology by javier_e06 on Friday November 03, 2023 @11:02AM (#38129852)
I 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.