Some lesser-known git commands
I am a developer who develops new stuff from scratch
Git is such a complex tool that I often feel as if I'm barely using 10% of its complete functionality. The various commands range from the absolutely essential (commit, push, pull) to the more exotic to the downright obscure or scary. However, this list compiles 6 commands that you may not know about that have seriously improved the way in which I use git.
git log -p
git log is git's way of showing you the history of your codebase, and adding a path to the command will limit the log to the changes in that one file. Finally, the -p flag includes the diff on the file at each commit, so you can see exactly how the file has changed at each point in the history of the code.
$ git log -p README.md
git checkout
If you want to see the state of your code at a particular point in history, git makes this trivial. Just run this command with a commit hash as the argument (you can use git log to find the hash you want to revert to).
$ git checkout 08c6fa
git checkout --
This is useful in the case when you need to restore a deleted file, or restore a file to a previous state. Here, refers to a commit hash, tag or branch, and is the path of the file relative to the top directory of the project.
$ git checkout 05c5fa -- config/routers.rb
git stash
This is an extremely useful command that isn't as well known as it should be. If you have uncommitted changes in your tree that you want to temporarily undo, then this command allows you to stash them for later. This is particularly useful if you want to check the state of your code before your changes, or if you want to merge in someone else code but aren't yet ready to commit your code as it is.
$ echo "This is a temporary" >> README
git cherry-pick
This command allows you to apply a single commit to your working tree. For example, if there's a commit in another branch that you want to apply but the branch isn't ready to fully merge, you can use this command to grab that commit and drop it into your current branch.
$ git cherry-pick 9f4afc
git annotate
This handy command shows each line of a file next to the information about which commit last changed that line, when it changed, and who changed it.
$ git annotate Readme