Little Git Tricks: Revert Changes in a File
If you want to revert a change in a file with Git, you can use this command:
The double-hyphen (--) tells Git to take what follows as its second argument (the path to the file). This makes it clear that you did not specify a branch.
While this works, it is not ideal. You might wonder what command to use or where the -- should go. It can be hard to remember and we often need to check the documentation to make sure we do it the right way. Is there a better way?
Since version 2.23 (released in August 2019), you can use this simpler command in Git:
This command is clearer and provides a better match between the action you want to take and the command you need to use. Git includes many improvements like this one, but we often miss them when they are released. This makes working with Git harder than it should be.
What is your favourite improvement in Git that people mostly overlook? Let me know on Mastodon or Bluesky.