Hacker News new | past | comments | ask | show | jobs | submit login

git revert is probably what you want?



This creates a new commit that reverses the changes of a past commit. That’s different from undoing your last commit, as in, you made a commit but then changed your mind, and want, say to make some more changes before you commit. (Explained in man git-revert.)


> Explained in man git-revert

If git at least had good man pages. The man-page's description is "Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them." [1] I have a hard time parsing that to mean what it's supposed to mean.

It contains a note that's much better at explaining what git revert does, then goes on to explain alternatives for other things you might want to do, but doesn't mention how to undo a commit without creating a reverting commit.

1: https://git-scm.com/docs/git-revert


“ I have a hard time parsing that to mean what it's supposed to mean.”

Can’t disagree. That part could certainly be written better. I’m not saying I would want to learn Git from the man pages, but they’re pretty good in general after learning it elsewhere.


They are decent for reference, and some are genuinely great (like git-everyday [1] or git rebase [2]). But just as often as they are helpful they just leave me scratching my head, either because they are too deep in git-lingo or because they forget to clarify some crucial details

1: https://git-scm.com/docs/giteveryday

2: https://git-scm.com/docs/git-rebase


That's by design; Git history is intended to be immutable when working with others.

But, if you haven't pushed something up to a remote, the right command to make more changes is to use `git commit --amend`.


> Git history is intended to be immutable when working with others.

That's another pet peeve (though maybe less git's fault): immutable, yes, but then rebase is pushed quite liberally. Arguably not by git itself, but by many online learning resources.

It's convenient, and mostly works, but then occasionally really stings you.

...yet undoing the last commit, arguably the least aggressive of history-rewriting commands, remains awkward.


Rebasing has a simple rule: it is perfectly fine as long as you haven't shared those commits.*

Perhaps git should have a "checked rebase" command that requires a `--force` flag if there's an upstream branch set.

*And even then it's kind of okay as long as no one else has made commits on top of the rebased commits.


It's also very wise to make sure you disallow force pushing to master/main/production. If you force push to a topic branch, it's usually ok. If you accidentally overwrite someone's changes, it's because you are working together and you can communicate that you messed up and for them to `pull --rebase` or however you want to resolve it. Usually that is. There's always room for disaster :)


> Usually that is. There's always room for disaster :)

For me, this is where the problem with got lies. I use so many of it's commands infrequently enough that I don't know where the weird disastrous edge cases are, where I should use.

I think git would greatly benefit from a topical man page; a list of common and uncommon situations that shows the recommended commands for solving them, and explains what those commands do. I know that hundreds of random "guides" exist, but are they outdated, are they cannon, do they have bugs? When your UI is esoteric, situational documentation is really important.


Ya, the git UI is not great. It has improved a ton over the years but still not great. Have you ever seen Git Koans? It's good for a laugh. https://stevelosh.com/blog/2013/04/git-koans/


--force-with-lease

Which BTW I learned it exists when magit switched "p -f u" to use it instead of plain --force.


Oh right. I always neglect to do that mostly out of laziness of typing it out on the command line. It's funny, though, I maintain a branch plugin for vim (it's a fugitive extension) and I recently accepted a PR to switch `--force` to `--force-with-lease` yet I still don't do it. I need to start doing it!


How is "git reset HEAD~" "awkward"? It's not obviously named, but as was noted above, if there was git-uncommit, it would need every option that git-reset has, and so it would just be an alias, without the final argument.

"History is immutable" is generally accepted as describing the contents of commits, not their id's or relative ordering.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: