Git: Undoing

In the last instance of this series, how to add and commit the code using git. Here we move towards basic concepts, which will start to make ‘git’ sound useful as a tool.

Undoing Changes

Stashing

git stash is the command which undoes the changes made after the previous commit .

Stashing is the process by which the dirty state of the working directory can be saved in a stack of unfinished changes , that can be reapplied .

So if there are some unwanted changes in the working directory, that the user has applied after the last commit, the working directory is in an uncertain state. In this state , most operations cant be done because, git is unsure what to do with the unsaved/uncommitted changes in the working directory. In many occasions, it may be required to remove the changes from the working directory , and is some other situations it may be required to transfer these changes in a separate branch.

In all these above cases, stashing will be your friend. Using git stash will remove all the current unsaved changes and store in a git stack.To discard those changes , git stash drop can be used.

Screenshot from 2018-04-22 02-05-13

Screenshot from 2018-04-22 02-05-28

Undoing a wrong ‘add’/’stage’

git rm is the command to remove files that are staged for a commit.

Suppose if a commit requires all changes but from one particular file. In this situation , either we can add all the other individual files manually or we can just add all the changes in the repository and remove that particular change. This can be accomplished with the help of git rm.

 git rm --cached

Screenshot from 2018-04-22 02-08-37

Screenshot from 2018-04-22 02-08-50

--cached option mentions that rm operation is only meant to delete the file from the git’s index and not actually remove the file from the working directory.

 git rm

Screenshot from 2018-04-22 02-11-47.png

will permanantly remove the files mentioned both physically and from the index. Removing from the index means one can never recover any changes related to that particular file.

NOTE : It must be noted that simply removing the file ( by using ‘rm’ ) , which was already added/staged , simply removes the file physically. But it resides in the index and can be recovered by checkout like operation.

A stellar example of where this can be useful is while dealing with accidentally staged binaries. Binaries usually change by a large even for a small change in the code which produces it. So when some binary is accidentally staged into the index , it takes up a huge amount of space inside the index. Simply deleting the file would not reduce the space already taken by the binary in the index. In these cases, git rm should be used to ensure removal of binary from both the directory and also from the index

Undoing a wrong ‘commit’

This is by far the most used among the lot. Often in a project , it will be necessary to just undo a change introduced by a certain commit to test the code before the changes introduced by that particular commit.

In those cases, two sub-tools in git can help.

git reset

 git reset --soft

Screenshot from 2018-04-22 02-15-44.png

is used to perform a soft-reset on the working tree. It removes the changes introduces by the commit from the index, but keeps all the changes in working directory. ( i.e. git forgets the changes , but actual changes are left untouched ).

This can be useful whenever a commit is performed wrong . For example, in situations where unrelated files are added to a commit.

git reset --hard

Screenshot from 2018-04-22 02-17-15.png

is another variant of the same command, where git removes the change both from the index and the actual directory. ( i.e. git throws the changes out the window and forgets about it ! )

Precaution must be exercised before using this command, as all the changes brought in by that particular commit will be gone.

git checkout

git checkout is not a command for correcting wrongly done commits per se. But what git checkout does is, git temporarily keeps aside the changes brought in by the commit(s) , and undoes all the commits and gives the version of the working directory at the time of that particular commit.

This command technically undoes changes done by a commit . But does not remove anything from the index . So git here, keeps the changes with it , but changes the actual files inside the directory . So whenever you want the original version , git searches for that version in its index and reproduces that version of the working directory.

Screenshot from 2018-04-22 02-19-21

Screenshot from 2018-04-22 02-19-31

git checkout can perform more than the above mentioned . We will see about these later in the section about Branching.

Here , we saw how to remove accidentally or intentionally introduced code from the index and/or the working directory .In the next in the series , we will see about Branching in Git.

1 thought on “Git: Undoing”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s