git reset

Views:
 
Category: Education
     
 

Presentation Description

Git reset means to restore repository at a particular version. Let’s move a little bit deeper and take a real time scenario. Suppose we are working on a product and we have delivered 10 version to client of that product. Now client want a new feature.

Comments

Presentation Transcript

slide 1:

LinuxDady.Com What is git reset | Git revert and its types for beginners For more information visit https://linuxdady.com

slide 2:

Git reset | git revert and its type easy explanation for beginners BY DEEPAK KUMAR · PUBLISHED NOVEMBER 4 2019 · UPDATED NOVEMBER 5 2019 If you are a developer or devops engineer then this tutorial is going to be very useful for you. Here we will try to understand concept of git reset or you can say git revert and all related question like what does git revert why we should use it and how many types of git revert. Understand Git Reset Git reset means to restore repository at a particular version. Let’s move a little bit deeper and take a real time scenario. Suppose we are working on a product and we have delivered 10 version to client of that product. Now client want a new feature. Now developers have two options they can start coding from starting for new feature that will wastage of time and resource because already we have written our raw code. No need to write raw code again and again. Now second way is suppose up to 7 version of our raw code is same so we can reset our repository up to version 7 and can start work next. That will be easy and time saving. Type of git reset How to understand Git reset Git tutorial V

slide 3:

Mind it after restore point all commit id will delete. Our data or latest changes will delete or not it depend on type of reset. So there are mainly three type of git reset. Soft reset In case of soft reset all commit id will delete after restore point but all latest changes or you can say data will remain as it is means data will not delete from latter on version from restore point. Let’s take an example suppose we have a repository with name test and we have total 4 version into that repository. As shown below figure. Suppose we want to reset this repository at version 2. When we will reset this repository at version 2 then after version 2 all commit will delete but changes of version 3 and 4 will remain as it is as before. So now my question is when we should use soft reset. Let’s take a scenario suppose our commit id become more. Then more commit id will create confusion. But our all commit are tested and working fine so we can make final commit from all commit that will help to troubleshooting any issue easy. For better understanding go throw from below diagram once. Flow diagram of git revert Now let’s perform practical. In my case I am creating a test repository and I will make 4 commits into it. Then I will reset it from version 4 to version 2.

slide 4:

1 2 3 mkdir test cd test git init Now let’s make our first commit for version 1. 1 2 3 echo hello abc git add . git commit -m “Version 1” similarly make up to version 4 as shown in below figure. Click Read: – What is git and basic terminology for DevOps engineers Click Read: – Explain git workflow for beginners step by step Click Read: – Explain how to add ssh key on GitHub step by step Our all version has been committed properly. So let’s check our all commits by using below commands. 1 git log

slide 5:

The output of above command is showing we have total 4 commit into our repository. So let’s reset it at version 4 to version 2. Copy the commit id where you want to reset. Suppose we want to reset at version 2. So copy the id of version 2. 1 2 3 git reset --soft ba61fddd36e2292c3657074f6468cfc13280497a git log ls

slide 6:

Our repository has been reset now let’s check the status of our files. 1 git status -s Finally soft reset is used when we are not interested to discard latest changes. Only we want to delete commit id then we will prefer soft reset. Hard reset In case of hard reset after restoring point our data and commit id will permanently delete means we cannot access our latest changes or data after hard reset. They will delete permanently form staging area. Again for demo we are resetting our repository from version 3 to version 2.

slide 7:

1 2 3 git reset --hard ba61fddd36e2292c3657074f6468cfc13280497a ls git log Mixed reset In mixed git reset after restore point all commit id will delete. Major advantage of using mixed reset is our data will not delete. But it will discard our data and changes into non staging area. For demo first make commit again up to version 4. Then follow below steps. 1 2 3 4 5 ls git status -s git reset --mixed ba61fddd36e2292c3657074f6468cfc13280497a git log git status -s

slide 8:

Soft vs hard vs mixed reset comparison Let’s have a look between soft vs hard vs mixed reset that will help to understand this topic in a better way. Conclusion — All about git reset In this tutorial I have tried to explain types of git reset and it’s use cases. I have explained this topic practically as well as interview point of view. If still anyone have query write me in comment box. I will try my best to resolve them. Interview question 1. What does git add command Ans. Git add command add latest changes from non-staging area to staging area means it brings data into git reference. 2. What is difference between soft reset and hard reset

slide 9:

Ans. In case of soft reset our data remain in staging area but in case of hard reset our data will delete permanently from staging means we cannot access that data after hard reset never. 3. What is difference between soft reset and mixed reset Ans. This is interviewer favorite question. There are 90 changes to ask this question instead of soft reset and hard reset. So please go throw once from above post for better answer. 4. Interviewer can create a scenario suppose I don’t need repository data or latest change which one reset will you prefer 5. Suppose I have committed some wrong data into repository then to rectify and correct that which once reset will most preferable Ans. Mixed reset. Now my question is why write me in comment box. 6. What are the major advantage of using git reset Ans. We can manage easily all commit id. Please answer the above given question and if I am wrong in any above answer correct me in comment box with right answer.

authorStream Live Help