Git Fix Error

Source: Internet
Author: User
Tags using git

Most people will make mistakes. So each VCS provides a feature that corrects the error until a specific point. GIT provides the functionality to use, we can undo the changes made to the local repository.

Suppose the user accidentally made some changes to his local repository, and now he wants to throw away these changes. In this case, the recovery operation plays an important role.

Redo UNCOMMITTED changes

Let's assume that Jerry accidentally modifies the file from its own local repository. But he wanted to throw away his changes. To handle this situation, we can use the git checkout command. We can use this command to recover the contents of a file.

[Email protected] src]$ PWD/HOME/JERRY/JERRY_REPO/PROJECT/SRC [[email protected] src]$ git status-s M string_operations . c [[email protected] src]$ git checkout string_operations.c

[[email protected] src]$ git status–s

We can even use the git checkout command to delete files from the local library. Let's assume that Tom deleted the file from the local repository and we want this file. We can do this by using the same command.

[Email protected] src]$ PWD/HOME/TOM/TOP_REPO/PROJECT/SRC

[Email protected] src]$ ls-1 Makefile string_operations.c

[Email protected] src]$ RM string_operations.c [[email protected] src]$ ls-1 Makefile

[[email protected] src]$ git status-s D string_operations.c

Git is the letter D before the file name is displayed. This indicates that the file has been deleted from the local repository.

[[email protected] src]$ git checkout string_operations.c

[Email protected] src]$ ls-1 Makefile string_operations.c

[[email protected] src]$ git status-s

Note: We can perform all these operations before committing the operation.

Delete Staging Area changes

We have seen that when we perform addition operations, the files move from the local repository to the parameter area. If the user accidentally modifies a file and adds it to the staging area, he immediately realizes that he made a mistake. He wished to restore his change. We can handle this case by using the git checkout command.

In Git is a head pointer that always points to the latest commit. If you want to undo the change staging area, you can use the git checkout command, but the checkout command must provide additional parameter head pointers. The extra commit pointer parameter indicates the git checkout command, resetting the work tree, and being able to remove the staged phase.

Let's assume that Tom modifies a file from his local repository. What if we look at this file?? Status, it will show that the file has been modified but not added to the staging area.

[Email protected] src]$ PWD/HOME/TOM/TOP_REPO/PROJECT/SRC # unmodified file

[email protected] src]$ git status-s # Modify file and view it's status.

[[email protected] src]$ git status-s M string_operations.c

[[email protected] src]$ git add string_operations.c

The status of git shows that the file is in staging area, and now it recovers files using git checkout command and view state recovery.

[[email protected] src]$ git checkout head-string_operations.c

[[email protected] src]$ git status-s

Reset the head pointer to git

After making some changes, you may decide to delete these changes. The Git Reset command is used to reset or restore some changes. We can perform three different kinds of reset operations.

The display graph represents the git reset command.


SOFT

Each branch has a head pointer pointing to the latest commit. If we use the git–soft Reset command option, then commit the ID and then reset only the head pointer, without destroying anything.

The head pointer to the commit ID of the. git/refs/heads/master file Store. We can verify it by using the git log-1 command.

[Email protected] project]$ cat. Git/refs/heads/master 577647211ED44FE2AE479427A0668A4F12ED71A1

Now look at the latest submitted ID, which will match the commit ID above.

[[email protected] project]$ git log-2

The above command produces the following results.

Commit 577647211ED44FE2AE479427A0668A4F12ED71A1 author:tom Cat <[email protected]> date:wed Sep 11 10:21:20 2013 + 0530 removed Executable binary commit 29af9d45947dc044e33d69b9141d8d2dad37cc62 Author:jerry Mouse <[email protected]> date:wed Sep 10:16:25 +0530 Added compiled binary

Let's reset the head pointer.

[[email protected] project]$ git reset–soft head~

Now we only reset the head pointer back to one position. Let's check the content. git/refs/heads/master file.

[email protected] project]$ cat. Git/refs/heads/master 29af9d45947dc044e33d69b9141d8d2dad37cc62

The commit ID change from the file is now verified by viewing the submitted information.

[email protected] project]$ git log-2

The above command produces the following results.

Commit 29af9d45947dc044e33d69b9141d8d2dad37cc62 Author:jerry Mouse <[email protected]> date:wed Sep 11 10:16:25 20 +0530 Added compiled binary commit 94f7b26005f856f1a1b733ad438e97a0cd509c1a author:jerry Mouse <[email protected]& Gt date:wed Sep 10:08:01 +0530 Added Makefile and renamed STRINGS.C to STRING_OPERATIONS.C



Mixed

The Reset-mixed option for Git has not yet been submitted for restore changes from the staging area. It restores only changes to form staging area. The actual changes made to the working copy of the file are not affected. The default git reset is equivalent to the git reset-mixed.

For more details, see the section delete the same chapter temporary area changes.

Hard

If you use the –hard option to reset the command with git, it clears the staging area and resets the head pointer to the specific commit ID of the last commit, and also removes changes to the local file.

Let's check the ID of the submission

[Email protected] src]$ PWD/HOME/JERRY/JERRY_REPO/PROJECT/SRC

[[email protected] src]$ git log-1

The above command produces the following results.

Commit 577647211ED44FE2AE479427A0668A4F12ED71A1 author:tom Cat <[email protected]> date:wed Sep 11 10:21:20 2013 + 0530 removed executable binary

Jerry modified the file, at the beginning of the file, adding a single line comment.

[[email protected] src]$ head-2 STRING_OPERATIONS.C/* This line is removed by git reset operation */#include <stdio. H>

He uses the git status command to authenticate.

[[email protected] src]$ git status-s M string_operations.c

Jerry modified the file to add to the staging area and verify that it is running with the GIT state.

[[email protected] src]$ git add string_operations.c [[email protected] src]$ git status

The above command produces the following results.

# on Branch Master # changes to being committed: # (use "git reset HEAD <file> ..." to Unstage) # # Modified:string_opera TIONS.C #

The status of git shows that the file is in the staging area. Now reset the head with the –hard option.

[[email protected] src]$ git reset–hard 577647211ed44fe2ae479427a0668a4f12ed71a1 HEAD is now at 5776472 removed EXECUTABL E binary

The Git Reset command succeeds, which restores files from the staging area and deletes local changes to the file.

[[email protected] src]$ git status-s

Git status Display, file recovery from the staging area.

[Email protected] src]$ head-2 string_operations.c #include <stdio.h>

The Head command also shows that the reset operation removes local changes.

PS: If you want to communicate with the industry technology Daniel, please add QQ Group (521249302) or pay attention to the public number (askharries), thank you!

Git Fix Error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.