In this tutorial you will learn about the Git reset command: how to perform a git reset and how to use different modes on git reset: soft, mixed and hard.
Code used during this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# Git reset command # Reset current HEAD to the specified state # Save git log output to ~/git-log.txt git log > ~/git-log.txt vi ~/git-log.txt # Soft mode git log # Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all # modes do). This leaves all your changed files "Changes to be committed", as git status would put it. git reset --soft e4d661ac9a11f69c91 git status git log git diff --staged git reset --soft 299cf85ba7b934453ca40df2ff06af8c6438b474 git status git log # Mixed mode git log # Resets the index but not the working tree (i.e., the changed files are preserved but not marked for # commit) and reports what has not been updated. # This is the default mode in git reset git reset --mixed e4d661ac9a11f69c91 git status git log git diff # See the default mode in git reset git add . git reset HEAD file-1.txt git reset --mixed 299cf85ba7b934453ca40df2ff06af8c6438b474 git status git log # Hard mode git log # Resets the index and working tree. Any changes to tracked files in the working tree since <commit> # are discarded. git reset --hard e4d661ac9a11f69c91 git status git log git reset --hard 299cf85ba7b934453ca40df2ff06af8c6438b474 git status git log # Commit and reset --hard backup git log git reset --hard e4d661ac9a11f69c91 git status git log echo "Hello world" > file-4.txt git add . git commit -m "file-4.txt with Hello world content" git log > ~/git-log-2.txt git reset --hard 299cf85ba7b934453ca40df2ff06af8c6438b474 git status git log ls git reset --hard 1d423d504770cf5da53f4471535e968a0fc834d0 git status git log # Man page man git-reset # Useful links https://www.liviubalan.com/git-head-explained |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
git init echo "Hello world" > file-1.txt echo "Hello world" > file-2.txt echo "Hello world" > file-3.txt git add . git commit -m "file-1.txt, file-2.txt and file-3.txt with Hello world content" echo "Hello people" >> file-1.txt git add file-1.txt git commit -m "Add Hello people content to file-1.txt" echo "Hello people" >> file-2.txt git add file-2.txt git commit -m "Add Hello people content to file-2.txt" echo "Hello people" >> file-3.txt git add file-3.txt git commit -m "Add Hello people content to file-3.txt" |
Useful links:
https://www.liviubalan.com/git-head-explained