Besides some very useful git commands, such as git add, git status, git commit, etc., there are three additional commands that gives more flexibility to your work— they are git reset, git checkout and git revert.

credit to Atlassian Bitbucket tutorials

It is help to think about each command having effect on three state management mechanisms. They are Working directory, Staged snapshot and commit history.

CommandScopeCommon use cases
git resetcommit-levelDiscard commits in a private branch or throw away uncommited changes
git resetfile-leverUnstage a file
git checkoutcommit-levelSwitch between branches or inspect old snapshots
git checkoutfile-levelDiscard changes in the working directory
git revertcommit-levelUndo commits in a public branch
git revertfile-levle(N/A)

reset Link to heading

resetting means you move your HEAD forward/previous commits. And it will DELETE dangling/orphaned commits.

                               HEAD/Hotfix
                                    |
                                    V
                       / N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master                

git checkout hotfix git reset HEAD~2

                    HEAD/Hotfix
                         |
                         V
                       / N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master                

there are options like --soft, --mixed and --hard, for more details, find online.

checkout Link to heading

checkout doesn’t move any branches, it just moves the pointer points to the git tree.

                               HEAD/Hotfix
                                    |
                                    V
                       / N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master                

git checkout HEAD~2

                       HEAD       Hotfix
                         |          |
                         V          V
                       / N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master                

You should always make sure at least an additional pointer that is pointing to HEAD so that you don’t lose control on head.

revert Link to heading

As the time I am writing this, I find I rarely use this command. However, it is useful at some circumstances.

reverting undoes a commit by creating a new commit.

                         *        Hotfix
                         |          |
                         V          V
                       / N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master  
* is where a I want to undoes the commit, by undoing changes and add new commit              

git checkout hotfix

git revert HEAD~2

                       used
                        to
                        be 
                         *             Hotfix
                         |                |
                         V                V
                       / N --- N --- N ---N
                      /
                     /
   N --- N --- N --- N --- N --- N
                                 ^
                                 |
                               master                

git revert does add changes to existing commit history. for this reason, git revert should be used on public branches and git reset should be used on private branches.

branch Link to heading

git branch <new_branch_name> is used to create a new branch here at HEAD pointer.