Consider I've staged for committing a file (e.g. db/schema.rb
) that I didn't intended to change. I need to do:
git reset db/schema.rb git checkout db/schema.rb
Can I do it by single command?
To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.
Unlike git reset , git checkout doesn't move any branches around. This is useful for quickly inspecting an old version of your project. However, since there is no branch reference to the current HEAD , this puts you in a detached HEAD state.
git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory. Only use this if you know what you're doing.
git checkout, git reset, and git restore are commands that can help you revert to a previous version not just of your codebase, but of individual files, too. Get to know the details of these commands and you’ll be jumping around your file history like an expert in no time. What Does Resetting a File Mean, Anyway?
Git Reset A Specific File When invoked with a file path, git reset updates the staged snapshot to match the version from the specified commit. For example, this command will fetch the version of foo.py in the 2nd-to-last commit and stage it for the next commit: git reset HEAD ~ 2 foo.py
git-reset - Reset current HEAD to the specified state In the first three forms, copy entries from <tree-ish> to the index. In the last form, set the current branch head ( HEAD) to <commit> , optionally modifying index and working tree to match.
Undoing changes means considering exactly where you want to undo them. git checkout, git reset, and git restore are commands that can help you revert to a previous version not just of your codebase, but of individual files, too. Get to know the details of these commands and you’ll be jumping around your file history like an expert in no time.
I tried this one and works well for me:
git checkout HEAD -- path
I just added this to my .zshrc
/ .bashrc
checkout() { git reset "*$1*" git checkout "*$1*" }
And then you can just do checkout <file>
and you're all set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With