Git examples: Resetting, Undoing and Reverting Changes
Last updated:- Reset working directory to remote branch
- Revert working directory to last commit
- Revert working directory to commit
- Restore file from commit
- Restore file from previous commit
- Undo commit
- Unstage changes
- Add changes to previous commit
- Untrack file
- Unstage change in file
- Undo git add
- Change commit message
- Revert file to previous commit
Git version used: 2.7.4
Reset working directory to remote branch
Warning You will lose unsaved/uncommited work in the working directory!
Reset the working directory to look exactly like the code in the remote branch:
$ git fetch --all
$ git reset --hard origin/another-branch
Revert working directory to last commit
Warning You will lose any unsaved changes!
In other words, make your working directory look exactly the same as it was after the previous commit.
$ git reset --hard HEAD~1
Revert working directory to commit
Warning You will lose any unsaved changes!
That will make your working directory mirror what your Repository was like after commit <commit_hash>
$ git reset --hard <commit_hash>
Restore file from commit
Revert a file to the way it was on a given commit.
Useful for when you've modified or deleted a file by mistake.
Use git checkout <commit_hash> -- path/to/file
Example: Restore file.txt
the way it was in this commit:
$ git checkout f6f207 -- file4.txt
Restore file from previous commit
When you mistakenly delete/modify a file and commit it
Use this to revert a file to how it was in the previous commit.
Use git checkout HEAD~1 -- path/to/file
Example: Revert file.txt
the way it was before the previous commit:
$ git checkout HEAD~1 -- file.txt
Undo commit
No files in the working directory will be changed.
The repository will be pointing to the previous commit.
$ git reset --soft HEAD~1
Unstage changes
I.e. un-add all added files:
$ git reset --soft HEAD
Add changes to previous commit
Just add
new files and then run git commit --amend
To add file file1.txt
to the previous commit:
$ git add file1.txt
$ git commit --amend
This will open up the previous commit message in case you want to edit it or keep the same message.
Untrack file
In other words: make git forget about a file but keep it in working directory.
Use git rm --cached <path-to-file>
and then commit
Example:
Remove the file from git
$ git rm --cached myfile.txt rm 'myfile.txt'
Check it is still there (it's now untracked)
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: myfile.txt Untracked files: (use "git add <file>..." to include in what will be committed) myfile.txt
Commit changes
$ git commit -m "Removed myfile.txt from Git, but kept it in directory" 1 file changed, 5 deletions(-) delete mode 100644 myfile.txt
(P.S. it'll always show as untracked unless you add it to .gitignore
)
Unstage change in file
- To make git forget about a file instead, see untrack file
To unstage (un-add) changes in some file, use git reset HEAD <file>
:
Example: add text to some file, stage (add
) changes and then unstage those changes:
- Change and add the file:
$ echo "bar" >> my-file.txt
$ git add my-file.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: my-file.txt
- Now unstage:
$ git reset HEAD file
Unstaged changes after reset:
M my-file.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: my-file.txt
no changes added to commit (use "git add" and/or "git commit -a")
Undo git add
This is the same as unstaging changes in a file.
To undo adding a file to a commit (but keep it tracked) use $ git reset HEAD path/to/file.txt
.
Change commit message
git commit --amend
will open a text editor for you to change the last commit message
$ git commit --amend
Revert file to previous commit
This is what you should do to reset one single file to the way it was in the previous commit:
$ git reset HEAD~1 -- path/to/file
Then you can re-add/re-commit it again into the repo's history.