My Brain
My Brain

Undoing Changes

Amending a commit

What if you commit a change to your remote repository only to realize later that you have a typo in the commit message or you forgot to add a line in your most recent commit. How do you edit that? This is what this tutorial covers.

Changing a recent commit message after you have pushed to Github

To do this without opening a file:

  • Type in the git commit --amend -m "followed by your new commit message"
  • Run git push origin <branch-name> to commit the changes to the repository.

Note: If you type in just git commit --amend, your text editor would open up prompting you to edit the commit message. Adding the -m flags prevents it.

Modifying on a single commit

So, what if we forgot to make a minor change to a file like changing a single word and we have already pushed the commit to our remote repository?

To illustrate here is a log of my commits:

g56123f create file bot file
a2235d updated contributor.md
a5da0d modified bot file

Let's say I forgot to add a single word to the bot file

There are 2 ways to go about this. The first is to have an entirely new commit that contains the change like so:

g56123f create file botfile
a2235d updated contributor.md
a5da0d modified botfile
b0ca8f added single word to botfile

The second way is to amend the a5da0d commit, add this new word and push it to Github as one commit. The second sounds better since it is just a minor change.

To achieve this, we would do the following:

  • Modify the file. In this case, I will modify the botfile to include the word I omitted previously.
  • Next, add the file to the staging area with git add <filename>

Usually after adding files to the staging area, the next thing we do is git commit -m "our commit message" right? But since what we want to achieve here is to amend the previous commit, we would instead run:

  • git commit --amend This would then bring up the text editor and prompt you to edit the message. You can decide to leave the message as it was before or change it.
  • Exit the editor
  • Push your changes with git push origin <branch-name>

That way, both changes would be in one single commit.

Reset

To undo a local commit, all you need to do is

git reset

This command will reset your staging area to your most recent commit, but the changes you made to your working directory will not change. So, you can still re-commit again what you've changed. Or, if you only want to remove one file from your previous commit. Then, you can do the command below

git reset <file>

The command will remove only the specified file from the staging area, but changes made on the file will still remain.

Example of git reset usage

# Make changes in index.php and tutorial.php
# Add files into the staging area
git add .
# Remembered both files need to be committed separately
# Unstage tutorial.php
git reset tutorial.php
# Commit index.php first
git commit -m "Changed index.php"
# Commit tutorial.php now
git add tutorial.php
git commit -m "Changed tutorial.php"

Let's say if you have messed up your local repository and you just want to reset it to your last commit.

Then, you can run the command below.

git reset --hard

The command will not only reset your staging area, but also revert all your changes on the files to your last commit.

The mode --hard tells Git to undo all the changes in the working directory too.

You should only run this when you are really sure of throwing your whole local development out.

Example of git reset --hard usage

# Decided to start a crazy experiment
# Create a new file 'crazy.php' and add some code to it
# Commit crazy.php
git add crazy.php
git commit -m "Started a crazy dev"
# Edit crazy.php file again and changed a lot of other files
# Commit all tracked files
git add .
git commit -m "Continued dev"
# Tested and things went out of hand
# Decided to remove the whole things
git reset --hard HEAD~2

The git reset --hard HEAD~2 moves the current branch backward by 2 commit points at the same time reverting all changes you have made and remove the 2 snapshots we have just created from project history.

Never perform git reset --hard if you've already pushed your commits to a shared repository as it will cause problems to everyone on that repository.

Revert

To revert a commit simply means to create a brand new commit that undoes all the changes made in a previous one. It is like doing a CTRL + Z on git.

Reversion is made easier in git because every commit you push to your remote repository has a unique alphanumeric key known as SHA(Secure Hash Algorithm) tied to it.

So this means you can revert any commit as long as you have the SHA.

But then, you have to be careful to reverse orderly so as not to mess your repository up.

To pick out the SHA of the specific commit we want to undo, a log of all the commits we have made so far would come in handy. To get this, we would run the command:

git log --oneline

Running the git log command alone would also give us the SHAs (in long form)

However using the --oneline flag tells git that we want it displayed in a concise (one line) manner for easy read.

The first 7 characters displayed when you run this command is called the abbreviated commit hash.

For example, here is what I get when I run git log --oneline on this repository:

389004d added spacing in title
c1b9fc1 Merge branch 'master' into tutorials
77eaafd added tutorial for reverting a commit

So this shows that with git log --oneline, we can fetch a list of all the commits made on the repository together with the first 7 characters of its SHA.

Now, Let's assume I want to undo my commit of "added spacing in title", here are the steps I would take:

  • Copy the SHA of the commit which, in this case is 389004d
  • Then, run the command git revert 389004d

This would pop open my text editor and prompt me to edit the commit message.

You can decide to leave the commit message as the default git message which starts with the word Revert or you can also decide to customize the message to your liking.

  • Next, I will save and close the text editor.
  • Return to the command line.
  • Run git push origin <branch-name> to push the reverted changes to Github.

And that is it, the change would be undone. In this case, my repository would be reverted to how it looked like in c1b9fc1

Removing a file from Git

Sometimes, you may want to remove a file from Git but not delete it from your computer. You can achieve this by using the following command:

git rm <file> --cached

So what happened?

Git will no longer keep track of changes in the removed file. As far as Git knows, it's as if you had deleted the file. If you were to locate the file in your file system, you will notice that it's still there.

Notice that in the example above, the flag --cached is used. If we didn't add this flag, Git will remove the file from not just the repo, but from your file system too.

If you commit the change with git commit -m "Remove file1.js" and pushed it to the remote repository using git push origin master, the remote repository will remove the file.

Additional features

  • If you want to remove more than one file, you can include them all in the same command:

    git rm file1.js file2.js file3.js --cached

  • You can use a wildcard (*) to remove similar files. For example, if you would like to remove all .txt files from your local repository:

    git rm *.txt --cached

Backlinks