Chuck's Academy

GIT

Undoing Changes in Git

Throughout development, we will make mistakes. Fortunately, Git offers various tools to undo changes and fix errors. In this chapter, we will explore how to undo modifications in files, revert commits, and restore a project's state to previous versions. By the end of this chapter, you'll know how to handle complex situations and maintain control over your repository's history.

Undoing Changes in Uncommitted Files

When you make changes to files but haven't yet added them to the staging area, you can easily undo those changes and restore the files to their original state.

To restore a modified file without adding it to the staging area, use the command:

bash
"The command 'git restore followed by the filename' undoes uncommitted changes in that file, returning it to its previous state."

If you have already added the file to the staging area but want to undo that action, you can use:

bash
"The command 'git restore --staged followed by the filename' removes that file from the staging area but keeps the changes in the working directory."

Reverting Commits

Sometimes, a commit contains errors or unwanted changes. Git offers two main ways to handle this situation: git reset and git revert.

Git Reset

The command git reset allows you to move your branch pointer to a previous commit, removing subsequent commits in the process. This is useful when you want to "erase" recent commits and return to a previous state.

bash
"With the command 'git reset --hard followed by the commit identifier,' you return to a previous state of the repository, removing recent commits and discarding changes in the working directory."

If you don't want to lose the changes, you can use the --soft option, which preserves the modified files in your working directory:

bash
"The command 'git reset --soft' keeps the changes in the working directory while bringing the commit pointer back to a previous version."

Git Revert

Unlike git reset, the command git revert does not delete commits. Instead, it creates a new commit that "reverts" the changes made in a previous commit. This is useful when you need to maintain the change history but want to undo a specific commit.

bash
"The command 'git revert followed by the commit identifier' creates a new commit that undoes the changes made in that commit, without altering the prior history."

Recovering Deleted Commits

If you accidentally deleted commits using git reset, you can recover them using the git reflog command. This command tracks all recent actions performed in the repository, allowing you to restore deleted commits.

bash
"The command 'git reflog' shows a history of all recent actions, including deleted commits, branch changes, and resets. This allows you to find the lost commit."

Once you find the commit you want to recover, you can use git reset or git checkout to restore it.

Using Git Stash to Save Temporary Changes

If you are working on changes but need to switch context or branch quickly, you can temporarily "stash" those changes using git stash. This saves your modifications without committing, allowing you to retrieve them later.

To save the current changes:

bash
"The command 'git stash' temporarily saves your current changes, removing them from the working directory and allowing you to switch branches or perform other tasks without losing your work."

To retrieve the stashed changes:

bash
"The command 'git stash pop' retrieves the previously saved changes with git stash, returning them to the working directory."

You can view the list of all saved stashes using:

bash
"The command 'git stash list' shows all the changes you have temporarily saved with git stash."

Conclusion

In this chapter, we have learned how to undo changes in Git, from reverting modifications in individual files to recovering deleted commits. With these tools, you can handle errors without fear, knowing you can always return to a previous state. In the next chapter, we will explore how to work with tags in Git and how to use them to mark important versions in a project's history.


Ask me anything