Learn Git in a scenario-based way

MING WU
5 min readAug 21, 2021

--

The git commands you will learn:

  1. git stash
  2. git reset
  3. git cherry-pick
  4. git blame
  5. git reflog
  6. git rebase

These are all very important and easy-to-understand git commands in daily dev workflow.

git stash

1. I am currently in ticket001 branch adding a new feature. I just finished 50% of the work but I have not committed anything. I am assigned an emergent bug fix ticket. I need to do the bug fix ticket right now. What should I do with my uncommited work?

Steps in English:

For instance, here is my git status:

I have two uncommitted files. The below git command is used to save the new changes into a local temp zone.

git stash save "WIP: change new files"

After doing it, the new changes are saved, and will disappear so that I am able to check out to a new branch to fix the bug.

git stash list

This will list all the saved stashes locally like the below screenshot.

stash{0} is like a reference to the first stash. We can use it to re-apply the changes on any branch using git stash apply

git stash apply 'stash{0}'

After doing it, the previously saved changes will reapply. We are then able to continue to work on it.

git reset: with --soft HEAD, --mixed HEAD, --hard HEAD

2.1 I did a lot of changes but I want to delete all of them. What should I do?

git reset --hard HEAD

This will delete all new changes locally.

2.2 I am working on a ticket but I made 3 commit messages. How could I squash them into one?

Option 1:

git reset --soft HEAD~3

This command will remove the previous commit messages and leaves us in a state where “Changes need to be commit”.

We can just use git commit -m "commit message name"

Option 2:

git reset --mix HEAD~2

This is very similar to “soft”. It will leave us in a state where we have staged these changes like this

We just need to manually git add and git commit -m

git cherry-pick

3. I made a commit in master branch but I should make it in feature branch. How can I move the git commit to feature branch?

Step 1: Find the git commit hash by git log

This is the hash for this git commit.

Step 2: Go to feature branch and do this:

git cherry-pick d7ecb81fd5d5789d729240dfbb7fcc6b4022c404

This commit is copied from the one in master to the one in feature.

git blame

3. Someone introduced a bug and I need to know who changed this file?

git blame 'name of the file'

It tells us who made what changes to which file.

I prefer to use git blame function in webstorm. It is easier to visualize the change.

4. I accidently used git reset --hard HEAD^1 to permanently deleted the last commit. How can I recover it?

If I deleted the first commit with sha of ‘87489b7’ and I am in this state:

The deleted git commit is gone.

git reflog will show me everything I did previously.

Find the commit sha, and do this:

git reset --hard 87489b7

5. If you are working on feature branch (checked out from master 5 days ago). but master is updated to the newest from server. You hope that the new commit in feature branch is on top of the newest commit in master. It will look like your feature branch just checked out from master. How do you do it?

Your base was the master branch. Now you need to change your base to the new master branch (the one with new commits).

Step 1: Make sure master branch is already up to date with server. Now Go to feature branch by git checkout feature

Step 2:

do the rebase command in feature branch.

git rebase master

After doing these, your old base will be changed to the new master branch. It looks like you just checked out from master (instead of checking it out from master 5 days ago). But if you merge master, you commit will be behind the new commits in master.

Remember: git rebase is another kind of git merge for different purposes. git rebase changed the history. git merge keeps what the history is. When you made a commit on a branch, ask yourself whether you want your commit to be on the top of new coming commits or behind the new coming commits. If you want your commit on the top, then use git rebase otherwise use git merge.

References:

https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/

Originally published at https://codewithming.hashnode.dev.

--

--

MING WU

aws solution architect associate, developer associate, top 11% in stackoverflow, https://linktr.ee/mingw