In this series of articles, we are going to learn the basics of the most popular versioning system today - Git. We already covered some basic commands and interacting via the Command Line Interface (CLI). In this part, we are going to use the most common techniques to undo changes and alter history.
When committing changes, it is likely you mistype the commit message. Or you want to change the commit message before pushing to remote repository. Git's solution for this problem is using --amend flag on commit command like so:
$ git commit --amend
This makes Git open the default text editor, allowing us to change the message and overwrite the last commit.
One of the most common parts of the Git workflow is adding files to the staging phase, by invoking add command. In the previous part we saw that files added to staging are ready for the commit. But there are times when you add files to staging by accident and want to remove them. You can unstage files by running:
$ git reset HEAD
Note: Running this command moves changed files into the modified phase, while new files remain intact.
By changing the file itself, you tell Git to put it into the modified state. If you are not satisfied with changes you’ve made, you can revert them back to initial state (last commit) by running:
$ git checkout <file>
This command is also one of the most dangerous. This is because Git doesn’t have a mechanism for undoing what you removed from the modified stage. In other words, you need to explicitly define what’s going to be saved and what not. The best practice is to often stage and commit changes, as Git allows us to go back in time and modify history if we want.
Committing changes opens a world of possibilities for altering the history and reverting changes back in time. The first option for undoing commit is revert command:
$ git revert
The cool thing about this one is that it actually doesn’t alter the history, it creates a completely new commit with opposite changes. If something is removed, Git will add it back, and vice versa. This is the safest way of undoing things. If you don’t want to create a new commit, but still want to undo the changes, you can use:
$ git reset $ git reset --hard
This deletes all commits like they never happened, up to the one specified by commit hash value, . The difference between the --soft (or no flag) and --hard is:
This sounds the same as with checkout command. Yet, there's an mechanism for undoing reset commands using:
$ git reflog
Reflog lists all recent HEAD states in the repository, represented with an short SHA value and the message. In other words, whenever the HEAD has changed, Git takes a snapshot so you can redo changes with:
$ git reset --hard
Undoing things in Git can be confusing at first. There are a few options allowing us to go back in time, either by modifying history or reverting changes and adding new commits on top. The takeaway from this is to always commit changes and leverage these options. Otherwise, chances are you might lose your work. The next article will be about branches and working with remote repositories.
Fill out the enquiry form and we'll get back to you as soon as possible.