Reset,Revert and Delete files in Local Repository
In this article,we will see Reset,Revert files in Local Repository.
Implementation Steps
- Delete Snapshot from Staging Area.
- Delete file from both areas (Staging Area & Work Space).
- Remove specific file from workspace.
- Delete All untracked files.
Git Reset
Delete Snapshot from Staging Area
Create a new file “ktexperts-reset”
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts-reset ktexperts is a useful website [root@ip-172-31-42-20 mumbaigit]# cat ktexperts-reset ktexperts is a useful website |
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-reset nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# git add . [root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-reset |
Delete the snapshot from staging area by using reset command
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git reset . [root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-reset nothing added to commit but untracked files present (use "git add" to track) |
Git Reset
Delete file from both areas (Staging Area & Work Space)
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-reset nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
1 |
[root@ip-172-31-42-20 mumbaigit]# git add . |
To see the status of git
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-reset |
Delete file from both areas (Staging Area & Work Space) by using reset –hard command.
1 2 3 4 5 |
[root@ip-172-31-42-20 mumbaigit]# git reset --hard HEAD is now at 0c76f5b My second task is completed [root@ip-172-31-42-20 mumbaigit]# git status On branch master nothing to commit, working tree clean |
To see the list of files
Here,we unable to see the file “ktexperts-reset” because we have done the reset–hard recently.
If you do the reset–hard,the file will be deleted from staging area and work space.
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file1.txt file2.txt file3.txt ktexperts.java kt-stash |
To see the current branch
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Git Revert
Revert to a existing commit
Create a new file “ktexperts-Revert”
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts-Revert Hello ktexperts! [root@ip-172-31-42-20 mumbaigit]# cat ktexperts-Revert Hello ktexperts! |
Add and commit the file “ktexperts-revert”
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-Revert nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# git add . [root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-Revert |
Commit the file into Local Repository
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "This is my latest code" [master 7ad3592] This is my latest code 1 file changed, 1 insertion(+) create mode 100644 ktexperts-Revert |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit 7ad3592cd6b637376d819d52b4cc3326af813c34 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Thu Dec 19 11:40:58 2019 +0000 This is my latest code |
To see the all commits in a summarized manner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 7ad3592 (HEAD -> master) This is my latest code 0c76f5b My second task is completed b86c07c My first task is finished 57b2258 commited kt-stash empty file ec49bcf (origin/master) commited ktexperts-1 from master 1ed4eda ktexperts has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
To do the revert changes by using revert command
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git revert 7ad3592 [master 2f4cf13] Revert "please ignore" 1 file changed, 1 deletion(-) delete mode 100644 ktexperts-Revert |
To see the all commits in a summarized manner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 2f4cf13 (HEAD -> master) Revert "please ignore" 7ad3592 This is my latest code 0c76f5b My second task is completed b86c07c My first task is finished 57b2258 commited kt-stash empty file ec49bcf (origin/master) commited ktexperts-1 from master 1ed4eda ktexperts has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
To see the list of files
Here,we can’t see the file “ktexperts-revert”.
If you do the revert of any file,that will delete the file from staging area and workspace.
Revert will create a new commit ID.
You need not to add and commit again. (Automatically new commit ID will be generated).
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file1.txt file2.txt file3.txt ktexperts.java kt-stash |
Git rm
Remove specific file from workspace
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git rm file1.txt rm 'file1.txt' |
Note
The file will be deleted from staging area as well as workspace.
The commit ID won’t get deleted.
To see the list of files
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file2.txt file3.txt ktexperts.java kt-stash |
To see the status of git
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Your branch is ahead of 'origin/master' by 5 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: file1.txt |
Commit the deleted file into Local Repository
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "file1.txt deleted" [master 57c09de] file2.txt deleted 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 file2.txt [root@ip-172-31-42-20 mumbaigit]# git status On branch master Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean |
Git Clean
Delete All untracked files
Create a new empty files
By using touch command
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# touch kt@{1..5} [root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file2.txt file3.txt ktexperts.java kt-stash kt@1 kt@2 kt@3 kt@4 kt@5 |
To see the status of git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) kt@1 kt@2 kt@3 kt@4 kt@5 nothing added to commit but untracked files present (use "git add" to track) |
To show the confirmation message to delete untracked files
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git clean -n Would remove kt@1 Would remove kt@2 Would remove kt@3 Would remove kt@4 Would remove kt@5 |
To see the status of git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) kt@1 kt@2 kt@3 kt@4 kt@5 nothing added to commit but untracked files present (use "git add" to track) |
To Delete all untracked files forcefully by using clean -f command.
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git clean -f Removing kt@1 Removing kt@2 Removing kt@3 Removing kt@4 Removing kt@5 |
To see the status of git
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean |
To see the list of files
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file2.txt file3.txt ktexperts.java kt-stash |
Tag
It is a meaning full name.
Whenever you commit, you will get one commit ID.
This commit ID contains 41 alphanumeric characters, so we can’t remember commit ID.
we can’t remember commit ID for that we use tag to pick the data of commit ID.
For easier understanding we give tag for the commit ID.
Create a new file “ktexperts-tag”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# vi ktexperts-tag [root@ip-172-31-42-20 mumbaigit]# cat ktexperts-tag Ktexperts is a knowledge sharing platform |
Add and commit the file “ktexperts-tag”
Adding file to Staging Area
1 |
[root@ip-172-31-42-20 mumbaigit]# git add . |
Commit the file into Local Repository
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "ktexperts-tag" [master (root-commit) 1d0f9fe] ktexperts-tag 1 file changed, 1 insertion(+) create mode 100644 ktexperts-tag |
To see the list of commits in a summarized manner
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 1d0f9fe (HEAD -> master) ktexperts-tag |
Assign Tag to Particular Commit ID
1 |
[root@ip-172-31-42-20 mumbaigit]# git tag -a kt_tag -m My_important_commit 1d0f9fe |
To see the list of commits in a summarized manner
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 1d0f9fe (HEAD -> master, tag: kt_tag) ktexperts-tag |
To show the data of commit ID by using tag
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[root@ip-172-31-42-20 mumbaigit]# git show kt_tag tag kt_tag Tagger: Ram <ram@gmail.com> Date: Mon Dec 23 11:28:12 2019 +0000 My_important_commit commit 1d0f9fee3b84e1b23138ac14fec15fe8e419c35e (HEAD -> master, tag: kt_tag) Author: Ram <ram@gmail.com> Date: Mon Dec 23 11:20:24 2019 +0000 ktexperts-tag diff --git a/ktexperts-tag b/ktexperts-tag new file mode 100644 index 0000000..6c9db01 --- /dev/null +++ b/ktexperts-tag @@ -0,0 +1 @@ +Ktexperts is a knowledge sharing platform |
To remove the tag from Commit ID
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git tag -d kt_tag Deleted tag 'kt_tag' (was 5639d64) |
To see the list of commits in a summarized manner
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 1d0f9fe (HEAD -> master) ktexperts-tag |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Priya
Best learning platform for devops students
santhi
Nice article, Easy to understand and learn quickly and please try to release all the articles on DevOps