Have you ever committed something but realised that you forgot to add a file or added a file that you shouldn't have?
This post will look at how to revert your last commit without losing your changes.
Imagine you're working with different files in a project but you want to commit a single one of them. You add your changes and commit the file; however, you realise that you have also committed another file that you shouldn't.
How do you revert this without losing any change esp. the other file you've committed?
The simplest solution is to use the
git reset command.
git reset --soft HEAD~1
git reset --soft HEAD^
--soft flag will reset the commit but leave the changes in your files.
HEAD^ will reset the last commit.
In this post, we looked at how to revert your last commit without losing your changes. This is extremely important when working on complex projects as you don't want to lose any changes you've made -- while still being able to modify your commit history.
Other ways to do the same include:
- git revert
- git amend
- git rebase