0% found this document useful (0 votes)
14 views

Key Insights: Undoing Local Changes That Have Not Been Committed

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Key Insights: Undoing Local Changes That Have Not Been Committed

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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.

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:

1. In your terminal (Terminal, Git Bash, or Windows


Command Prompt), navigate to the folder for your Git
repo.
2. Run git status and you should see the affected file
listed.
3. 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
4. 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:

1. In your terminal (Terminal, Git Bash, or Windows


Command Prompt), navigate to the folder for your Git
repo.
2. Run git status and make sure you have a clean
working tree.
3. 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
4. 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).
5. 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.
6. 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.

1. In your terminal (Terminal, Git Bash, or Windows


Command Prompt), navigate to the folder for your Git
repo.
2. 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.
3. 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:

1. In your terminal (Terminal, Git Bash, or Windows


Command Prompt), navigate to the folder for your Git
repo.
2. Run git status and make sure you have a clean
working tree.
3. 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
4. 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.

1. To revert the latest commit and discard changes in the committed file do:
git reset --hard HEAD~1
2. To revert the latest commit but retain the local changes (on disk) do:
git reset --soft HEAD~1
This (the later command) will take you to the state you would have been if you did git add.
If you want to unstage the files after that, do

git reset
Now you can make more changes before adding and then committing again.

1 - Undo commit and keep all files staged: git reset --soft HEAD~
2 - Undo commit and unstage all files: git reset HEAD~
3 - Undo the commit and completely remove all changes: git reset --hard HEAD~
Remove git commit which has not been pushed

Some times we need to undo code from the remote branch which is not pushed yet into Git.

There are multiple ways to undo the commit, which has not been pushed yet.

Step 1:- Change in your file

Let's assume you have a project. Which should be pushed on Git. Now you should change in

one file. Now check the status through the following command.
$ git status

git status
Step 2:- Add and commit changes

Now you should add and commit these changes to the repository through the following

commands.
// Add file
$ git add validator.js// Commit code
$ git commit -m "change some validations"

git add

git commit

Step 3:- Check the status

Now, we will check the status through the following command.


$ git status

git status

Now, we will undo the commit. So there are multiple methods for undo, we will perform one

by one. Before performing the different methods we should know about logs. Use git log to

check all commits then you can decide how many commits do you want to roll back.
$ git log
git log

Step 4:- Remove Commit

There are three ways to remove commits.

1. Remove commit and keep file staged

For keeping files in the staged so we should have to use the following command.
$ git reset --soft HEAD~1

git soft reset


git status

2. Remove commit and unstaged file

If you want to remove the last commit and unstage all files then you can use to use the

following command.
$ git reset HEAD~1

git reset

git status

3. Remove commit and discard changes

In case you want to remove commit and discard all changes then you should use the following

command.
$ git reset --hard HEAD~1
git hard reset

Git reset vs. git revert


In choosing between git reset and git revert, it's important to
understand exactly what each one does. Git reset will delete commits
from the history, so if admins are working locally and haven't pushed any
commits, this will likely be the better option.

But if the team has pushed the commits, consider git revert instead.
That way, anyone who has pulled the bad commit can also pull the revert
commits as well.
How to Undo a Commit in GitHub
A very common task performed by developers while coding is undoing
a commit. This is a common mistake which prompts developers to turn
to Google for answers! In this article, I will be teaching you how to
undo these mistakes with Git.

There are four common scenarios where I think developers perform this
task:

 Undoing local changes that have not yet been committed


 Undoing a Commit (That Has Been Pushed) to a remote
branch/repository
 Undoing a Commit (That Has Not Been Pushed)
 Undoing Multiple Commits

UNDOING LOCAL CHANGES THAT HAVE


NOT YET BEEN COMMITTED
If you have made changes that you don't like, and they have not been
committed yet, do the following:
You can also use another command checkout to achieve the same thing,
so:

UNDOING A COMMIT (THAT HAS BEEN


PUSHED) TO A REMOTE
BRANCH/REPOSITORY
Each commit has a commit hash (A sequence of 7 random characters)
that looks something like this - 224bc7a, in order to start the process of
uncommiting changes that have been pushed, do the following

This will output the following; on the left you can see the commit
hashes followed by the commit messages.

To undo a specific commit use:


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
you use the git log –oneline command again you should see a new hash
and commit message that says “Reverting <previous commit message>”.
The --no-edit option prevents git from asking you to enter 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)

The last step is to push your changes to the remote branch:

UNDOING A COMMIT (THAT HAS NOT


BEEN PUSHED)
To undo a commit that has not been pushed to a remote repository, use
the reset command. Note that the reset command should be used if the
commits only exist locally. If not, use the revert command, that way the
history of undoing your commit is preserved. The command below also
works if you want to undo the last commit you have locally:

A great hack is to add a number to the end of ` ~` to undo multiple


commits. For example, to undo the last 2 commits - run git reset --soft
HEAD~2.
UNDOING MULTIPLE COMMITS (THAT
HAS BEEN PUSHED)
To undo multiple commits that are in a remote repository, you can use a
cool command called rebase, which allows you to interactively pick
what commits you want to keep or discard. You just have to give rebase
a starting point. Use the git log –oneline command to check for the hash
you want to use as a starting point.

In the example below, hash 58c2736 is the starting point.

In the vim editor, you can edit what commits you want to pick or
discard. By default, all commit hashes have the word pick before the
hash number, however, if you would like to delete a commit you press
the `i` key on your keyboard which allows you to edit the file and
change `pick` to `d`. To save changes, use `:wq` to close the vim editor.

You might also like