Learn how to undo local changes, specific commits, and last commits in Git through this comprehensive guide. Master the commands and steps to navigate your Git repo and keep your projects clean and error-free.
Key Insights
- Uncommitted local changes in Git can be undone by navigating to the Git repo folder on your terminal, running git status to see the affected files, and running the command 'git checkout filename.html', which reverts the file to its state at the previous commit.
- A specific commit that has been pushed can be undone by using the unique hash for the commit. The commit hash can be found in the commit history on GitHub, Bitbucket, or by running 'git log --oneline' on your terminal. The command 'git revert 2f5451f --no-edit' will create a new commit that reverses the existing commit.
- The last commit that has not been pushed yet can be undone by running 'git reset --soft HEAD~' on your terminal. This command undoes the latest commit, keeps the changes in place, and reverts the files back to the staging area.
- Local changes that have been committed but not yet pushed can be undone by running 'git reset 2f5451f' or 'git reset --hard 2f5451f' on your terminal. While the 'git reset' command removes the commits but keeps the changes as uncommitted, 'git reset --hard' undoes the commits and discards the changes.
- Navigate the commands and steps to undo changes and commits in Git with real-world projects through comprehensive courses. These courses are offered by Noble Desktop and are taught by expert instructors.
- Opportunities for further learning and skill development are available through a variety of courses and bootcamps offered in areas such as web development, Python, data science, and web design.
Sometimes you make a mistake and want to go back to a previous version. Here's how to rollback changes.
Undoing Local Changes That Have Not Been Committed
If you have made changes that you don't like, and they have not been committed yet, do as follows:
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder for your Git repo.
- Run Git status and you should see the affected file listed.
- Run the following command, replacing filename.html with your file path (which you could copy and paste from the Git status command):
- Git checkout filename.html
- That file has now been reverted to the way it was at the previous commit (before your changes).
Undoing a Specific Commit (That Has Been Pushed)
If you have one specific commit you want to undo, you can revert it as follows:
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder for your Git repo.
- Run Git status and make sure you have a clean working tree.
- Each commit has a unique hash (which looks something like 2f5451f). You need to find the hash for the commit you want to undo. Here are two places you can see the hash for commits:
- In the commit history on the GitHub or Bitbucket or website.
- In your terminal (Terminal, Git Bash, or Windows Command Prompt) run the command Git log—oneline
- Once you know the hash for the commit you want to undo, run the following command (replacing 2f5451f with your commit's hash):
- Git revert 2f5451f—no-edit
- NOTE: The —no-edit option prevents Git from asking you to enter in a commit message. If you don't add that option, you'll end up in the VIM text editor. To exit VIM, press : to enter command mode, then q for quit, and finally hit Return (Mac) or Enter (Windows).
- This will make a new commit that is the opposite of the existing commit, reverting the file(s) to their previous state as if it was never changed.
- If working with a remote repo, you can now push those changes:
- Git push
Undoing Your Last Commit (That Has Not Been Pushed)
If you made a mistake on your last commit and have not pushed yet, you can undo it. For example, maybe you added some files and made a commit, and then immediately realized you forgot something. You can undo the commit, and then make a new (correct) commit. This will keep your history cleaner.
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder for your Git repo.
- Run this command:
- Git reset—soft HEAD~
- TIP: Add a number to the end to undo multiple commits. For example, to undo the last 2 commits (assuming both have not been pushed) run Git reset—soft HEAD~2
- NOTE: Git reset—soft HEAD~ is the same as Git reset—soft HEAD^ which you may see in Git documentation.
- Your latest commit will now be undone. Your changes remain in place, and the files go back to being staged (e.g., with Git add) so you can make any additional changes or add any missing files. You can then make a new commit.
Undoing Local Changes That Have Been Committed (But Not Pushed)
If you have made local commits that you don't like, and they have not been pushed yet you can reset things back to a previous good commit. It will be as if the bad commits never happened. Here's how:
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder for your Git repo.
- Run Git status and make sure you have a clean working tree.
- Each commit has a unique hash (which looks something like 2f5451f). You need to find the hash for the last good commit (the one you want to revert back to). Here are two places you can see the hash for commits:
- In the commit history on the GitHub or Bitbucket or website.
- In your terminal (Terminal, Git Bash, or Windows Command Prompt) run the command Git log—oneline
- Once you know the hash for the last good commit (the one you want to revert back to), run the following command (replacing 2f5451f with your commit's hash):
- Git reset 2f5451f
- Git reset—hard 2f5451f
- NOTE: If you do Git reset the commits will be removed, but the changes will appear as uncommitted, giving you access to the code. This is the safest option, because maybe you wanted some of that code and you can now make changes and new commits that are good. Often though you'll want to undo the commits and through away the code, which is what Git reset—hard does.
Grow Your Skills
We offer a full suite of coding courses for students of all levels. Learn through real-world projects from expert instructors. Check out our coding bootcamps and courses now: