Fixed a bug in Git
Being human, everyone makes mistakes. So each VCS provides a feature to fix errors at some point. Git provides a feature that we can use to undo the modifications made on the local repository.
Suppose the user accidentally made a few changes to the local repository and then wanted to undo these changes. In such cases, revert operation plays an important role.
Return uncommitted changes
Suppose Jerry accidentally edits a file from his local repository. But he wants to undo this custom. To resolve this situation, we use the git checkout command. We can use this command to return 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
Further, we can use the git checkout command to obtain deleted files from the local repository. Suppose Tom deletes a file from the local repository and he wants to use this file later. We can do this by using the 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 indicating the D before the file name. This indicates that the 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 the organization area
We saw when we performed an extra operation, the files moved from the local repository to the organization area. If a user accidentally edits a file and adds it to the organizer area, he can return the changes, using the git checkout command.
In Git, there is a HEAD pointer that always points to the latest commit. If you want to undo a change from the organizer area, then you can use the git checkout command, but with this command, you must provide an additional parameter, ie, the HEAD pointer. This additional commit pointer parameter instructs the git checkout command to reset the working tree and also to remove organized changes.
Suppose Tom modifies a file from the local repository. If we look at the status of this file, it will indicate that the file is edited but not added in the organization 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 indicates that the file is present in the organizer area, now returns it using the git command command and observes the status of the returned file.
[tom @ CentOS src] $ git checkout HEAD - string_operations.c
[tom @ CentOS src] $ git status -s
Move the HEAD HEAD with git reset
After making some changes, you can decide to unload these changes. Git reset command is used to reset or return changes. We can perform three different types of reset operations.
The diagram below shows the process of git reset command.
Soft
Each branch has a HEAD pointer, which points to the latest commit. If we use the git reset command with the --soft option followed by a commit ID, then it will only reset the HEAD pointer without destroying anything.
.Git / refs / heads / master files keep the commit ID of the HEAD pointer. We can verify it using the git log-1 command.
[jerry @ CentOS project] $ cat .git / refs / heads / master
577647211ed44fe2ae479427a0668a4f12ed71a1
Now, look at the latest commit IDs, which will connect to the commit ID above.
[jerry @ CentOS project] $ git log -2
The above command will produce the following result:
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat
Date: Wed Sep 11 10:21:20 2013 +0530
Removed binary executable
commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Jerry Mouse
Date: Wed Sep 11 10:16:25 2013 +0530
Added compiled binary
Let us reset the HEAD pointer.
[jerry @ CentOS project] $ git reset --soft HEAD ~
Now we have just reset the HEAD pointer again after a location. We check the contents of the .git / refs / heads / master file.
[jerry @ CentOS project] $ cat .git / refs / heads / master
29af9d45947dc044e33d69b9141d8d2dad37cc62
The commit ID from the file is changed, now verify it by checking the commit message.
jerry @ CentOS project] $ git log -2
The above command will produce the following result:
commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Jerry Mouse
Date: Wed Sep 11 10:16:25 2013 +0530
Added compiled binary
commit 94f7b26005f856f1a1b733ad438e97a0cd509c1a
Author: Jerry Mouse
Date: Wed Sep 11 10:08:01 2013 +0530
Added Makefile and renamed strings.c to string_operations.c
Optional mixed
The git reset command with the --mixed option returns changes from the organization area that have not yet been committed. It only returns changes from the organization area. The actual changes to a working copy of a file are not affected. The default git reset command is equivalent to git reset --mixed.
Hard option
If you use the --hard option with the git reset command, it will clear the organization area; it will reset the HEAD pointer to the latest commits of a specific commit ID and delete the local file changes.
We check the commit ID.
[jerry @ CentOS src] $ pwd
/ home / jerry / jerry_repo / project / src
[jerry @ CentOS src] $ git log -1
The above command will produce the following result:
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat
Date: Wed Sep 11 10:21:20 2013 +0530
Removed binary executable
Jerry edits a file by adding a single line comment at the beginning of the file.
[jerry @ CentOS src] $ head -2 string_operations.c
/ * Dòng này được gỡ bỏ bởi git reset operation * /
#include
He verifies it using the git status command.
[jerry @ CentOS src] $ git status -s
M string_operations.c
Jerry adds this edited file to the organizer area and verifies it with the git status command.
[jerry @ CentOS src] $ git add string_operations.c
[jerry @ CentOS src] $ git status
The above command will produce the following result:
# On the master branch
# Changes to be committed:
# (use "git reset HEAD ." to unstage)
#
#
modified: string_operations.c
#
Git status is indicating that the file is present in the organizer area. Now, reset HEAD with the --hard option.
[jerry @ CentOS src] $ git reset --hard 577647211ed44fe2ae479427a0668a4f12ed71a1
HEAD is now at 5776472 Removed executable binary
The git reset command executes successfully, which will return the file from the organizer area as well as remove any local changes made to the file.
[jerry @ CentOS src] $ git status -s
Git status is indicating that the file has been removed from the organization area.
[jerry @ CentOS src] $ head -2 string_operations.c
#include
The head command also indicates that the reset operation has removed the local changes.
According to Tutorialspoint
Previous post: Delete operation in Git
Next lesson: Tag operation in Git
You should read it
- Instructions on how to reset Windows 10 extremely fast and simple
- Steps to reset Apple TV, How to factory reset Apple TV
- What is mixed content? And why does Chrome block it?
- What is Mixed Reality (MR)? How is MR used?
- How to create a yellow circle around the mouse cursor on Windows
- Steps to reset the computer on Windows 11 to fix annoying errors
- How to find and delete broken bookmarks in Firefox
- Cursor this in C ++
May be interested
- Microsoft has fixed the critical bugs of Windows 10 21H1many users who installed kb5001391 for windows 10 21h1 encountered a critical error and microsoft promptly fixed it.
- Fixed IDM does not catch links on Firefox and Chromefix idm error does not catch links on firefox and chrome - currently, internet download manager (idm) - a tool used to download the most used in the world. the regular update of the new version makes idm sometimes fail to catch links
- How to insert a fixed image into a cell in excelthe following video article tipsmake will guide you how to insert fixed images into a cell in excel extremely detailed
- How to fix missing or missing Msvcp110.dll filemsvcp110.dll error occurs due to situations that lead to the removal or crash of msvcp110 dll file. in some cases, these dll errors are caused by registry problems, viruses or malware, even hardware errors.
- AAG cable breakdown will be fixed on October 1according to the operating unit of the international marine optical cable aag (asia gateway pacific), fiber optic breakdown will be fixed earlier than originally planned for 2 days, due to favorable weather and the hong kong government allows cable welding ship enters the territorial sea early.
- Viber launches Viber Out, allowing calls to any fixed and mobile numbertoday, the free messaging and calling service on viber smartphones introduced a new feature called viber out. this feature allows users to make calls from viber to any fixed or mobile phone number not registered to use viber.
- Buy aluminum chairs for families which is the best?in the family, you will often have to repair or install in high positions, choosing a family an aluminum ladder to clean the house immediately is a great suggestion for you. aluminum chair ladder is a fixed ladder, can not adjust the size or height, so it is suitable for fixed ladder work in the family such as cleaning cabinets, hanging paintings, hanging curtains, cleaning doors, offering flowers fruit…. aluminum ladders on the market have many different types and types.
- Apple patched a serious flaw in Mac OS Xapple has just fixed 10 vulnerabilities in the mac os x operating system that has been rated seriously by security vendors.
- iOS 14.4 and mac OS 11.1 fix a lot of errors on iPhone and Macbookon january 27, apple released a new ios 14.4 with many bugs fixed, notably the new-generation iphone ending noise when shooting in hdr mode in low-light situations.
- Finding the cause of the Fn key being reversed (Fixed)are you experiencing a situation where the fn key is reversed and don't know what to do? tipsmake will tell you the cause and how to fix this error!