Learn Git in a scenario-based way

date
Aug 21, 2021
status
Published
tags
git
summary
type
Post
The git commands you will learn:
  1. git stash
  1. git reset
  1. git cherry-pick
  1. git blame
  1. git reflog
  1. 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:
notion image
I have two uncommitted files. The below git command is used to save the new changes into a local temp zone.
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.
notion image
This will list all the saved stashes locally like the below screenshot.
notion image
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
After doing it, the previously saved changes will reapply. We are then able to continue to work on it.
notion image
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?

This will delete all new changes locally.
notion image

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

Option 1:
This command will remove the previous commit messages and leaves us in a state where “Changes need to be commit”.
notion image
We can just use git commit -m "commit message name"
Option 2:
This is very similar to “soft”. It will leave us in a state where we have staged these changes like this
notion image
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
notion image
This is the hash for this git commit.
Step 2: Go to feature branch and do this:
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?

It tells us who made what changes to which file.
notion image
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:
notion image
The deleted git commit is gone.
git reflog will show me everything I did previously.
notion image
Find the commit sha, and do this:

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

© ming 2021 - 2025