Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git reset and checkout by single command

Tags:

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?

like image 480
Alexey Avatar asked Apr 28 '12 10:04

Alexey


People also ask

How do I completely reset git?

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).

What is git reset command?

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.

What is the difference between git reset and git checkout?

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.

What is git reset soft and hard?

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.

What are Git checkout and Git reset?

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?

How do I reset a specific commit in Git?

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

How do I reset the current git branch?

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.

How to undo changes in Git?

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.


2 Answers

I tried this one and works well for me:

git checkout HEAD -- path 
like image 200
Ethan Zhang Avatar answered Oct 28 '22 13:10

Ethan Zhang


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.

like image 26
habitats Avatar answered Oct 28 '22 12:10

habitats