/ 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.
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
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.
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
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.
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