Git - Fix Mistakes

Advertisements


To err is human. So every VCS provides a feature to fix mistakes till certain point. Git provides feature by using that we can undo modifications that have been made to the local repository.

Suppose user accidentally does some changes to his local repository and now he wants to throw away these changes. In such cases, revert operation plays important role.

Revert uncommitted changes

Let us suppose Jerry accidentally modifies file from his local repository. But he wants to throw away his modification. To handle this situation, we can use git checkout command. We can use this command to revert the contents of a file.

[jerry@CentOS src]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git status -s
M string_operations.c

[jerry@CentOS src]$ git checkout string_operations.c

[jerry@CentOS src]$ git status –s

Even we can use git checkout command to obtain deleted file from local repository. Let us suppose Tom deletes file from the local repository and we want this file back. We can achieve this by using same command.

[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src

[tom@CentOS src]$ ls -1
Makefile
string_operations.c

[tom@CentOS src]$ rm string_operations.c

[tom@CentOS src]$ ls -1
Makefile

[tom@CentOS src]$ git status -s
D string_operations.c

Git is showing letter D before filename. This is indicating that file has been deleted from the local repository.

[tom@CentOS src]$ git checkout string_operations.c

[tom@CentOS src]$ ls -1
Makefile
string_operations.c

[tom@CentOS src]$ git status -s
Note: We can perform all these operations before commit operation.

Remove changes from staging area

We have seen that when we perform add operation; file moves from local repository to the stating area. If user accidently modifies a file and adds it into staging area, but immediately he realizes that he did mistakes. And he wants to revert his changes. We can handle this situation by using git checkout command.

In Git, there is one HEAD pointer that always points to latest commit. If you want to undo a change from staged area, then you can use the git checkout command, but with checkout command you have to provide addition parameter, which is HEAD pointer. The additional commit pointer parameter instructs the git checkout command to reset the working tree and also to remove the staged changes.

Let us suppose Tom modifies a file from his local repository. If we view status of this file, it will show file is modified but not added into staging area.

tom@CentOS src]$ pwd
/home/tom/top_repo/project/src
# Unmodified file

[tom@CentOS src]$ git status -s

# Modify file and view it’s status.
[tom@CentOS src]$ git status -s
M string_operations.c

[tom@CentOS src]$ git add string_operations.c

Git status shows that file is present in staging area, now revert it by using git checkout command and view status of reverted file.

[tom@CentOS src]$ git checkout HEAD -- string_operations.c

[tom@CentOS src]$ git status -s

Move HEAD pointer with git reset

After doing few changes, you may decide to remove these changes. The Git reset command is used to reset or revert some changes. We can perform three different types of reset operations.

Below diagram shows pictorial representation Git reset command.

git Tutorial
git Tutorial

Soft

Each branch has HEAD pointer, which points to the latest commit. If we use Git reset command with --soft option followed by commit ID, then it will reset only HEAD pointer without destroying anything.

.git/refs/heads/master file stores the commit ID of the HEAD pointer. We can verify it by using git log -1 command.

[jerry@CentOS project]$ cat .git/refs/heads/master
577647211ed44fe2ae479427a0668a4f12ed71a1

Now, view latest commit ID, which will match with above commit ID.

[jerry@CentOS project]$ git log -2

Above command will produce the following result.

commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 10:21:20 2013 +0530

Removed executable binary


commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 10:16:25 2013 +0530

Added compiled binary

Let us reset HEAD pointer.

[jerry@CentOS project]$ git reset --soft HEAD~

Now, we just reset HEAD pointer back by one position. Let us check contents of .git/refs/heads/master file.

[jerry@CentOS project]$ cat .git/refs/heads/master
29af9d45947dc044e33d69b9141d8d2dad37cc62

Commit ID from file is changed, now verify it by viewing commit messages.

jerry@CentOS project]$ git log -2

Above command will produce the following result.

commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 10:16:25 2013 +0530

Added compiled binary


commit 94f7b26005f856f1a1b733ad438e97a0cd509c1a
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 10:08:01 2013 +0530

Added Makefile and renamed strings.c to string_operations.c

mixed

Git reset with --mixed option revert changes from the staging area which hasn’t been committed yet. It only revert changes form staging area. The actual changes made to the working copy of the file are unaffected. The default Git reset is equivalent to git reset -- mixed.

For more details, refer section Remove changes from staging area from same chapter.

hard

If you use --hard option with Git reset command, it will clear staging area; it will reset HEAD pointer to latest commit of specific commit ID and delete the local file changes too.

Let us check the commit ID

[jerry@CentOS src]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git log -1

Above command will produce the following result.

commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 10:21:20 2013 +0530

Removed executable binary

Jerry modified file by adding single-line comment at the start of file.

[jerry@CentOS src]$ head -2 string_operations.c
/* This line be removed by git reset operation */
#include <stdio.h>

He verified it by using git status command.

[jerry@CentOS src]$ git status -s
M string_operations.c

Jerry adds modified file to the staging area and verifies it with git status operation.

[jerry@CentOS src]$ git add string_operations.c
[jerry@CentOS src]$ git status

Above command will produce the following result.

# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#
modified: string_operations.c
#

Git status is showing that file is present in the staging area. Now, reset HEAD with --hard option.

[jerry@CentOS src]$ git reset --hard 577647211ed44fe2ae479427a0668a4f12ed71a1

HEAD is now at 5776472 Removed executable binary

Git reset command succeeded, which will revert file from the staging area as well as remove local changes made to the file.

[jerry@CentOS src]$ git status -s

Git status is showing that file is reverted from the staging area.

[jerry@CentOS src]$ head -2 string_operations.c
#include <stdio.h>

Head command also shows that reset operation removed local changes too.



Advertisements
Advertisements