There are times when you (or a teammate) make a change that is not desired or causes your product or site to not work. Other times you forgot to include a certain change, or made a mistake in your commit message. In these situations, it’s common to want to rollback, or undo, a change made by you or your team.
How do I remove all uncommitted changes in my working directory?
Git offers many features to manage your project from different historical commits. For example, you can view an old commit, then create a branch from it. A hash is an alphanumeric code that identifies each commit. 'Discard' in SourceTree is a bit confusing as it is not, to my knowledge, analogous to a git command. SourceTree does tend to offer at least three ways to perform any given task, and that can be a bit confusing. In your situation, either Discard or Reset would have worked.
Sourcetree for Windows; SRCTREEWIN-5984; Show uncommit change incorrect. Undo in Git using SourceTree 17 January 2018 on Tips & Tricks, Development. If you are not a command-line person, working with Git could be a painful exercise. You can use SourceTree to make your Git workflow a lot simpler. One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the -amend option.
As well as uncommitted changes, there will inevitably changes that you have committed to your repo that you want to revert. This is not something that should be scary — it's one of the greatest benefits of version control. Let's look at a few common scenarios.
How do I add an additional file to a commit I just made?
How do I rollback a file to a certain commit in history?
How do I revert changes introduced by a specific commit?
Sourcetree Uncommit Files
Sourcetree Uncommitted Changes
How do I make a commit completely disappear from history?
# create and check out a temporary branch at the location of the bad merge
# remove the incorrectly added file
# commit the amended merge
# go back to the master branch
# replant the master branch onto the corrected merge
# delete the temporary branch
Local vs. remote
Revert uncommitted changes
You can always undo the changes you've made locally before you commit them:
In the Commit tool window Alt+0, select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z. All changes made to the selected files since the last commit will be discarded, and they will disappear from the active changelist.
Undo the last commit
IntelliJ IDEA allows you to undo the last commit in the current branch.
You cannot undo a commit if it was pushed to a protected branch, that is a branch to which force --push is not allowed (configure protected branches in the Settings/Preferences dialog Ctrl+Alt+S under Version Control Git) Note that if a branch is marked as protected on GitHub, IntelliJ IDEA will automatically mark it as protected when you check it out.
Open the Git tool window Alt+9 and switch to the Log tab.
Select the last commit in the current branch and choose Undo Commit from the context menu.
In the dialog that opens, select a changelist where the changes you are going to discard will be moved. You can either select an existing changelist from the Name list, or specify the name of a new changelist (the commit message is used by default).
Select the Set active option if you want to make the changelist with the changes you are about to discard the active changelist.
Select the Track context option if you want IntelliJ IDEA to remember your context and reload currently opened files in the editor when this changelist becomes active.
Revert a pushed commit
If you notice an error in a specific commit that has already been pushed, you can revert that commit. This operation results in a new commit that reverses the effect of the commit you want to undo. Thus, project history is preserved, as the original commit remains intact.
- Locate the commit you want to revert in the Log tab of the Git tool window Alt+9, right-click it and select Revert Commit from the context menu. This option is also available from the context menu of a commit in the file History view. The Commit Changes dialog will open with an automatically generated commit message.
If you apply this action to multiple commits selected in the Log view, a separate commit will be created to revert each of them.
If the selected commit contains several files, and you only need to revert some of them, deselect the files you do not want to touch.
Click Commit to commit a changeset that reverts the changes to the selected files in this particular commit.
Revert selected changes
IntelliJ IDEA lets you undo selected changes from a pushed commit if this commit contains multiple files and you only need to revert some of them.
In the Log view select the commit containing the changes you want to discard.
- In the Changed Files pane, right-click the file that you want to revert and select Revert Selected Changes from the context menu.
This results in a new commit that reverses the changes you want to undo.
Drop a commit
Unlike reverting a commit, which is reflected in the branch history, you can discard a pushed commit in the current branch without leaving any traces of the operation.
Like any operation that rewrites a branch history, dropping a commit requires a --force push and cannot be performed in protected branches (these can be configured in the Settings/Preferences dialog Ctrl+Alt+S under Version Control Git.
Select a commit you want to discard in the Log view and choose Drop Commit from the context menu.
Reset a branch to a specific commit
If you notice an error in a set of recent commits and want to redo that part, you can roll back your repository to a specific state. This is done by resetting the current branch HEAD to a specified commit (and optionally resetting the index and working tree if you prefer not to reflect the undo in the history).
Open the Version Control tool window Alt+9 and switch to the Log tab.
Select the commit that you want to move HEAD onto and select Reset Current Branch to Here from the context menu.
- In the Git Reset dialog that opens, select how you want your working tree and the index to be updated and click Reset:
Soft: all changes from commits that were made after the selected commit will be staged (that means they will be moved to the Local Changes view so that you can review them and commit later if necessary).
Mixed: changes made after the selected commit will be preserved but will not be staged for commit.
Hard: all changes made after the selected commit will be discarded (both staged and committed).
Keep: committed changes made after the selected commit will be discarded, but local changes will be kept intact.
Get a previous revision of a file
If you need to revert a single file instead of discarding a whole commit that includes changes to several files, you can return to a particular version of that file:
Select the required file in any view (in the Project tool window, in the editor, in the Local Changes view, and so on).
Select Git Show History from the main VCS menu or from the context menu of the selection. The History tab is added to the Git tool window showing the history for the selected file and allowing you to review and compare its revisions.
When you've identified the revision you want to roll back to, select it in the list and choose Get from the context menu.