Some Interesting Git Command that I Learn This Week

Nana Lau
3 min readJun 7, 2021

Hello everyone! This is yet another “What I Learn This Week” type of blog from me again! Today I will be sharing some interesting git command that I learn from The Git and Github Bootcamp by Colt Steele. Let's get started!

The first one that I want to talk about is the git diff command. The git diff command allows you to view changes between commits, branches, files, the current working directory, and more. It is often used together with git status and git log to view how the repository changed over time. There are a few very useful tags that you can use along with the git diff command to compare changes:

  • git diff => list all the changes in our working directory that are NOT staged for the next commit
  • git diff HEAD => lists all changes in the working tree since your last commit
  • git diff -stage OR git diff -cached => list the changes between the staging area and the last commit (show me what will be included in my commit if I run git commit right now)
  • git diff HEAD <filename> => list all changes, staged or unstaged since the last commit in that file
  • git diff -staged <filename> => list all changes in that file that is staged
  • git diff <branch1>..<branch2> => compare changes between two branches
  • git diff <commit-hash1>..<commit-hash2> => compare changes between two commits

The second one that I would like to talk about is stashing. As Colt said “Git provides an easy way of stashing these uncommitted changes so that we can return to them later, without having to make unnecessary commits. git stash is a super useful command that helps you save changes that you are not yet ready to commit. You can stash changes and then come back to them later. This will be very useful when you are working on some code and you need to work on something else very urgently, you can stash your current work progress and work on them afterward.

  • git stash OR git stash save => save the changes but don’t make a commit and you can get back to them
  • git stash pop => to remove the most recently stashed changes in your stash and re-apply them to your working copy (Kind of like cut and paste)
  • git stash apply => to apply whatever is stashed away, without removing it from the stash. This can be useful if you want to apply stashed changes to multiple branches. It will apply the most recent stash when you run this command
  • git stash list => will show a list of all the stashes you have stashed away
  • git stash apply stash@<num> => to recall a particular stash
  • git stash drop stash@<num> => to drop a particular stash
  • git stash clear => to clear out all stashes

The third command that I want to cover is git restore. It can be very useful when you made some changes to a file since your last commit, but you realized that you actually didn’t want to keep those changes. Especially useful when you have made multiple big changes across a few files, but all of a sudden you don’t want any of those changes in your file anymore, and you can’t really identify what you have changed since your last commit. This is where git restore will comes in handy.

  • git restore <filename> => to restore the file back to the most recent commit
  • git restore --source {HEAD~<num> or <any-commit-hash>} <filename> => to restore the file back to the parent commit or a particular commit
  • git restore --staged <filename> => this is not really for reverting any file change, instead, it will un-stage your file and keep it in your local machine when you commit(like some secret file that include some API keys that was supposed to be kept in your local machine)

This is all that I would like to share for today! Hope this blog can help some of your workflow to become smoother and better!

Weeeeee~~~ (ノ^ヮ^)ノ*:・゚✧

--

--

Nana Lau

A real dummy who are trying to learn in a smart way