There are several ways you can revert a previous git commit and it depends on your requirement.
- Temporarily switch to a previous commit – git checkout <commit_sha>
- Create a branch from previous commit – git checkout -b <new_branch_name> <commit_sha>
- Revert to previous commit keeping history – git revert <commit_sha>
- Revert to previous commit discarding history – git reset –hard <commit_sha>
Example:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
## Create a new git local repository git init myrepo && cd myrepo ## Create first commit echo "cat" > file1 git add file1 git commit -m "commit1" ## Create second commit echo "dog" >> file1 echo "bat" > file2 git add . git commit -m "commit2" ## Create third commit echo "cow" >> file1 echo "rat" >> file2 git add . git commit -m "commit3" ## Check git logs git log --oneline ## dfea93a (HEAD -> master) commit3 ## c13ee80 commit2 ## 6059d8c commit1 ## Check git status git status ## nothing to commit, working tree clean ## temporarily go back to previous commit (commit1) git checkout 6059d8c ## You are in 'detached HEAD' state. You can look around, make experimental ## changes and commit them, and you can discard any commits you make in this ## state without impacting any branches by switching back to a branch. ## Check git logs git log --oneline ## 6059d8c (HEAD) commit1 ## Check git status git status ## HEAD detached at 6059d8c ## nothing to commit, working tree clean ## move back to current commit (commit3) git switch - ## Check git logs git log --oneline ## dfea93a (HEAD -> master) commit3 ## c13ee80 commit2 ## 6059d8c commit1 ## Check git status git status ## On branch master ## nothing to commit, working tree clean ## create a new branch from previous commit git checkout -b feature 6059d8c ## Check git logs git log --oneline ## 6059d8c (HEAD -> feature) commit1 ## Check git status git status ## On branch feature ## nothing to commit, working tree clean ## get back to current commit git checkout master git branch -d feature ## Check git logs git log --oneline ## dfea93a (HEAD -> master) commit3 ## c13ee80 commit2 ## 6059d8c commit1 ## Check git status git status ## On branch master ## nothing to commit, working tree clean ## switch back to previous commit (keeping history) git revert --no-commit 8ff9c78 c3a660b git commit -m "commit4" ## Check git logs git log --oneline ## 1fc7b61 (HEAD -> master) commit4 ## dfea93a commit3 ## c13ee80 commit2 ## 6059d8c commit1 ## Check git status git status ## On branch master ## nothing to commit, working tree clean ## switch back to previous commit (discarding history) git reset --hard 6059d8c ## Check git logs git log --oneline ## 6059d8c (HEAD -> master) commit1 ## Check git status git status ## On branch master ## nothing to commit, working tree clean ## delete the repository cd .. && rm -rf myrepo |