Git : Undoing Changes

Since Git has so many components, “undoing” can take
on many different meanings. For example, you can:

  • Undo changes in the working directory
  • Undo changes in the staging area
  • Undo an entire commit
    • Unmodifying a Modified File

    • What if you realize that you don’t want to keep your changes you just did to your files in your working directory ? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, git status tells you how to do that, too.

      • Running git status after modifying a file gives the following result :
        # On branch master
        # Changes not staged for commit:
        #   (use "git add ..." to update what will be committed)
        #   (use "git checkout -- ..." to discard changes in working directory)
        #
        #	modified:   ex2.py
        #
        
      • It tells you pretty explicitly how to discard the changes you’ve made.
        On the terminal, within your project directory, Run :

        git checkout -- <filename> 

        Example :

        ~$ git checkout -- ex2.py
    • Unstaging a Staged File

    • Unstaging, in other words, means Undoing ‘git add’. Once you have added the files using git add, say I added ex2.py, how can I revert it ?
      The command to undo git add is :

      git rm --cached <filename>

      Example:

      ~$ git rm --cached ex2.py 
    • Changing Your Last Commit

    • 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 try that commit again, you can run commit with the –amend option:

      ~$ git commit --amend

      This command takes your staging area and uses it for the commit. If you’ve have made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same and all you’ll change is your commit message.( You can edit the message the same as always, but it overwrites your previous commit ).
      Now, for an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:

      ~$ git commit -m 'initial commit' 
      ~$ git add forgotten_file
      ~$ git commit --amend

      All three of these commands end up with a single commit — the second commit replaces the results of the first.
      Similarly,

      To remove a file from last commit

      Run :

       ~$ git rm--cached <filename> 
      ~$ git commit --amend
    • Undoing the Commits

    • Undoing the commit is little different from the changing the commit using –amend option. Undoing the commit completely reverts the changes made by the specific commit.
      One way to undo a commit is :

      • Use the following command :
        ~$ git reset HEAD~1

        OR

        ~$ git reset --soft HEAD~1
      • The HEAD~1 syntax parameter specifies the commit that occurs immediately before HEAD (likewise, HEAD~2 refers to the second commit before HEAD). By moving the HEAD reference backward, you’re effectively removing the most recent commit from the project’s history.

      • Another way to undo the commits is Reverting. To remedy the problems introduced by resetting public commits, Git developers devised another way to undo commits: the revert. Instead of altering existing commits, reverting adds a new commit that undoes the problem commit:
        ~$ git revert 
      • where the commit-id is the SHA of your desired commit that can be seen using ‘git log’ command.
        This command takes the changes in the specified commit, figures out how to undo them, and creates a new commit with the resulting changeset.

      • Undoing the ‘Undid’ Commit

      Confused ? What I mean here is, what if you undid a commit and realize later that the undid commit was supposed to be there, what then ? In other words, how to undo an ‘undid commit’ or how to undo ‘git reset HEAD’ ?
      Here’s the solution that comes to your rescue. Run the following set of commands :

      • ~$ git reflog show master
      • where master can be replaced by your desired branch.

      • ~$ git reset 
      • where sha is the SHA of your desired commit you want to go to and find its SHA using git log command.

      Advertisements