Full transcript
Introduction
0:00Hello and welcome. I am Raghav and this
0:03session is going to be very interesting
0:05because we are going to go beyond the
0:07basics in Git. So we have already
0:09learned what is Git, how do we use it,
0:12how do we start, how do we install Git,
0:14how do we uh start git on a project
0:17folder and then how do we run all the
0:19git commands, git push, git pull, git
0:21commit, git stage and the basic
0:23commands. How do we connect our local
0:25repository to the remote repository and
0:27how do we make connections between those
0:29these two repositories and all these
0:31basic things that we have done. Now this
0:33session is going to go beyond these
0:35basics and this is all going to be real
0:38scenario- based. So this is going to be
0:40very interesting. So we will understand
0:42and see how do we handle scenarios.
0:45Let's say uh two developers are working
0:47on the same file and at the same part of
0:50the file and when they merge their
0:52changes what happens? What if there is a
0:55conflict? How do get resolves merge
0:58conflicts and what will happen after
0:59that and how do we resolve these merge
1:02conflicts? What happens if you
1:04accidentally delete a file and then you
1:06have committed and pushed your changes
1:07and everything is now gone. How can you
1:10recover those deleted files or your
1:12deleted work? Can you uh commit only a
1:16part of the file that you are working
1:18on? Let's say you have made several
1:19changes in a file but now you want to
1:21only pick some particular change and
1:23want to commit it. What if you want to
1:26take a particular commit from a branch
1:29and then push only that change to the
1:32main branch and nothing else. What if
1:34you have you are working on some branch
1:36and you suddenly got a production issue
1:38and now you have to switch back to the
1:41main branch and work on it and then
1:42switch back to your feature branch. Then
1:45where is your work? How do you get back
1:46your work and all these kind of real
1:49world scenarios. So this is going to be
1:50very interesting and this is going to be
1:52a complete hands-on session along with
1:54quizzes. So please do hands-on with me
1:56and whenever you face any issues please
1:58let me know in the comment section or if
2:00you have any general issue as well I
2:02will reply to all your comments. Okay.
Getting Started - Topics
2:04So let's get started. Let us very
2:06quickly see the topics we are going to
2:08do. So we are going to do a demo on
2:11gerge. How can we how do we do git merge
2:15and how do git merge helps us? How can
2:18get help to automatically resolve
2:20conflicts and when will git tell us that
2:22you have to manually resolve the merge
2:24conflicts? How can we commit only a part
2:27of a file? How do we recover
2:29accidentally deleted files? And here we
2:31will also learn how can we recover any
2:33accidentally deleted work on any of the
2:36files and how to copy a specific commit
2:40from one branch to another. So let's say
2:42you have made multiple commits but you
2:44want to pick just a specific commit then
2:47how can we identify who last modified
2:50any line of a file. So sometimes there
2:53is some issue happening after a after a
2:56release something breaks and now you
2:58want to check what exactly happened who
3:00modified what was the part of the file
3:02that got modified what was changed. So
3:04how do we find out that how can we
3:06discard local uncommitted changes in one
3:08or more files? How can we move back to
3:12an earlier point in history using get
3:14reset and how do we cover recover lost
3:17commits. So these are the scenarios we
3:20are going to understand and we will do
3:23practical hands-on sessions. So let's
Fast-Forward Merges (No Conflict)
3:25get started and let us see the very
3:27first demo.
3:30Here we will learn how we can do
3:32fastforward merge or how git actually
3:35manages these merge changes when there
3:38is no conflict. So let's start by
3:40creating a new folder and initialize it
3:44with git using git init command. So I
3:47will go here on my system and let's say
3:52I will go to
3:58I will go to my
4:01D drive and
4:05I will create a new project here or new
4:08folder here. So you can just say
4:12I will say
4:17my projects
4:20and inside this I will create another
4:22folder. You can go to new folder or you
4:25can press control + shift + n key on
4:28your keyboard to create a new folder.
4:30And I will call this as
4:33get
4:37demo project.
4:40So this is the project folder that we
4:44are going to use. So basically when I
4:45say project folder, it is just a folder
4:47where you can imagine that this is your
4:49project folder where you will be having
4:51all your work and this will be managed
4:54by git. And to make it as a git folder
4:57or have git track this folder, I will
5:01open this folder in terminal or command
5:03prompt. You can do a right click and
5:05open in terminal or you can just copy
5:09the project folder location from here.
5:13Just copy this and let's go to our
5:17command prompt. You can use your
5:19terminal, your git bash. If you are on
5:21Mac, you will go to uh you will go to
5:25terminal. Let me just show you very
5:27quickly. Uh I will
5:32go here. This is my Mac and I will open
5:35terminal here. So this is where you will
5:37work. If you are on Mac or uh Linux
5:40also, you can work on terminal and on
5:42Windows you can work either on command
5:43prompt or git bash. So I hope you
5:46remember in the earlier sessions we have
5:48seen that once you install git you also
5:52get git bash which is a terminal for
5:55git. You can do a right click and you
5:58can
6:00say
6:02git bash here that will also work or let
6:05me just go on
6:07go with the common normal command
6:11prompt.
6:14Okay. So I am on my command line. Now I
6:16have to change first change the location
6:19to the location of my folder. So because
6:23command prompt is on C and our folder in
6:27my case it's on D. So first I will
6:29change the drive by saying D colon hit
6:33enter. And now I will say CD to change
6:36directory and space and paste. So on
6:40command prompt you can just do a right
6:42click to paste. That should work. And
6:45this is my folder. And now I am on my
6:49folder. Now just to make it as a git
6:52folder or make git track this I will say
6:57get init.
7:00So I will just say here
7:05get
7:07init.
7:08And as soon as I will hit enter, you
7:10will see that it will generate a hidden
7:14folder. Git inside this folder. And that
7:17generally means that now this is being
7:19tracked by git. And just in case you do
7:22not see this hidden folder, you can
7:24always go to view and say show
7:28uh hidden items. In case it is not
7:30checked, you can check that and you
7:32should see this. So this is just
7:33optional just to tell you that you will
7:35have this generated in the folder. All
7:39right. Now let's create a feature branch
7:42and check out the feature branch. So
7:47I will first just say get status
7:51and everything is clean nothing to
7:54commit. Now I will say get
7:59branch to check. So as of now there is
8:02nothing here. So let us just first add
8:06something on the main branch or then we
8:09will switch to a new branch. So let's
8:11create a new file.
8:15I will create a text file and call it as
8:19demo 1.txt txt
8:22and I will just say this is a demo file
8:28and
8:29save and close this file and if I now uh
8:34let me also in
8:36use the large thumbnails. Yeah, this is
8:40good. All right, I can now also
8:45say
8:47get status
8:49and yes we have unttracked changes. So
8:53to stage these changes I will say get
8:55add and I can either either just give
8:57the file name demo 1.txt. So I just say
9:00de and press tab to autocomplete or just
9:03say get that get add dot to stage all
9:08the changes. And if I say get status now
9:12it is ready to be committed. I will say
9:13get commit- m and give a commit message
9:17added demo.txt.
9:23All right. And this is done. And also if
9:25I want to check my commit history I will
9:27say get log. And you can see we have got
9:30a single commit. This is the commit ID
9:34and this is the author, date and details
9:38of our commit message. Or I can also say
9:41get log - 1 line to see a concise
9:46history and it will now show a concise
9:51history in a single lines. Now this is
9:54very very useful this command because as
9:56of now we just have a single commit but
9:58in general we will have lots of commits
10:01and then just to see each commit in a
10:04single line this will really help. All
10:06right and if I say get branch so you can
10:11see I am on my master branch. Okay. So
10:15now I will switch to
10:20I will switch to
10:23a new branch. I will call it as feature
10:25one. Now
10:28either you can just say get
10:35branch
10:36feature one and then the branch will be
10:39created. And then to check out or switch
10:41to the branch you will say get checkout
10:43feature one or to create and switch to
10:45the branch in a single command we can
10:47say get checkout - b feature one or we
10:51can also say get branch c feature one.
10:55This is the branch name this can be
10:56anything whatever you want to keep. So
10:58both commands will work. I will say get
11:01checkout - b
11:05feature one.
11:07Okay. And this is done. And you can see
11:10switch to the branch. If I say I'm
11:13pressing the up arrow to go to the last
11:15commands. If I run get branch, you can
11:17now see now I am on the feature branch.
11:21All right. Now let us create a new file
11:25here. So now you can see our local
11:28directory, our local repository is now
11:30on the feature branch. So whatever I do
11:33here now will go in the feature branch
11:35and not in the master branch unless and
11:38until we merge the feature branch to the
11:40master branch. So I will create a new
11:43file here
11:46merge demo.txt
11:49you can call it anything anything.
11:53And I'll add something here. This file
11:58is created
12:01in
12:03feature one branch
12:07and
12:09close it and of course we will add we
12:14have added some comments uh we have
12:16added some content and we will stage and
12:18commit the changes. So I will say get
12:24add uh get let me just say get status
12:26just to check we have changes to be
12:29added. Yes. Now get add dot and get
12:36commit m and a message added
12:41merge demo.txt.
12:43Now see these commit messages are very
12:46important. always add meaningful
12:48messages because this will be shown in
12:50the commit history in the log and it
12:52will help us to identify what exactly
12:54you did in this particular commit. Okay.
12:58So I will say enter and if I now go and
13:03check get status everything is done.
13:06Okay. Now we have committed the changes.
13:10Let us now switch back to our main
13:13branch. So if it is main you will say
13:15get checkout main. If it is master you
13:17will say get checkout master. So in my
13:19case the branch is master. So I will say
13:22get checkout
13:24master. Now also observe here as soon as
13:28I will hit enter you will see that this
13:32file will no longer be there because
13:34once we have switched to the master
13:36branch this file is not there. So let's
13:39see. I will hit enter
13:41and you can see the file is now gone
13:43from here. Okay, now just to confirm I'm
13:48on the master branch. I will
13:51just run the command get branch. And
13:55yes, I'm on the master branch here.
13:58Okay, now
14:00I will say get merge feature one branch.
14:04So we will merge the feature branch to
14:06the master branch. And make sure to f to
14:10merge the feature branch, you should be
14:12first checked out to master branch. You
14:14should have switched to the master
14:16branch. And now I will say get
14:19merge
14:22feature one. Whatever is your branch
14:24name, you can do that. You can put that.
14:26And as soon as I will hit enter, you
14:28will observe here. We will get that file
14:30in the master branch as well. So I will
14:33hit enter. And you can see merge
14:35demo.txt has come here. And this is a
14:41it if you observe the message it says
14:45fast forward.
14:47Okay. So because there were no conflicts
14:50the file itself did not uh was not
14:54already there in the master branch. So
14:56there was no conflict no issues. It just
14:59merged without any problem. So this was
15:01a fastforward merge.
15:05Okay. Also now uh if you run the get log
15:10command or I will say get log -
15:15one line
15:17and hit enter. So you can see here this
15:21was the first commit and this is the
15:24commit that we did in feature branch and
15:26it has now come to the master branch as
15:28well. Okay. And you can see these are
15:30the commit messages. So this is very
15:32important as I said to put meaningful
15:35commit messages.
15:37Okay. And head is our current commit or
15:41the last commit. So our head is always
15:43on our last commit.
15:46We will understand this concept in a
15:48moment. But for now you can now see here
15:52we have done merging without any
15:54conflict and this was a fast forward
15:56merge. Also if you run this command get
15:59log one line and say hyphen
16:02graph. So let's see what happens.
16:05Uh
16:08yeah so you will see here everything is
16:12fine now. See this command
16:15showing the graph will be very useful
16:19when you have lots of commits and lots
16:22of history. So you can actually see some
16:24graph lines here. I will show you this
16:26again when we have some uh good commit
16:29history. As of now, we just have two
16:31commits. So, it is not much to show
16:33here. But then you will see this as
16:36well. So, I can also say hyphen - all.
16:40And yeah, still because we don't have a
16:43lot of commits, we are not seeing the
16:45graph lines here. Okay. Now, you can now
16:49see this was a commit without any
16:51issues. Fast forward commit. Fast
16:53forward merge. no conflict. Now let us
Automatic Merge (No Conflict)
16:56see another demo where Git will do a
17:00automatic merge which is also called as
17:02the ORT strategy. And here also there is
17:05no conflict. So it will do it
17:07automatically without we have to
17:09manually checking what is the conflict
17:11and resolve it. So let's imagine two
17:14branches modify different parts of the
17:17project. Now because they have modified
17:21different parts when we combine them
17:23when we merge them there should not be
17:25any conflict and git should be able to
17:28automatically combine the changes and
17:30create a merge commit. Okay. So let's
17:34create a feature branch and check out to
17:37the branch. I will say
17:40let me clear this.
17:44I will say get checkout
17:48- B
17:50feature 2. Now let me call it as feature
17:532. We have already used feature one.
17:56Okay. And if I say get branch, I am on
18:00my feature 2 branch.
18:03Now let us create a new file and commit
18:06it. I will so now our working directory
18:09is on feature 2 branch. I will just
18:11create a new
18:15file here. I will call it as
18:20login.txt
18:22and open and say this is created in
18:28feature 2 branch
18:30and
18:32save it.
18:35I will say get status
18:38and
18:40I will say get add to add the changes or
18:44stage the changes and get commit to
18:47commit the changes.
18:53Everything is done. And if I again hit
18:56the up arrow on my keyboard to go to the
18:58earlier command get status. Everything
19:00is fine now. Okay. Now we will switch
19:04back to our main or the master branch. I
19:07will say get
19:11checkout
19:14master
19:16and just to confirm
19:19I will run get branch. I'm on the master
19:22branch and here
19:25I'm going to create another file a new
19:27file and commit it. So let's say I'm
19:30going to create a file here
19:33text file.
19:35I'll call it as
19:37search.txt
19:40and add something created in
19:45master branch.
19:51Okay. And
19:54I will say get status
19:58get add dot
20:04and get commit
20:08added
20:10search.txt.
20:15Okay. And
20:19everything is fine here. We have a clean
20:22working tree.
20:24Okay. Now let us merge the feature
20:27branch. So I will say I'm on the master
20:30branch. So I will say get merge
20:34feature and our branch name is feature
20:362. So let's do this. And now you can see
20:41here I'm getting this option to enter a
20:45commit message to explain why this merge
20:49is necessary especially if it merges and
20:52updated upstream into a topic branch.
20:55Okay. So here whatever you want to type
20:59or add you can add here. So
21:05press I on your keyboard. So you will go
21:07in the insert mode and then you can type
21:09your command. I will say
21:13merging branch feature 2. And of course
21:17you should give some more details why
21:19you are merging it. What did you do in
21:20real world you can add meaningful
21:22messages here. And after that you can
21:24press escape and press colon and
21:29say wq to write and quit and hit enter.
21:36And we are now here and merging has
21:39done. Merging is done. And now
21:44here here
21:47you can see login.txt that we created in
21:51our feature branch has also come here.
21:54Okay. So it has come here and now if you
21:59see here you will find the message merge
22:04made by ORT strategy. Okay. So this
22:09means it is not a fast forward merge. So
22:11fast forward merge is very clean merge
22:13because there is no issues no conflicts
22:16you are using the parts you are changing
22:20are completely different. Let's say in
22:22the earlier demo we have created a file
22:25which was no long which was not at all
22:27there and after coming back to the
22:29master branch we did not make any change
22:31to the master branch we just did a
22:33merge. So it was very clean, no
22:35conflict. But this time after coming
22:37back to the master branch, we again did
22:39some changes. And now kit will check and
22:42decide if it can do the merging without
22:46any manual intervention and it decided
22:48yes because there were two different
22:50files that we were working on in two
22:52different branches. Therefore, it did
22:53the merge using the ORT strategy. Okay.
22:57So you can see this and of course if I
23:02say so this is what happened here and
23:05this ORT stands for ostensibly recursive
23:10twin and I'm not going to do very deep
23:12into what it is and why why it is used
23:15but in simple words when you try to
23:19merge a branch to the when you try to
23:21merge two branches and you have changes
23:24made in both the branches git will check
23:26and decide side that if there was any
23:29change to a to the same part or the same
23:33reference point by two branches. If not
23:36if the changes that were done in both
23:38the branches are completely separate
23:40from each other then it will check and
23:42then finally do a automatic merge
23:44otherwise it will create a conflict and
23:46ask us to resolve the conflict manually.
23:48Okay. So that is the strategy. Okay. And
23:53of course if I run the command
23:55get log.
23:58Let me see where is that command. Yeah,
24:01if I run this command now you can see
24:04there is some graphs you can check here.
24:07Okay. So this was here we
24:10added this in a feature branch then we
24:15added this
24:18then we added this in feature two
24:20branch. So you can see these are the you
24:23can see here we have these branches and
24:25then we are merging these branches. So
24:28you can see it is going from master we
24:32are creating branches and then merging
24:33back to master. And now this is what we
24:36have. We have all these commits and we
24:40have done this merge as well.
24:44All right. So this is done. Now let us
Merge Conflicts
24:46see a demo where both branches modify
24:50the same line in the same file. Okay. So
24:53this is where we will get a merge
24:54conflict. So let's create a file
24:58in the main or the master branch. So I'm
25:01on the main branch.
25:03I will create a new file.
25:11I'll call it
25:13employee.txt
25:18and I will add some data here. Okay, I
25:22will call I will say employee management
25:24system and commit.
25:30Okay. So see what we going to do is we
25:32are going to change the same line of the
25:35same file in different branches and then
25:36we will see what will get do when we try
25:38to merge. So save it and commit. I will
25:44say get status
25:47and yes we have uncommitted changes. I
25:50will first stage the changes then I will
25:52say get commit
25:55and
25:56hyphen m the message. I will say created
26:01employee.txt
26:05and if I run get status
26:09everything is clean.
26:12Okay. Now let's create and switch to a
26:16new branch. So in case you want you can
26:18use any of your existing branches if you
26:20don't want to create a lot of branches.
26:22So let me just use one of my existing
26:24branches. In that case I will just say
26:29get let's say if I say get branch
26:36we have these branches feature one
26:37feature two I will say get checkout
26:42and I'm not using hyphen B because I
26:44don't want to create a new branch
26:46otherwise you can use that and I just
26:48want to switch to my feature one branch
26:54Okay. And if I run get branch now I am
26:58on my feature one branch.
27:01All right. Now
27:04you can see here you don't have those
27:09changes that you have done now on the
27:13main branch or the master branch. So I
27:15think
27:16instead of again switching back or I
27:20will first have to switch this with the
27:23main branch and then finally switch it
27:25back to this branch. So let me just say
27:28get checkout
27:31master
27:33and now let me just instead of doing
27:36that extra step I will create a new
27:39branch and check out to that branch. I
27:41will say get checkout feature. This time
27:43I will call it as three. Okay. So this
27:46will
27:48go to a check out a new branch. Create
27:51and switch to a new branch. Okay. And
27:55I'm on my feature three branch now.
27:59Okay. So now because it is a new branch,
28:02it has taken everything that we have on
28:04our master. And now I am going to
28:08modify the same file and the same line
28:11in this branch. And I will open this
28:15employee.txt.
28:17And let's say instead of employee I will
28:20call it as HR management system. Okay.
28:24And
28:25save. And
28:28I will say get status.
28:32And let's commit it. get add dot get
28:38commit-m
28:40and I will say updated employee txt.
28:46All right.
28:48Now
28:50I will switch back to the main branch or
28:53master branch. I will say get checkout
28:58master
29:01and I'm on master now. Okay. And now
29:07let's see what happens. So I am on
29:09master. So if I now check my
29:11employee.txt, it still has employee
29:14here, not HR because we have not yet
29:16merged the changes. But before merging
29:20the changes, I'm going to modify the
29:22same line again in my master branch as
29:25well. So this time I'm going to change
29:27it from employee to payroll.
29:32Okay. Although still the conflict will
29:34happen because even if I don't do this
29:36because still there is there are changes
29:37on the same line. But just to make sure
29:40that two different people are working
29:42and they are changing the same part of
29:44the same file. I'm mim mimicking a real
29:47world scenario. So let's do this
29:50and
29:52I will commit these changes.
29:56Get add
30:01get
30:02commit
30:05updated
30:06employee dot
30:09txt
30:13and this is done. Now let us try to
30:17merge and see what happens. So I will
30:21say let me
30:24just say first
30:28get branch and this is the feature three
30:32branch I want to merge with master
30:34branch. So I will say you should be on
30:36the master branch and now say get merge
30:40and the branch name feature three.
30:45Okay, now you can see we it was trying
30:49to automerge but there are some
30:51conflict. So so there are merge conflict
30:54in employee.txt.
30:56Automatic merging has failed. So get
30:59informs us fix the conflict manually and
31:02then commit the result. Now how do we
31:05know where are the conflicts? So for
31:08that open the file and g will create
31:12these
31:14head and all these uh conflict markers.
31:19These are called conflict markers. So it
31:22will tell us that this is what we have
31:25in the in this branch and then this is
31:28coming from the branch that you are
31:30trying to merge which is feature three
31:33branch. So this is what feature three
31:35branch has done to this particular line
31:37and this is what you have in this
31:38branch. So what do you want now? Fix it
31:41and then commit the final change. Okay.
31:44And this is obvious that git was not
31:48able to resolve because git does not
31:50know which change to keep. You are
31:52changing the same part of the same file,
31:54the same line, the same reference. So
31:56git will not know what to keep and what
31:58to discard. So it will just tell you
32:01that there are merge conflicts and you
32:03have to resolve it and it has also
32:06generated these conflict markers. So now
32:09you will
32:11you can discuss with the developers. You
32:13can always check who did those changes
32:15by saying get log or to see a I will
32:21press Q to come out and then I will say
32:24get log -
32:28one line
32:30and you can see all these. So you are we
32:34are working on employee.txt. So this was
32:36created here in the master branch that
32:38is fine and then you have also updated
32:41this in the master branch and now you
32:44see here that this change was done in
32:46the feature branch. So if you want you
32:48can switch to feature branch feature
32:50three branch again run git log and see
32:53who did what change and then discuss
32:56with the developers or the users or the
32:58teams who have done these changes and
33:00then decide which is what you have to
33:02keep and then you can finally remove
33:05these markers. So these are the conflict
33:08markers. remove these markers
33:14and
33:16then finally whatever is you think is
33:19the right data here save and commit
33:23that. So now I will say
33:26get status
33:29get add
33:33commit hyphen m and
33:37I will say resolved
33:39merge conflicts in
33:44employee
33:47dot txt
33:50and done
33:52And
33:54here
33:56I will say
33:59get status.
34:03Now everything is clean. And if I now
34:05run the command get log one line. Now
34:08you can see here resolve merge
34:11conflicts. And this is our final commit.
34:16Okay. And you can also see that we have
34:21got everything here. Now feature three
34:24branch is also added here. We have
34:27commit history for everything. And now
34:30we have got this done.
34:33Okay. So see uh this is something that
34:37is very very common in real world
34:40because let's imagine that there's a
34:42huge project and a lot of developers a
34:44lot of users are working on the same
34:46project. the uh and they have their
34:49copies in their local folder in their
34:51local systems and they're working on
34:53that and it can happen that multiple
34:56people are working in the on the same
34:57file. It can be a coding file
34:59configuration file and then somebody has
35:02done some change and pushed the changes
35:04committed and pushed the changes. Now
35:06the other person is also working on the
35:09same file on the same line on the same
35:11reference point. when he or she tries to
35:15commit and push, g will say that there
35:17are some issues. You will you have to
35:19first pull because there are some new
35:21changes in the repository. So first pull
35:23from the remote. So you will say pull
35:25and then when you will uh try to commit
35:28you will see there are some conflicts
35:30merge conflicts and then you can again
35:33fix those conflicts and then commit and
35:35push the final change. Okay. So this is
35:38very common in real world scenario.
35:42All right. Now we have seen what happens
35:46when only one branch changes. So there
35:48is no issues. Git can do a fast forward
35:50merge. What happens if both branch
35:52change different things and when you try
35:54to merge still it will do a automatic
35:56merge using the ORT strategy. And what
35:59happens if both branches change the same
36:01thing and then when you try to merge
36:03there is a merge conflict that you have
36:05to resolve manually and then finally
36:09commit the final change.
QUIZ 1 - Merge and Conflicts - https://forms.gle/rhdhDkzzj7AzcbhY7
36:12All right. So the here is a quiz and uh
36:16I will keep this link of the quiz in the
36:20description of this video and it is very
36:23very important that you take this quiz
36:25because that will not just check your
36:27practical knowledge. I am hoping that
36:29you are doing hands-on along with me. So
36:31please follow these sessions along with
36:33hands-on and then only you will
36:34understand what exactly is done and then
36:36you will also have some good questions
36:38for me as well. So please do hands on
36:40and then take this quiz. So this is not
36:42just will help you to check your
36:45understanding but will also act as a
36:47refresher and a summary of what all we
36:49have done. So these quizzes are exactly
36:51based on the topics and the sessions we
36:53are doing. Okay. So I'll keep this link
36:56in the description. Uh and also after
37:00doing the quiz please let me know your
37:01score in the comment section.
37:05Okay. Now let's see what happens when
Commit Only Part of a File
37:11you want to stage and commit only
37:15selected changes within the same file.
37:18So let's say you have a file and you
37:19have made multiple changes in the file.
37:21Now you want to only pick up some
37:23particular change and commit those
37:25change and not the other ones. So can
37:28you do that with git? Let's see a
37:30scenario. So let's say you are modifying
37:33a file and you have made two changes in
37:36the file. Now one change is related to a
37:39bug fix and the other change is a new
37:42feature. So you are doing a bug fix and
37:44also in the same file you are creating a
37:46new feature which is still in progress
37:48and not ready. Now you just want to
37:52commit the bug fix part which is
37:54completed and ready. So what we have to
37:56do and how does git help us doing this?
38:00Okay. So let's say create let us create
38:04a file demo.txt.
38:07So
38:09I will say
38:11I will create a new file here
38:16and
38:18I will call it
38:20demo.txt
38:22txt
38:25and I will add something let's say login
38:28module user validation password
38:30validation so you can imagine it's like
38:32a some code file where we have different
38:34modules login
38:39user
38:42name validation
38:50password valid validation and
38:55that's it. Okay,
38:58let me call it as module. So you can add
39:00some data just for this demo. So let's
39:06save this file and commit it.
39:11So I will say
39:16get status
39:19get add dot get commit- m
39:26addit
39:27demo.txt.
39:30Okay. Now
39:34here we are we are going to modify two
39:36different lines in the file. Okay. So
39:38let's say some part is related to bug
39:39fix and then we are also adding a new
39:41feature. Let's say forgot password. So I
39:44will say
39:46here there was some bug fix. So
39:50I will say fixed bug here. So see here
39:54I'm just typing this in real world it is
39:56maybe a code file where you have done
39:59some bug fix in the in this particular
40:01code part. And then I will say uh here
40:05there is some new feature let's say
40:08forgot password feature which we have
40:09added. Now I will say forgot
40:13password
40:16feature. I will just say in progress.
40:23Now see here
40:26we are doing this change and we are
40:29going to commit it in a single commit.
40:32We are going to stage and commit in a
40:34single commit. So let's see what
40:36happens. So I'm going to save it and I
40:40will say
40:42let me just run
40:44get status
40:46and get add
40:51and then I will say get commit m
40:57and I will say
40:59uh no I will have to say let us say
41:05I will go here. So I will have to
41:08actually after get status I will have to
41:11only stage a part of the file. So I will
41:14say here after changing this I will not
41:18stage everything but only a part. So for
41:21that I will say get add- p. So let me
41:24just try this again.
41:28uh
41:29or let me say
41:33I can say get restore
41:39if I say get status it is already staged
41:43you can see it is in the green that
41:44means already staged but I want to bring
41:47it back from staging area I will say get
41:49restore
41:52-staged
41:55and give the file name demo tx XT.
41:58Now don't worry on these commands. We
42:00have not learned it yet. I will show you
42:03all this. So I'm just trying to restore.
42:06And if I now say get status, you can see
42:11my file has again gone to the unstaged
42:14changes area or unstaged area. Okay. Now
42:19to stage it instead of saying get add
42:23dot or get add file name I'm going to
42:25say get add- p. So hyphen p is for
42:31partial staging. So let's see what
42:34happens.
42:35I will say
42:38here
42:44get add- pit
42:49enter. Now you can see here we are
42:52getting the changes that we have done in
42:54the file. So this is the first change
42:56where we changed this and we did a we
43:00said fixed bug and this is the other
43:03change where we added this forgot
43:06password feature. Okay. Now
43:10here
43:12sometimes g will take these two changes
43:15separately and will show you one of two,
43:18two of three of two like that. Okay. But
43:21a lot of times it will take both of
43:24these changes or any number of changes
43:26that you have done in the file as a
43:27single change because we have done all
43:29these changes at once. So g is taking
43:32this as a single change or a single
43:34modification but we want to split it. We
43:38want to split these modifications. So we
43:41will say in in case you want to stage
43:44this because we are just seeing one
43:47change you can say yes. If you want to
43:50uh don't want to stage it, you can say
43:51no. But we want to split first and then
43:54decide. So we will say S. S means is to
43:56split. So I will say S here
44:00and hit enter. And you can now see we
44:03have got two different changes. First it
44:06is showing me the first change which is
44:08one of two. And now we can decide do we
44:10want to stage it or not stage it. And we
44:13have other options as well. And then I
44:15will let's say I will say yes I want
44:18this because this is a bug fix part. I
44:20will stage this. I said yes. Now it is
44:24showing me the other change the next
44:25change or the next modification
44:28which is two of two. And now again
44:30asking us do you want to stage this
44:32change? This I don't want. I will say n
44:36and hit enter. And now you can see now
44:40the interesting thing we have already
44:42done what we wanted. We wanted one
44:44change to be staged and one to not to be
44:47staged or the other one not to be
44:49staged. We have done that. If I say get
44:51status
44:52now let's see what happens. So you can
44:55see this is an interesting thing. The
44:58same file is now shown as
45:02staged and the same file is shown as
45:04unstaged. And we know why because in the
45:08same file there are more than one
45:10changes. We have staged one change and
45:13we have not staged the other change.
45:14That means there are still work in this
45:17file that is yet to be staged and
45:20committed. Therefore, we are seeing this
45:22file in both the unstaged and the staged
45:24area.
45:26All right. So,
45:29we have done this.
45:31If both edits are shown together, press
45:33S to split. And we have done that. And
45:36then shows us all the changes one by
45:39one. And we can decide what we want to
45:41do whether we want to stage or not. And
45:45then
45:46uh we have ran get status to check and I
45:50will commit. So now only what we have
45:53staged will go to commit. I will say get
45:56commit-m
45:58and we'll say
46:01bug
46:04fix added.
46:09All right. Now if I see get status you
46:12can see the change that we have not yet
46:15staged and we are working on is still
46:17there. So I can complete that part as
46:21well. And once it is done I will
46:25I can again stage and commit this one as
46:28well. So I will say get add dot
46:32and then
46:34get commit-m
46:37I will say
46:40feature added in demo.txt txt
46:46and if I say get status now everything
46:50is clean.
46:54All right. So this is how we can commit
46:57only a part of the file.
47:02All right.
47:05Okay. So the options that we got when we
47:09were doing this partial commit.
47:13So Y is to stage the display change. So
47:15whatever G is displaying if you want to
47:17stage it you say Y. N is to not to stage
47:21the display change. S is to split the
47:24changes into smaller hunks that we have
47:26already seen. Q is to quit patch mode
47:30and question mark is to show help. E is
47:32to manually edit the patch. So these are
47:35the options we generally use and mostly
47:38we use these three options. S only when
47:40we have to split in into smaller changes
47:44hungs. Yes is to stage and is to not
47:47stage. So if you want you can try other
47:49options and let me know in the comments
47:51if you face any issues.
47:53Okay. So now
Recover Deleted Files
47:56let us see how can we recover
47:59accidentally deleted files. And here we
48:02can also use the same strategy to
48:05recover accidentally deleted changes or
48:08data in a file. Okay. And this is this
48:11will work at a file level. So let's
48:13create a file. So I we already have
48:17login.txt. Let us continue using it.
48:21Okay. So this we have here.
48:24Now
48:27we will add some data and
48:30stage and commit this.
48:33I will say
48:36using this to check
48:40get restore
48:43recovery
48:51deleted
48:53changes.
48:55Okay, let's
48:58save it and I will also commit and push.
49:04So get
49:07status
49:10get add
49:12and get commit
49:18hyphen m
49:21I will say modified
49:25login.txt.
49:30Okay. Now we are going to delete the
49:33file. So we are mimicking a scenario
49:36where we accidentally delete a file.
49:38Okay. Now you can try this also for
49:42something like you have deleted some
49:44data in the file instead of deleting the
49:47complete file. If you have deleted some
49:49data in the file and then you run the
49:51next commands we are going to learn it
49:53will work for that as well. But let us
49:55see a demo of completely deleting a
49:58file. So I am deleting this file. You
50:02can see login.txt is gone from here.
50:05Okay. Now
50:08let us
50:10see what happens if I say get status.
50:21You will see we have deleted this file.
50:24Now we have not yet staged or committed
50:27this change. So what happens?
50:30So g has g forgotten the file. So no g
50:34always remembers the last committed
50:36version. And now to recover the changes
50:40or recovered lost file I will say get
50:42restore login.txt.
50:44So I will say
50:47get
50:49restore
50:51login.txt txt and let's see what
50:54happens. I will hit enter and you can
50:56see the file has come back here.
51:00Okay. So we have got the file here.
51:04Okay.
51:06Okay. And we can try this same thing
51:09same command for one more thing that is
51:11let's say we accidentally deleted some
51:14data in the file and we save the file.
51:17Okay. And I will save the file. Now if I
51:20say get status
51:25you can see it says something is
51:28modified here but I have not yet staged
51:31the changes. I will just say get restore
51:35and the file name. So let's see what
51:37happens. So I've done this and now if I
51:41go and check login.txt you can see the
51:43data that we deleted has come back. So
51:47it recovers the file in the sense even
51:50if you delete the file or you do some
51:53changes it will recover it. This is the
51:55get restore command.
51:57Okay. Now here let us see the scenarios.
52:01The first scenario that we just saw is
52:04we deleted the file but did not stage
52:06it. In that case recovery is very easy.
52:09We just say get restore and the file
52:11name. What if we deleted the file and
52:15also staged it. So after deleting we
52:17also said get add dot that means we
52:20staged the change in that case if you
52:23have to restore you will say hyphen
52:26staged and then give the file name and
52:28then again run get restore command. So
52:31let us see if I delete this file
52:33login.txt
52:36and if I say
52:38get
52:40status you can see it is deleted and I
52:42will also stage it. I will say get add
52:45dot
52:46Now to recover I will say
52:50get restore
52:53I will have to now say
52:56hyphen -y staged because I want to
53:00restore from the staged area and what I
53:02want to restore is login.txt.
53:07Okay. Now after running this you can see
53:09I have still not got my login.txt file
53:12here. Now it has come back from the
53:14staging area but still not here in the
53:16working directory. So I will again run
53:18the command get store get restore
53:23login.txt
53:24and now you will see I have got back my
53:28file here.
53:30Okay. Scenario three. You have deleted
53:33the file or you have uh deleted some
53:37data in the file staged the changes and
53:39also committed it. how we can recover
53:42now. So in this case we can still
53:44recover because as we I told you g will
53:48remember all the older commits and it
53:49keeps a history until it is garbage
53:51collected. It will maintain the history
53:53and by default for 90 days it will keep
53:55it. So here we will first run the
54:00command get log or you can say get log
54:03one line and we will then see the commit
54:08id where we had that particular file. So
54:11if I say I will run the command
54:14I will say
54:17get log - one line and this will show me
54:21all the commit history and for every
54:24commit we have got this unique ID. It is
54:26a SHA number. Now we can see
54:30any earlier
54:32commit where we had login.txt.
54:36Okay. So we know if we know that we
54:38deleted it at this commit. So this
54:40commit will have that file. We will just
54:43copy this. You can press enter to copy
54:45from command prompt. And then you can
54:48just say get restore - source and give
54:54the commit id. Let me just show you. So
54:57if I say get status,
54:59everything is fine. I will just remove I
55:02will delete this file. And if I again
55:05run get status, we have deleted this
55:08file. And I will also stage this change
55:11and as well commit it. I will say get
55:14commit m. Now see here although I'm
55:17saying deleted the message but in in
55:20real world it is something that you have
55:22accidentally deleted. You may not even
55:23know that you have deleted the file and
55:25you will just give some commit message
55:27based on whatever work you are
55:28committing. But here I will say just for
55:30this demo deleted
55:32login txt.
55:35Okay. And this is done. If I now say
55:41get log
55:44one line. So you can see this is the
55:49latest commit where the file is deleted.
55:52But before this the file should be
55:53there. So I will just copy this commit
55:56id and I will say
55:58get restore
56:01and I will say source equals
56:05this commit
56:06and I will say
56:10the file which is login.txt txt and this
56:14should restore the file from this
56:17particular
56:19commit and bring it back. Now instead of
56:23this
56:26id commit id you can also say head minus
56:29one or head minus2. So see head is
56:33always at our last commit. So this is
56:37you can refer to this commit by this
56:39commit ID or just say head. Then this is
56:43head minus one. So you can refer this
56:44commit by this commit id or say head
56:46minus one. This is head minus2. So you
56:49can refer this with the commit ID or
56:51head minus2. So that means if I have to
56:54go to this commit I can say this ID or
56:57head minus one. So if even if I say
57:01instead of a commit ID I say
57:08head
57:09minus one. Now see we say head minus one
57:12but we use this symbol. This is the till
57:14day symbol. This key is at the top left
57:18corner of your keyboard just below the
57:20escape key. So you can use this as well.
57:23Okay. And if I hit enter so just see
57:25what happens. I hit enter and I have got
57:29our login.txt back from that commit. And
57:32you can see everything is fine.
57:35Okay.
57:37So this is how you can use it. You can
57:39use like this. So basically we have to
57:40give the source and the commit from
57:43where we have to get it. We can refer to
57:45the commit by its commit id or by using
57:47head minus1 head minus2 and so on and
57:50then give the file name that you want to
57:52restore.
57:55All right. So this is how we recover
57:58deleted files or deleted data in files.
58:02Okay. Now scenario four. You have
58:05deleted commit and also pushed it to a
58:07remote repository. Okay. What happens in
58:10that case? So once you do that still you
58:14can recover because your remote
58:16repository whether it is GitHub or Azure
58:18or Bitbucket it will keep a history and
58:21also because Git is a distributed
58:24version control system it will also keep
58:27the entire project history on your local
58:30system and from here you can go to any
58:32of the previous commits and get back the
58:35file from any of the previous commits.
58:37Okay. Now a git as I told you is a
58:41distributed version control system. That
58:43means every team member or every
58:46developer or every user that we have in
58:48the team will have the complete copy of
58:50the project on their local systems.
58:53Okay. So if anything happens on any
58:55other system or even at the on on the
58:58cloud repository where it is hosted and
59:00the repository our remote repository is
59:02still there is no issue because every
59:05person has a local copy of their
59:08complete project along with the complete
59:10history all the commit history and
59:12everything and also to work locally with
59:16their project using it. We do not have
59:19to be connected to the internet. We need
59:21a internet connection only when we have
59:23to push our changes to the remote
59:24repository. But as long as we are
59:26working on our local repository on our
59:28local working directory, we don't need
59:31any network connection. So that is why
59:33it is called as a distributed version
59:36control system. Okay. So this is in case
59:40of you have also pushed the changes then
59:44still you can use this command get
59:46restore and say hyphen - source give
59:49your old commit id and then give the
59:52file name. So this will work even if you
59:54have pushed your changes to remote.
59:56Okay.
59:58And if you want you can also use get
1:00:01revert in case you do not want your your
1:00:05goal is not to delete commit while
1:00:08preserving history. You goal is to undo
1:00:11the deletion commit which you want
1:00:12because we want to restore while
1:00:15preserving the history. In that case you
1:00:17can do get reward. Don't worry about
1:00:18this get reward as of now. We will we
1:00:20are going to learn this as well. But as
1:00:22of now we understand how we can recover
1:00:25accidentally related files or changes in
1:00:27a file by using g restore.
1:00:31All right.
Git Commands learned till now
1:00:33So the commands we have learned
1:00:36for merging we know we use git merge and
1:00:39the branch name. And to check merge
1:00:43conflicts we can run git status which
1:00:46will show us if there are any merge
1:00:47conflicts. Although whenever we merge
1:00:49and there are conflicts, g will tell us.
1:00:52And if you want, we can again run get
1:00:53status. Okay. To stage particular
1:00:58changes from a file, we use get add- p
1:01:03and then to split those changes into
1:01:06smaller chunks, we use s for split. And
1:01:09then we can decide which change to stage
1:01:11and which to not. To restore deleted
1:01:14file or to restore deleted data from a
1:01:17file, we use get restore. And we have
1:01:20also seen uh in case we have just
1:01:24deleted data or file and have not yet
1:01:26staged, we just say get restore and the
1:01:28file name. If we have staged, we also
1:01:31use the option hyphen staged here along
1:01:34with key restore and give the file name.
1:01:37And if we have also restored uh we have
1:01:41also staged committed
1:01:44and also we have pushed the changes then
1:01:48we say get restore and hyphen - source
1:01:50and give the commit id from where we
1:01:53want to restore the file and give the
1:01:55file name and this is what we have
1:01:58already seen to restore stage selection
1:02:01we do this.
1:02:04All right. Now let us see a very
Using Git cherry-pick
1:02:07interesting demo.
1:02:10Let's say you have
1:02:13you are creating some you are doing some
1:02:16changes in a new branch in a feature
1:02:18branch and now you want to pick some
1:02:21particular commit from that feature
1:02:24branch and merge to the main branch. So
1:02:26you don't want to merge the complete
1:02:28branch because you may be working with
1:02:29several uh things on the feature branch.
1:02:32It can be a bug fix, it can be a new
1:02:34feature creation and you have already
1:02:36committed the bug fix and for the
1:02:38feature creation you have done some
1:02:40commits but it is still in progress. You
1:02:42just want to pick the bug fix commit
1:02:45from the feature branch and merge it
1:02:47with the main branch. So in that case we
1:02:49use git cherrypick. It is very
1:02:51interesting. So let's say you are
1:02:54working on your feature branch and it
1:02:56contains a bug fix which is completed
1:02:58and ready and a new feature which is
1:03:00still in progress and you say uh let's
1:03:04say there's a scenario that you you your
1:03:06bug fix has to go into production today.
1:03:08So if you merge the complete branch with
1:03:12the main branch it will not work because
1:03:15in your feature branch you are still
1:03:18working on the new feature but the bug
1:03:20fix is ready. So you just want to pick
1:03:22up the commit with a bug fix from the
1:03:25feature branch, merge it with the main
1:03:28branch. And you can do this with
1:03:31cherrypick.
1:03:34Let's do a demo. So let's create a file
1:03:38and add some content and commit it. So
1:03:41here I will say
1:03:46I'll create a new file.
1:03:52I will call it project
1:03:571.txt. You can call it anything.
1:04:00And I will say this is a demo
1:04:06project file
1:04:09and save it.
1:04:12Okay. And I will also going to commit
1:04:14it. So I will say
1:04:17so just to confirm I'm on my main or my
1:04:21master branch I will say get status and
1:04:24this is there we also have this earlier
1:04:26login.txt we have made some changes so
1:04:29that's fine I'm just going to
1:04:31stage all this by saying get add dot and
1:04:35then get commit-m
1:04:39added project 1.txt txt file
1:04:44and
1:04:46get status everything is clean. All
1:04:50right. Now let us check out a feature
1:04:54branch. So I will say
1:04:58get checkout - b
1:05:04feature
1:05:06I will call it feature project one. Okay
1:05:10it is done. If I run git branch, I am on
1:05:13my feature branch. And now my working
1:05:15directory is on my feature branch. Now I
1:05:20will make two separate commits
1:05:22in this feature branch. First I will do
1:05:24a bug fix. And just to mimic this
1:05:27scenario, I'm going to commit a file
1:05:30called bugfix.txt.
1:05:32So I'm going to create a file
1:05:39and call it
1:05:42buckfix.txt.
1:05:44In the file I can just say
1:05:47here is the
1:05:50here is the fix ready
1:05:53and I'm going to commit it. So
1:05:58get status
1:06:01get add dot and get commit m
1:06:09bug fix done.
1:06:13Okay.
1:06:15And get status.
1:06:18It's all done. Now I'm going to do
1:06:21another commit where I'm going to mimic
1:06:24that I am working on some new feature.
1:06:26Let's say dashboard. So I'm creating a
1:06:29file dashboard.txt txt
1:06:38and I will call that
1:06:40working on the new
1:06:43dashboard
1:06:45feature
1:06:47and save it and I'm going to commit this
1:06:52as well. So I will say get add dot and
1:06:56get commit-m
1:07:00and I will say
1:07:05uh let's say added
1:07:08module 1 in
1:07:12dashboard txt. So this is still in
1:07:16progress but I'm making one commit.
1:07:18Okay. If I say get log
1:07:21-
1:07:23one line,
1:07:25you can see here we have got this two
1:07:29commits done in this branch.
1:07:33Okay. Now
1:07:36let us say we have already seen the
1:07:39commit history. Now let us say
1:07:43we will take one particular commit. So
1:07:46we have got all our commits. Now make
1:07:48sure that these two should have been
1:07:50done in separate commits because get
1:07:52cherrypick will pick up the commit not
1:07:54the files or not the lines. So that's
1:07:57why we have done these two separate
1:07:59commits. I'm going to take this commit.
1:08:02So copy this commit ID. You can hit
1:08:05enter. They should be copied or let me
1:08:07keep it in a notepad.
1:08:12Okay. And we can switch back to our main
1:08:15branch. So I will say get
1:08:19check out main or master and
1:08:27I am on my master branch.
1:08:32Okay.
1:08:34Now we will pick only a particular
1:08:38commit from the feature branch and merge
1:08:41it with our main branch. And for that we
1:08:43use get cherrypick. we will say get
1:08:46cherrypick and we'll give only the
1:08:49commit id of that commit that we want to
1:08:52merge. So this is get spacey cherry
1:08:54hyphen pick space the commit ID. So
1:08:58let's see and you can also see here
1:09:03you can also see here as of now in my
1:09:07main branch I do not have any of those
1:09:10files that I created in my feature
1:09:11branch bugfix or uh dashboard.txt. txt.
1:09:15So what you will you can observe what
1:09:16happens here when I say get cherrypick
1:09:22get cherrypick
1:09:24and paste the commit id.
1:09:29Okay now I will hit enter and you will
1:09:31see here what happens. So as I hit enter
1:09:34you will see bugfix.txt has come but
1:09:36dashboard.txt has not come. So only that
1:09:39change or only that commit is merged.
1:09:42All right. You can also run the command
1:09:47get log one line and you can now see
1:09:50here we have got only this bug fix
1:09:53commit added to our master.
1:10:00Okay. So this is again a very common
1:10:05real world scenario where you want to
1:10:08pick up a specific commit from a feature
1:10:10and merge with your main branch and do
1:10:12not want to merge anything else. So in
1:10:15real world this is a very common thing
1:10:17as we check out branches and do work on
1:10:20that what is ready we just want to merge
1:10:22that and what is not ready we do not
1:10:24want to merge that. Okay. So always
1:10:26remember this command get cherrypick
1:10:28will be very very useful.
1:10:31Okay,
1:10:32so we already know get merge brings all
1:10:35the commits from another branch. Get
1:10:36cherrypick brings only the specific
1:10:39commit that we choose.
1:10:42Okay, again there's a quiz. So this is
QUIZ 2 - Commit Part of a File and Git Restore - https://forms.gle/tiXJUvg8pDSmTokF7
1:10:45based on what we have done now and this
1:10:49will be a very much practical quiz.
1:10:51Please take this quiz and let me know
1:10:52your score in the comment section. Let
1:10:56us now go to
Git Blame
1:10:58the next topic and demo which is get
1:11:02blame. Now whenever you want to identify
1:11:06that who has changed the last
1:11:10uh who has changed any any particular
1:11:12line of a file and when what is changed
1:11:16who has changed it what was the line
1:11:18earlier what exactly was the commit ID
1:11:20anything you can use get blame. Now see
1:11:23this name looks a little bit negative
1:11:26that like we are blaming somebody but in
1:11:29real world you can imagine that let's
1:11:31say you are running your playright tests
1:11:33and the test suddenly starts failing.
1:11:36Now you open the test and notice there
1:11:39is some change done by somebody on this
1:11:41particular line which has changed this
1:11:44ID or something which is breaking the
1:11:48tests and you wonder who changed this
1:11:50locator and instead of asking everyone
1:11:52in the team because see in real projects
1:11:56they there will be so much of changes
1:11:58happening every time with everyone
1:12:00making some changes. So instead of going
1:12:03and checking with everyone and even the
1:12:05person who has done it may not remember
1:12:06that when he did what he did or what
1:12:09exactly did he do. So instead of doing
1:12:12all that you can just run the command
1:12:14get blame and then
1:12:18you will come to know what has happened.
1:12:21Okay. So let's assume we already have
1:12:23some files and some we have already
1:12:25committed and we have commit history
1:12:27which is true. We have a lot of commit
1:12:29history now. So let's first say get log
1:12:33online and we have already executed
1:12:36this. We have this here. Okay. So now
1:12:42I will say get blame and give some
1:12:46particular
1:12:48file name
1:12:50or if you want you can also take some
1:12:53particular commit id. So from this log
1:12:55so see when we see the log we can just
1:12:57say get log and we can see the details
1:13:00about every commit. Okay. So you can see
1:13:03this will show us the all the commit and
1:13:07details about every commit. But if it is
1:13:11a long commit history in that case I
1:13:14will press Q to come out and in that
1:13:17case you can just say
1:13:20get log with the option hyphen 1 line.
1:13:23So every log, every commit history is
1:13:26shown in a single line with the commit
1:13:29ID and the commit message and details.
1:13:32In case we are on a different branch, it
1:13:34will show you we have already seen uh we
1:13:36can use graph and graph all options. So
1:13:40that will also uh show us I think
1:13:46okay
1:13:50that will also show us these graphs
1:13:52lines in case you want to check that. So
1:13:56this is to see all the history. Now here
1:14:00you can either check
1:14:03when was this file modified and take
1:14:06that particular commit id or if you just
1:14:10want to check that this has happened on
1:14:12a particular file you can just give the
1:14:14file name. So I will say get blame and
1:14:16some particular file uh let's say I will
1:14:20say get blame
1:14:25login.txt txt.
1:14:28Okay. Now all the changes in this file
1:14:32whatever we have changed whenever we
1:14:33have changed everything will come here.
1:14:35So as of here because we have not done a
1:14:38lot of changes we are just seeing two
1:14:41things here. So this is done
1:14:45here by this author at this particular
1:14:49time and this was when it this file was
1:14:52created in the feature 2 branch. So this
1:14:54is fine. Then in this particular commit
1:14:58by this particular author at this
1:14:59particular time this was using this to
1:15:03check get restore recover accidentally
1:15:05related changes. So we can see two of
1:15:07these changes. You can take any other
1:15:10file as well. Let's say if I say
1:15:17uh let me say employee.txt.
1:15:22Here also you can see we have these
1:15:25changes two changes here. This was
1:15:28changed to HR management system by this
1:15:31author at this time at this particular
1:15:33commit ID and this change as well. Now
1:15:37after taking after checking this now
1:15:40then you can say get show and that
1:15:43particular commit ID to investigate
1:15:45further. So if I want to check this, I
1:15:50will copy this commit id and I will say
1:15:55get show and that particular commit ID
1:16:00and here I will get all the details of
1:16:03what exactly was changed and who has
1:16:06changed when and exactly what happened.
1:16:08So here you can see this was the earlier
1:16:11state and this was then changed to this
1:16:15and you can also see all those resolve
1:16:19merge conflicts and then this was done.
1:16:21Okay. Similarly if I say
1:16:25get blame login.txt
1:16:28and I take this and say get show
1:16:34and then do this. You can see this is
1:16:37what was done here. So you will get all
1:16:41the details and then you can decide what
1:16:43we have to do, what action we have to
1:16:45take. So you can do that. Okay.
1:16:51All right. Now let us come to get
Git Restore
1:16:53restore
1:16:56and get restore. We have already seen
1:17:00that whenever you accidentally delete a
1:17:02file or change something in the file and
1:17:05you want to restore it you can use get
1:17:08restore and it's not that you if you
1:17:12have committed it even then you can
1:17:13restore it. We have seen that. So let's
1:17:17see the steps. So we have seen this get
1:17:19restore while learning how can you
1:17:21recover deleted files but here we are
1:17:24going to see what all we can do and we
1:17:26can also recover deleted data from a
1:17:28file. So let's say if you modify
1:17:32something in a file
1:17:34and then you save it and you have not
1:17:38yet staged or committed it, you can just
1:17:41run get restore and the file name and
1:17:44that will
1:17:46bring back the deleted part in the file.
1:17:48Okay. And you can open and check this.
1:17:50We have already seen then.
1:17:55So see get restore you can use when you
1:17:58want to discard local uncommitted
1:18:00changes in one or more files and return
1:18:02them to the last committed state. So you
1:18:05can use that and it is it will only
1:18:08affect uncommitted changes. It does not
1:18:10delete commits. However, still if you
1:18:14want to bring back a file from an
1:18:16earlier commit you can do that and we
1:18:19have seen that earlier. You can use
1:18:21commands
1:18:23like
1:18:26you can say
1:18:28just if you have not staged any file and
1:18:32you bring back the changes or the
1:18:34deleted files you will say get restore
1:18:38and what is whatever is the file name
1:18:39let's say hello.txt txt in case you have
1:18:42already committed
1:18:45uh in case you have already staged that
1:18:47means you have already added to the
1:18:49staged area and then you want to bring
1:18:51back the changes that you have deleted
1:18:53either a file or some things in the
1:18:56files you have deleted you will say
1:18:57hyphen -y staged and the file name and
1:19:00then you will again have to run the
1:19:03command get restore and the file name
1:19:05and if you have also committed the
1:19:07changes after deleting something the
1:19:09file or the data In that case you will
1:19:11say get restore - source equals and then
1:19:15you have to restore the file from an
1:19:18earlier commit when it was there
1:19:22in our repository. So you can either say
1:19:26the
1:19:30ID of the commit. You can take the ID by
1:19:32saying get log and copy the ID and give
1:19:34it here. So it will restore the file
1:19:36from that commit. Or you can also say
1:19:39head minus one, head minus 2 and so on.
1:19:43Okay. So when we say when we run the
1:19:47command
1:19:49get log,
1:19:52you can see these are the commit ids and
1:19:55our head is the last commit. Head minus
1:19:57one is the previous commit. Head minus2
1:20:00is this commit. Head minus 3 is this
1:20:02commit and so on. Okay. So this is what
1:20:06you can do. Now let's come to get reset.
Git Reset
1:20:10So what is get reset? Okay. Now
1:20:15it is like going in the backward
1:20:17direction. It is going to it is going to
1:20:20rewind our commit history. Okay. Instead
1:20:23of creating new history, it's going to
1:20:25rewind it. And when do we want to use
1:20:27it? So let us try this demo first. I
1:20:31will create a new file and commit it.
1:20:34Let me just
1:20:36check we have a clean working tree.
1:20:42Yes,
1:20:43I will create a new file
1:20:50and I will call it as let's say hello
1:20:541.txt.
1:20:57I will say this is a
1:21:00demo file.
1:21:03Save it.
1:21:05Commit it. Get add
1:21:09dot
1:21:11get commit-m
1:21:16and give a commit message addit hello
1:21:201.txt.
1:21:30And now if I run
1:21:33get status,
1:21:36we have a clean working tree. Now
1:21:40let us say I will say get reset - soft
1:21:44head minus one. Now with G get reset we
1:21:48have three options. We have soft, hard
1:21:51and mixed. Mixed is the default option.
1:21:53So if you do not use any option that
1:21:55means it is mixed. Otherwise we can say
1:21:57soft or hard. Now what does it mean? So
1:22:02before that
1:22:04just understand and I just want to
1:22:06confirm because we have already
1:22:08discussed head minus one means the
1:22:12previous commit then the last commit. So
1:22:16our
1:22:18last commit is head
1:22:21when I say get log. So our last commit
1:22:25is the head. Then this previous commit
1:22:28is head minus one. This one is head
1:22:30minus2. So you can refer these commit
1:22:32whenever you want to refer to any of
1:22:34these commits. You can either refer by
1:22:35their commit ids or by their uh yeah
1:22:38this is the commit ID which is the S sha
1:22:41number a unique number or you can say
1:22:43head head minus one head minus2 head
1:22:48minus 3 and so on. Okay. So generally if
1:22:51you have to go three or four uh levels
1:22:54back to the commit history and you know
1:22:57which one you want to go you can easily
1:22:59use head minus one head minus 2 head
1:23:01minus 3 but going back along a lot in
1:23:05the history then you it's definitely you
1:23:07can prefer the ids here okay now if I
1:23:12say get reset - soft head minus one
1:23:17okay I say it
1:23:21reset
1:23:23- soft head
1:23:28minus one
1:23:30and hit enter.
1:23:33Okay.
1:23:35And if I now say get status.
1:23:47So you can see our file has now come to
1:23:52the staged area and it is no longer
1:23:55committed. So the I had already
1:23:56committed my file. So you can see here I
1:23:59had already committed my file and after
1:24:01that when I ran get status everything
1:24:04was clean. There was nothing to commit.
1:24:06But after running this command
1:24:09get reset I am getting this.
1:24:14After running this command, get reset.
1:24:19After running this, my file has come
1:24:23back from committed area to the staged
1:24:25area.
1:24:27Okay.
1:24:29So, this is what has happened.
1:24:32So, we have seen the commit has
1:24:34disappeared. The file is still there in
1:24:36our working directory as well. And it is
1:24:38here.
1:24:40Okay. File is still here. and but it is
1:24:42not yet committed and it is not yet
1:24:45unstaged as well. It is still staged but
1:24:47not committed.
1:24:49All right. So
1:24:52head is our current commit.
1:24:55Then one commit before is head minus
1:24:57one. Head minus 2. Head minus 3. So what
1:25:01we did here when we say reset to head
1:25:03minus one. So now whatever was committed
1:25:09in this particular commit that is head
1:25:11minus one is there in the committed
1:25:13area. Whatever was not committed until
1:25:15here has gone from the committed area
1:25:19has gone back or is now reset and it has
1:25:23moved back to the staging area. So what
1:25:25we did here whatever we did as of now we
1:25:29have just added and committed a file
1:25:31hello 1.txt txt but it can be more
1:25:34changes whatever the the changes you
1:25:36have done will come back to the staging
1:25:38area and they will no longer be
1:25:39committed.
1:25:42All right.
1:25:45So
1:25:47here understand this with this example
1:25:50that you have made four commits A B C D
1:25:53and this is your commit history you did
1:25:55this is the first commit then you did
1:25:57come B commit C commit D our head is
1:26:01always on the last commit so this is our
1:26:04head when we say head minus one when we
1:26:07say head we refer to this last commit
1:26:09when we say head minus one we refer to
1:26:11this when we say head minus 2 we refer
1:26:13to this when we say head minus 3 refer
1:26:15to this. Okay. Now, if you want to go
1:26:20back to C, you will say head minus one.
1:26:22When you go want to go back to B, you
1:26:24will say head minus2 and so on.
1:26:29Okay. So, when we say get reset head
1:26:31minus one, uh this is what it will go.
1:26:35So whatever is committed here will be
1:26:37there in the commit and everything else
1:26:39that we did you did after that will go
1:26:41back to the staging area in case it is a
1:26:44soft reset. Okay. Now what will happen
1:26:48with the hard reset that we will
1:26:49discuss. So soft reset comet is removed
1:26:54and commit is removed in all the cases
1:26:56whether it is a soft mixed or hard reset
1:26:59and that is why we are doing reset. That
1:27:01means we are moving back in the commit
1:27:04history. We are rewinding the history.
1:27:07In case of soft changes are still kept.
1:27:10So we still have all the changes that we
1:27:12have done. It's just that the changes
1:27:14have now gone from the commit history or
1:27:17the commit that we have and the changes
1:27:20are still staged. Okay, that's why we
1:27:23saw here our file is still staged. It's
1:27:26just that it's not committed but still
1:27:28staged and we can now stage it.
1:27:31Okay. Now you may have you may think and
1:27:34get a question that why will this be
1:27:37ever needed? When will this be needed?
1:27:39So I will tell you that in a moment but
1:27:41just for now understand this. In case of
1:27:43soft what will happen if you do a mixed
1:27:46reset. If in case of soft we say mixed
1:27:48in that case commit is removed and then
1:27:52changes are still there but then changes
1:27:55are no longer in the stage area as well.
1:27:57So they will go back to unttracked
1:27:59changes. And in case of hard comet will
1:28:02be removed and then changes will also be
1:28:07removed and then
1:28:10they will they will no longer be staged
1:28:11as well. And when we say changes removed
1:28:14that means even from your working
1:28:16directory. So this is where when we say
1:28:18this column changes kept this means in
1:28:20your working directory in your local
1:28:22repository your local working directory.
1:28:24So in case of hard it will also go away
1:28:26from your local.
1:28:29Okay. So that's why we say we should
1:28:30always use this hard reset with caution.
1:28:33Now when we will need it
Git Reset Soft
1:28:36okay so let's think that you have commit
1:28:40something let's say add login and then
1:28:43you realize I forgot to add another file
1:28:47which may be a helper file or which may
1:28:49be a dependent file and it should go
1:28:51along with this file along with this
1:28:54change in this commit. So now instead of
1:28:56creating another tiny commit you say uh
1:29:01so after you commit this you will again
1:29:04create another tiny commit and say
1:29:06forgot login data in that commit.
1:29:07Instead of doing this you can just
1:29:11run get reset hard
1:29:15and then so sorry this is get reset
1:29:18soft.
1:29:20So this will uncommit your last commit.
1:29:23That means it will come back from the
1:29:25commit and then you can add more files
1:29:28whatever you want to add and then stage
1:29:31all those and commit it again. Okay. So
1:29:34our hello.txt file is already in the
1:29:37staged area. I can now add more files or
1:29:40whatever I want to change make some
1:29:42changes
1:29:43stage those changes and then along with
1:29:45hello.txt I will commit everything.
1:29:48Okay. So this can be a real world
1:29:50scenario. Why do you want to use a
1:29:54soft reset?
1:29:57Okay, let's uh also see what can be the
1:30:00other scenarios. So let's say if I go to
1:30:03chat and say uh
1:30:07when will we use this
1:30:12get reset hyphen
1:30:15soft head minus one
1:30:19what
1:30:22are real
1:30:24world scenarios?
1:30:27Okay. So
1:30:30you can see
1:30:32one of the scenarios can be you have
1:30:35committed but you realize that you have
1:30:37given a wrong commit message and it is
1:30:40not a either it's a wrong commit message
1:30:42or it is not a meaningful commit
1:30:45message. So you can just do a get reset
1:30:47soft and then commit again with a
1:30:49meaningful message. Or the other
1:30:53scenario can be you committed too much.
1:30:56So you you have made multiple changes in
1:30:58multiple files and you committed
1:31:00everything and then you realize that I
1:31:02should have committed this separately.
1:31:05So in that case also you can say get
1:31:07reset soft and then all the changes will
1:31:10be staged again and you can selectively
1:31:13commit. So you can say get restore
1:31:17staged and whatever file you want to
1:31:20restore. We have already seen the
1:31:22restore command and then you can commit
1:31:24one by one whatever you want. Okay. So
1:31:27you can just reset remove last commit
1:31:30and you can do that. Okay. Also we have
1:31:33already seen that in case you want to
1:31:35add more data more files in this commit.
1:31:39So you can again do this.
1:31:43Okay. So soft only moves your head. It
1:31:46doesn't touch your staged changes or
1:31:48working files. We have already seen
1:31:49this. Okay. So we have seen when you
1:31:54want to do get reset soft one is this
1:31:57scenario that you want to commit more
1:31:59files in the single commit along with
1:32:01what you have already committed or you
1:32:03want to you have committed too much and
1:32:05you want to uh bring it back and commit
1:32:07one by one. You may want to change your
1:32:10commit message. All these are scenarios
1:32:13you can use
1:32:16get reset soft for.
1:32:21Okay. Now you can uh we have already
1:32:25seen this. So before get reset soft your
1:32:28changes are committed in the are already
1:32:30in the staged uh area and also in your
1:32:34local repository working directory and
1:32:37after doing get reset soft the only
1:32:39changes your changes will be reversed
1:32:43from the commit area. They will come
1:32:45back from the commit area to the staging
1:32:46area but will still be staged and still
1:32:49be there in your working directory.
1:32:53Okay. So let's try this question and
1:32:56before I tell you the answer you can
1:32:58just pause and check what should be the
1:33:00answer. The question is after running
1:33:04get reset - soft head one where are your
1:33:07changes? Are they deleted completely?
1:33:09Are they only in the repository or still
1:33:12in the staging area and working
1:33:14directory? And of course we know the
1:33:16answer is still in staging area and
1:33:19working directory because soft will only
1:33:21move the head. it will not touch your
1:33:23stage changes or your local working
1:33:26files.
1:33:29Okay, let's go to get reset mixed and
Git Reset Mixed
1:33:32imagine a scenario that you have staged
1:33:36five files and also committed them and
1:33:40later your lead or manager says please
1:33:44split this into two commits. Now what do
1:33:47you want to do? You don't want to lose
1:33:49your work. You don't you just want this
1:33:51that these files are come back from the
1:33:54commit and also are unstaged so that you
1:33:57can again stage and commit them in two
1:34:01separate commits. Okay. So that that is
1:34:04exactly when you can use this get reset
1:34:07with mixed option.
1:34:09Okay. So you can create new file and
1:34:14commit it and check the commit history
1:34:17by saying get log one line and then so
1:34:20see mixed is the default option. So when
1:34:22you do not use any option with get reset
1:34:24it is the mixed option. So let's see if
1:34:29you
1:34:31uh let me just confirm. I will first
1:34:35commit this
1:34:37or I can just use my last command I used
1:34:41for committing.
1:34:45Okay. And let me check get status.
1:34:48Everything is clean. I will create a new
1:34:52file.
1:34:55I will call it
1:34:58hello 2.xt.
1:35:04And this
1:35:06is a demo file.
1:35:10And I will stage and commit it. I will
1:35:14say get add dot
1:35:19get
1:35:21commit- m
1:35:23added hello 2.xt.
1:35:29Okay. Get status. Everything is clean.
1:35:32Okay. And if I say get log
1:35:36one line
1:35:39and this is our commit is here.
1:35:44All right.
1:35:45Now I will say get reset head minus one.
1:35:49So get reset mixed or get reset is same
1:35:51thing. I will say get reset head minus
1:35:54one. Now let's see what happens. I will
1:35:56say get reset
1:35:59head
1:36:01minus one. And as I do this
1:36:05now see my hello.txt is still here in
1:36:08the working directory. But if I run get
1:36:10status now
1:36:13you will see it has now unttracked. That
1:36:17means it is still not added to the
1:36:19staging area. So it has gone from the
1:36:21commit as well from the staging area.
1:36:23Okay. So this is why this is what
1:36:26happens with get reset mixed. So file is
1:36:29no longer staged
1:36:31like it was in soft reset.
1:36:38Okay. So we know that so this part is
1:36:41same for both
1:36:44soft and mixed. Before running this get
1:36:47reset soft and before running get reset
1:36:49mixed we had all our changes committed.
1:36:52After running soft only the commit was
1:36:54gone or the changes were not were gone
1:36:57from the commit but after running get
1:36:59reset mixed the changes are no longer in
1:37:02commit and no longer in the staged area
1:37:04as well. It is only in our local working
1:37:07directory. Okay. Also if I run the
1:37:10command get log
1:37:14you can see our commit for adding
1:37:17hello2.xt txt is now gone and our last
1:37:20commit has or our head has moved to the
1:37:24previous commit which was head minus one
1:37:26earlier but now this is the new head
1:37:28where we added hello 1.txt txt.
1:37:33All right. So this we have already seen
1:37:35this table.
1:37:38Okay. So this is what happened after
1:37:40running mixed commit was removed.
1:37:42Changes are kept but changes are no
1:37:45longer staged.
1:37:50Okay. So again let us check this quiz.
1:37:55After running get reset head minus one,
1:37:58where are your files? Are they deleted?
1:38:00Are they still staged? Are they in the
1:38:03working directory only? So, this is
1:38:04should be the right option.
1:38:10Another quiz. So, make sure that you
1:38:13think and get the answer before I tell
1:38:15you. Which command leaves your changes
1:38:18staged? Okay. So it is saying which
1:38:21command will leave your changes in the
1:38:24staging
1:38:26directory or in the staging area. So is
1:38:29it get reset mixed? No. Because
1:38:33in case of mixed it will go away from
1:38:35the staging area as well. Get reset is
1:38:38also not the answer because get reset
1:38:40and get reset mixed are same thing. When
1:38:42we do not use any option that means it
1:38:44is mixed. So the answer is get reset
1:38:47soft.
1:38:49Okay.
1:38:51All right. Now let us see get reset
Git Reset Hard
1:38:53hard. Now why do you want to do this?
1:38:55Now imagine you are working in your
1:38:56project and you modify let's say a lot
1:38:59of files 30 files and you check with it
1:39:03and nothing works. Now you do not want
1:39:07to manually undo every file or manually
1:39:09delete every file. You can just do get
1:39:12reset hard. So that will instantly
1:39:14return your working directory to your
1:39:18last committed stage or last whatever
1:39:22was there whatever was uh the stage of
1:39:26your directory with the last commit it
1:39:30will just take it back there. Okay. And
1:39:33you can select which commit you want to
1:39:36go back to. Okay. So here use it with
1:39:41caution because this will also remove
1:39:43your changes from your local working
1:39:46directory as well. It will remove from
1:39:48commit, it will remove from staging and
1:39:50also from your working directory. Okay.
1:39:53So let us try this.
1:39:55Uh before that let me just run get
1:39:58status. And
1:40:01here we will have this unttracked file
1:40:04because it has after running get reset
1:40:06it has come back to the working
1:40:07directory. I will just stage and commit
1:40:12it.
1:40:14Okay, I will say this is addit
1:40:16hello2.txt.
1:40:19And now if I run get status everything
1:40:23is clean. So let us create another file
1:40:26now called I will call it hello3.txt
1:40:35and you can add something this is
1:40:40uh demo file
1:40:45and
1:40:47I will say get status
1:40:50and get add.
1:40:52Now if I say get status it is staged.
1:40:55Now I will say get
1:40:58commit- m
1:41:01added
1:41:04hello 3.txt
1:41:07and get status everything is fine get
1:41:12log one line and we can see this is our
1:41:17last commit. This is where our head is.
1:41:21All right. Now
1:41:23if I say we have done all this. Now if I
1:41:27say get reset hard and we can move to
1:41:30head minus one. So get
1:41:34now see if we do not use anything. If
1:41:36you just say get reset hard so then will
1:41:39assume that we just want to reset to the
1:41:42current to the last commit. So it will
1:41:45take head by default. And if we have to
1:41:48go back to any earlier commit, we have
1:41:51to act explicitly say head minus one,
1:41:53head minus2 or the commit ID. So see if
1:41:56I say get reset heart, you will not see
1:41:57anything happening actually because
1:41:59after
1:42:01doing this
1:42:03after committing this we have not made
1:42:05any changes. I will say get reset hyphen
1:42:09hard.
1:42:12Okay. So you can see head is now at this
1:42:16which is also you can see this is where
1:42:19our head is. So when we I say get reset
1:42:21hard head has not moved because this is
1:42:25this was already our last commit and
1:42:28nothing happens in my local directory as
1:42:30well. But just imagine if I make some
1:42:34more changes in my local directory after
1:42:38committing I will make I'll just add
1:42:41some more files. Now see I have added so
1:42:44many of these files. Copy of hello 1 2 3
1:42:484 all these files and not yet committed.
1:42:51And now if I say
1:42:54get reset hard. So what happens?
1:42:59You can see here head is now
1:43:04in my to my this particular commit which
1:43:09is already my
1:43:12last commit.
1:43:14Okay. Now what if
1:43:18I say now see I have done a lot of
1:43:22changes. is I have added a lot of files
1:43:24and due to some reason I don't want
1:43:26these I want to delete it or I want to
1:43:29go back to a particular commit point
1:43:33let's say this one where I did not had
1:43:36hello 3 also and all these different
1:43:38things in that case I can say
1:43:42get reset hard and can give head had
1:43:46minus one or that particular commit id
1:43:49so I will say get reset hard And I will
1:43:53say
1:43:54head minus one. And when I run this, you
1:43:59can see here
1:44:03now my head is moved to
1:44:08this particular point where we added
1:44:10hello2.txt.
1:44:12But if you see here, so see if you see
1:44:15here,
1:44:19we still have a lot of other files. So
1:44:23you can see hello 3.txt has gone, but
1:44:27all the things that I have done after
1:44:29that which are not yet staged and
1:44:31committed are still there. Okay. But
1:44:33let's say I will say get status
1:44:41and I will just stage and commit all
1:44:44these changes.
1:44:52Now see as of now I'm just giving some
1:44:54any commit message but always give
1:44:56meaningful commit messages. Okay. Now if
1:44:59I also run my
1:45:03glog one line you can see it is here but
1:45:07now I want to go back here also in my
1:45:10working directory. So I will say get
1:45:12reset
1:45:14and I will say
1:45:18uh let me just type it get reset
1:45:23- hard head
1:45:28minus one. So as I run this
1:45:31you will see all those files are gone
1:45:33from here also as well as if I say get
1:45:37status
1:45:39they are not in the staging area as
1:45:42well. So everything is clean. Okay. So
1:45:45this is how you can use it and this is
1:45:47the scenario when you may want to use
1:45:49it.
1:45:51Okay. And you can also see this before
1:45:55part is same for every reset soft mixed
1:45:59hard but after hard you can see
1:46:02everything is gone. Your commits are no
1:46:04longer there that you have committed
1:46:06staging area also it is gone and from
1:46:08working directory also they are gone.
1:46:11Okay. So this is how we use get reset.
1:46:17Okay.
1:46:20Let us try this. You modified 12 files
1:46:24and you have not committed. You want to
1:46:26throw away every change. Which command
1:46:28is appropriate? So whether it is get
1:46:31restore, get reset soft, get reset hard.
1:46:35Now think carefully. It is saying you
1:46:38have modified 12 files not yet committed
1:46:42and you just want to throw away
1:46:45all the changes. So in this case you can
1:46:47just use get reset hard.
1:46:53Okay.
1:46:55Again this question after get reset
1:46:58hyphen hard head head minus one. Which
1:47:01statement is true? File remain staged.
1:47:05No only head moves.
1:47:09Head moves but not only head moves. head
1:47:12moves but the staging area is cleared
1:47:14and the working directory is also reset
1:47:16to match the new head. Okay, so this is
1:47:20the correct answer.
1:47:24All right, so
1:47:27if you you can now answer this what's
1:47:29the difference between git restore and
1:47:31get reset hard get restore restore
1:47:33discards changes in specific files
1:47:36without changing the commit history. So
1:47:37if you remember when we were using g
1:47:39restore we were not changing the commit
1:47:41history we were only discarding the
1:47:43changes and that to in specific files
1:47:46but get reset hard will move our head
1:47:48that means it will change the commit
1:47:49history and also resets both the staging
1:47:52area and the working directory to match
1:47:54that particular head or that particular
1:47:57commit.
1:47:59All right so it is much more powerful
1:48:02and should be used carefully.
1:48:05Okay.
1:48:06All right.
1:48:08So here we have done all this.
1:48:13Now you can answer these questions. What
1:48:16head is and what get reset actually
1:48:20does. So what is head? It is our last
1:48:23commit. And if you want to go to the
1:48:27previous commits, we can say head minus
1:48:28one, head minus 22, head minus3 or we
1:48:30can refer the commit with the commit ID.
1:48:33And the last commit if you want to refer
1:48:36we can just refer by hat or its commit
1:48:38ID. Okay.
1:48:40You know the difference between get
1:48:42reset soft, mixed and hard and which one
1:48:45to use in different situations.
1:48:48Okay.
1:48:50Now again there will be a quiz. You can
QUIZ 3 - Git Cherry-Pick, Git Blame - https://forms.gle/DX3DhWzGxt9NHH2J7
1:48:54get the quiz link from the description
1:48:55and take the quiz and let me know the
1:48:58score in the comments. And please take
1:49:01these quizzes because see these quizzes
1:49:04are created very logically
1:49:07u and with the
1:49:09keeping the real world scenarios in
1:49:11mind. So when you run these quizzes
1:49:13after doing practical hands-on with the
1:49:16sessions we are doing you will
1:49:18understand why we are doing this when we
1:49:20do this it will also test your practical
1:49:23knowledge and along with that it was it
1:49:25will uh help you to refresh the topics
1:49:28we have just done. All right so let us
1:49:32now go to a very interesting topic
Git RefLog
1:49:37called get ref log. Okay. So let's say
1:49:43you have deleted something some commits
1:49:47you have lost some commits. Is there a
1:49:49way to bring it back? And the answer is
1:49:52yes. And that is when we will use get
1:49:56ref log. So let's imagine you have
1:49:59completed three commits and you
1:50:02accidentally run get reset hard head
1:50:06minus two. That means you have now
1:50:09rewinded your commit history and
1:50:12whatever was there in head minus 2
1:50:14becomes your new head and whatever you
1:50:16did after that is now gone deleted. It
1:50:20is reset.
1:50:22Okay. Now you realize that you want to
1:50:27bring it back and can you recover them?
1:50:30So that's exactly when you can use get
1:50:33reflog. Okay. And see git ref lock is
1:50:37very interesting. So when we say get log
1:50:39you can see all your commit histories
1:50:41and with along with their commit ids. So
1:50:43you can you can check here. If I when I
1:50:45whenever I run this command we have seen
1:50:48this multiple times. Whenever I run get
1:50:51log you can see all this log history is
1:50:55there. All these commit history are
1:50:57there. But what if we have executed get
1:51:00reset hard and the commit is also gone
1:51:02from our logs. So after running g reset
1:51:05hard when you check your logs you will
1:51:07not see those commits which are now
1:51:09deleted. In that case we can use g
1:51:12reflog and git reflog is like a ccb
1:51:16camera for your repository. It has all
1:51:21everything it has captured whatever you
1:51:24have done in your repository. Whatever
1:51:26commits you have done it it captures
1:51:28everything. Even the deletion part it
1:51:31will capture and keep it in its memory.
1:51:33But one thing is important that you can
1:51:36use git ref lock log to recover deleted
1:51:39commits until a period of time because
1:51:43once git runs its garbage collection
1:51:45which is generally after 90 days. If it
1:51:48is beyond that time you may not be able
1:51:49to uh use git ref log for those
1:51:53particular changes which are now also
1:51:54deleted from the history or from the
1:51:57memory. So before that you can always
1:52:00recover them. Let us see how.
1:52:04So as I told you if git log is g history
1:52:07book git ref lock is like its ccb
1:52:10camera. So it captures everything and
1:52:13even if you accidentally deletes any
1:52:14commits by saying get reset heart get
1:52:17reflock can often help you recover them.
1:52:19often means in case it is within the
1:52:23time until the garbage collection has
1:52:25happened which is generally 90 days.
1:52:29Okay,
1:52:31let's create a file and do three
1:52:33commits. So I will do three commits in a
1:52:37file. I will say
1:52:42I'll create a file just making sure I my
1:52:46working directory is clean and yes I
1:52:49will create a file
1:52:56and let's call it
1:53:03new file one
1:53:08and I will say
1:53:13addit
1:53:15version one
1:53:17and I will just
1:53:21add and commit. I will say get status.
1:53:25I will say get add
1:53:28get
1:53:32commit.
1:53:34- m
1:53:38new file one version
1:53:42addit version one
1:53:45and done. Now I again
1:53:48do some changes after this commit. I'm
1:53:51again saying added version two
1:53:56and I will again say
1:53:59get status
1:54:01and again say
1:54:05uh get add
1:54:09and then
1:54:11again say
1:54:15commit get commit m new file one added
1:54:18version two.
1:54:20All right. Now I will again make some
1:54:24changes. Addit version three.
1:54:28Save and close this file. And here I
1:54:31will say
1:54:38get add
1:54:41and then
1:54:43get commit
1:54:46hyphen m new file one added version
1:54:49three. Okay. If I run
1:54:55get status everything is clean. So if I
1:54:59see the project uh history the commit
1:55:01history get log
1:55:04one line.
1:55:06So you can see all this is now added.
1:55:10So this was our commit when we added
1:55:14version one then we added version two
1:55:16and this is the last commit where we
1:55:18added version three.
1:55:22Okay. Now
1:55:24we have also seen the log. Now let's say
1:55:28by mistake we have reset we have done
1:55:30get reset hard and moved to head minus2.
1:55:33So now you can imagine what is our head
1:55:35minus 2. This is our head.
1:55:39Okay this is our head. This is our last
1:55:41commit. Head is here. This is head minus
1:55:44one and this is head minus2. So if I go
1:55:49to head minus2 that means my working
1:55:52directory and everything will move to
1:55:56this commit or this particular level
1:56:00where I had added the new file and added
1:56:02version two but my version two and three
1:56:05will should have should go away if I do
1:56:07get reset hard here. So I will say
1:56:12get reset
1:56:14hard
1:56:16and I will say
1:56:19head minus 2.
1:56:22Okay. So this is exactly what I'm going
1:56:24to do. So if I now run this.
1:56:29So you can see head is now at this which
1:56:32was head minus2 earlier is our new head.
1:56:36Now you can just also verify by running
1:56:39get log one line and you can see this is
1:56:41our new head now where we added version
1:56:44one in the file and if I want to check I
1:56:47will open the file and you can now see
1:56:49we just have version one changes and
1:56:51version two and three are gone. Okay now
1:56:54you realize that I want the lost commits
1:56:59back. So let's now run get ref log. So I
1:57:04will say here
1:57:09let us say
1:57:14I will say get
1:57:17ref log. Now when you run this command
1:57:20you can see here
1:57:23running this command it actually give us
1:57:26kind of a log but it is not exactly what
1:57:28we see in when we run git log. It is
1:57:31something different. So it has
1:57:35everything that we have done including
1:57:38the reset things including the related
1:57:40commits everything it has. So you can
1:57:42see this is our
1:57:47head.
1:57:49Okay. And then this is what we
1:57:53did by doing get reset hard. This was
1:57:59what we have lost but it's still there
1:58:01in get ref lock because it is like a
1:58:03CCTV camera. It has everything whatever
1:58:06we have done it captures. Okay. And then
1:58:09it also says that we have reset moving
1:58:12to head minus two. Okay. So we have
1:58:15everything. Now we can pick what we want
1:58:19and then we can again do get reset hard
1:58:21by using either the commit id or we can
1:58:27just use the head reference. So let us
1:58:31say this is where we want to now reset
1:58:34where we had added version three. So
1:58:36either we can use this commit id or we
1:58:39can also use the get ref log reference
1:58:42which is head at the rate one in curly
1:58:47brackets. So both will work. So now I
1:58:50can say I will I will press Q to come
1:58:52out and I can say get reset hard
1:58:57get reset hard and I will either copy
1:59:02the commit ID or this and I will run
1:59:05this. Now you can see it is now again
1:59:09moving the head head is now at this. And
1:59:11if you want to check by running get
1:59:16log you can now see again we have got
1:59:19back the changes. So this is what we
1:59:22lost and now we have recovered this and
1:59:25to check I will check my file and we
1:59:27have got the changes we did in version
1:59:29two and three have also come back.
1:59:34Okay. So now everything is back and this
1:59:38is how we can use get ref lock.
1:59:41Okay. So again there's a quiz based on
1:59:44this. Please take the quiz and let me
1:59:46know your score in the comments.
1:59:49Okay. Now a very interesting demo on get
Git Revert
1:59:52revert. And uh here
2:00:06okay. I am not sure if the recording is
2:00:12okay.
2:00:14All right. So let's see this scenario.
2:00:17Uh
2:00:19you are on your last this is your last
2:00:22commit and your head is here and you
2:00:25have pushed your commit to GitHub or
2:00:28your online repository whether it is
2:00:30GitHub, Bitbucket, Azure. Now everything
2:00:32is good but sometime later your team
2:00:35says that login feature is breaking in
2:00:37production. Now in this case can you run
2:00:41get reset hard uh and go back to any
2:00:44particular commit? It is not
2:00:47recommended. Why? Because you have
2:00:49already pushed your changes to the
2:00:52shared repository to the remote
2:00:54repository and others may have pulled
2:00:56from the remote repository. So now if
2:00:59you try to do this get reset hard, it
2:01:01can break others changes as well. So it
2:01:04is not recommended. Now in this case you
2:01:07can try get reward. Now what it will do
2:01:10it will not remove a commit instead it
2:01:13will create a new commit that will
2:01:14reverse the changes made by the earlier
2:01:16commit.
2:01:18Okay. So see we are not going back in
2:01:20the commit history. Now we are going
2:01:23forward and we are by going forward we
2:01:26are actually reversing the change of an
2:01:29earlier commit. Okay. So let's imagine
2:01:32when you say get revert d it is not
2:01:36moving back but moving forward. It is
2:01:38creating another commit E which will
2:01:42undo everything you did in commit D.
2:01:47Okay.
2:01:49So let's see this with a demo and you
2:01:50will understand it. Let's create a new
2:01:53file and commit it. I'm I will just
2:01:56confirm that everything is clean. I'll
2:01:58say get status. Everything is clean. I
2:02:03will create a new file.
2:02:10And let's call it new file
2:02:16for demo.
2:02:19And you can add something.
2:02:23This is a new file
2:02:27and I will commit it. Get add
2:02:32get commit- m
2:02:36new file demo
2:02:39added
2:02:41and if I run get status
2:02:44everything is clean.
2:02:47All right.
2:02:49Now if I check the git history
2:02:52uh I will say the commit history I will
2:02:54say get log
2:02:56one line. You can see
2:03:00we have this as our head the last
2:03:02commit. Now
2:03:06I will again modify the file and commit
2:03:10again. So let's just say
2:03:17this is a this is line
2:03:22two
2:03:24and I will commit this as well.
2:03:27I will say
2:03:33get status
2:03:35and
2:03:36get add and I will say get commit
2:03:41m
2:03:43updated new file demo.
2:03:48Okay. And if I run get status everything
2:03:52is clean. And if I now run get log one
2:03:56line, you can now see here this is what
2:03:59we have here. We added and now we have
2:04:03updated. This is our last commit.
2:04:06Okay. Now
2:04:12let's say I want to revert the last
2:04:16commit. So in this case I can say get
2:04:21revert head or get revert commit id.
2:04:24Okay this is my last commit which is
2:04:26always the head and this is what I want
2:04:29to revert.
2:04:31So I can use just head or say the
2:04:36use the commit id. So let's say I will
2:04:39say get revert head.
2:04:41Okay,
2:04:47I will press Q to come out and I will
2:04:49say
2:04:51get revert
2:04:54head
2:04:56and
2:04:58hit enter. Now you can see here when I
2:05:02say get revert head we have to add and
2:05:06explain why we are doing this. So it you
2:05:10can see some things are already added.
2:05:12It says this reverts this particular
2:05:13commit and it is going to revert updated
2:05:16new file demo. This is what we had
2:05:18committed. Please enter a commit message
2:05:20for your change. And here you can press
2:05:23I to go to the insert mode and you can
2:05:27add something. I will just say reverting
2:05:33and then press escape on your keyboard.
2:05:36Then you can press colon wq exclamation
2:05:41or just press wq that means write and
2:05:43quit and press enter. And this is now
2:05:46reverted. Let us now see what happened.
2:05:48If I check my new file demo, you can see
2:05:53it is now reverted. The line I added in
2:05:56the last commit is have gone. And if I
2:05:59now check my log,
2:06:04you can now see my earlier commit is
2:06:08still there and the last comm the latest
2:06:13commit is actually reverting my earlier
2:06:15commit. Okay, so this is what we are
2:06:18using
2:06:20get revert for.
2:06:22Okay.
2:06:24So you can see you still have all the
2:06:26earlier commits but whatever I want to
2:06:29revert has reverted. Okay.
2:06:33So see when you have not uh you have
2:06:36your local branch not pushed yet you can
2:06:38do get reset. But if you have already
2:06:40pushed your changes, it is in your uh
2:06:42push in your repository online
2:06:44repository and you want to revert
2:06:46something then it's often recommended to
2:06:49use get revert.
2:06:53Okay.
2:06:56So let's see if you can answer this. You
2:06:59accidentally pushed a bad commit to
2:07:01main. Which command is usually is
2:07:04usually the safer choice? Get reset
2:07:07hard. No, because you have already
2:07:10pushed and it can break the changes
2:07:15get revert head or get restore. So we
2:07:19know you can use get revert head here
2:07:24and why we know now because it will
2:07:27preserve the history and will safely
2:07:29undo the changes with a new commit.
2:07:32All right.
2:07:34Okay. Let us do a very interesting topic
Git Stash
2:07:38get stash. So let's say in the morning
2:07:42you are working on a feature branch. You
2:07:44have checked out a feature branch to
2:07:46work on a login feature. But in the
2:07:48afternoon your manager says that there
2:07:50is a production issue in the checkout.
2:07:53So fix check out production issue. So
2:07:56what do you do is you are already
2:07:58working on your feature branch. So you
2:08:01can stash all the things that you are
2:08:03you doing. You can stash your work by
2:08:06using git stash and then switch to the
2:08:10branch where you have to fix that
2:08:13particular issue. And once you are done,
2:08:16you can you have committed and pushed.
2:08:18You can then switch back to your feature
2:08:21branch and say get / pop. It will bring
2:08:24back whatever you are doing and then you
2:08:26can continue from there. Okay.
2:08:30So let's see you once we do a demo you
2:08:32will understand this. So you can imagine
2:08:36another scenario that you are halfway
2:08:38through implementing a playright feature
2:08:40or any any feature and you are working
2:08:43on a feature branch. Nothing is
2:08:44committed yet. You are still working on
2:08:46it. Suddenly you have to due to some
2:08:49reason you have to uh come out of that
2:08:52branch and fix something else is it's a
2:08:55production issue or something. So you
2:08:57can't commit your half-finished work. So
2:09:00instead of just leaving it there or
2:09:03committing half finished work and you
2:09:06don't want to lose it so you can just
2:09:08stash your work by using get stash and
2:09:11then you can after
2:09:14doing other work you can come back and
2:09:16say get pop and you can continue where
2:09:19you left.
2:09:21Okay. So it's just like putting your
2:09:23unfinished work or your current work in
2:09:24a temporary locker and then coming back
2:09:27and taking back and starting continuing
2:09:30from there.
2:09:32Okay. So there are some analog is if you
2:09:34want that working desk, half return,
2:09:37notebook, coffee cup, calculator, your
2:09:38boss says clear desk and come to a
2:09:40meeting. You put everything in a drawer.
2:09:43You don't organize everything. You just
2:09:44put everything in a drawer and then
2:09:46later you open your drawer and continue
2:09:49from where you left.
2:09:51So
2:09:53here
2:09:54your drawer is like a stash where you
2:09:56are putting everything and then bringing
2:09:58it back and continuing from there.
2:10:02Okay. So let's create and modify a file.
2:10:07I will say you can call it anything. So
2:10:11I'll just create uh let me just confirm
2:10:16my working tree is clean and yes I will
2:10:20create a new file.
2:10:25I'll just call it
2:10:28I'll just say stash demo.
2:10:32Okay. And I will say working
2:10:37on something
2:10:40interesting.
2:10:42And I'm going to just if I say get
2:10:47status, it is there. Uh let's say I will
2:10:51also stage this. I will say get add but
2:10:54I will not yet commit it. So it's there
2:10:57in the staged area. Now let's say I have
2:11:01to due to any reason I have to move away
2:11:02from this branch and do something else.
2:11:05So I will before going away I will run
2:11:08the command get stash. So I will say get
2:11:12stash and
2:11:17let's see so what happens is saved
2:11:21working directory and index
2:11:24or this particular.
2:11:26So don't worry about this. So here it is
2:11:29saying updated new file demo. So it has
2:11:32gone there. And you can see my file has
2:11:35also gone to the stash. And now if I say
2:11:41get status
2:11:44you can see I'm I have a clean working
2:11:46tree because all my work that I was
2:11:48doing is now stashed. Okay. If I can
2:11:53also check I can say get stash
2:11:56list. If I run this command you can see
2:12:00this is what is there in the stash.
2:12:03Okay.
2:12:06Now I will say get stash pop. So after
2:12:10you have finished with the other work
2:12:12you can come back again and get your
2:12:13work back by saying get stash pop and
2:12:15you will see what happens here. I will
2:12:17say get stash pop. And as I hit enter,
2:12:24you can see my file has come back here.
2:12:26All the work I was doing before I ran
2:12:29git stash has now come back here. And I
2:12:33can also just check
2:12:36the status. You can see I have this file
2:12:40here.
2:12:42Okay.
2:12:44So I can do this thing
2:12:46and sometimes you may want to apply the
2:12:49stash but keep a copy. So you can also
2:12:52do get stash apply. Okay. So when you
2:12:56say apply so when you say pop it is like
2:12:59apply and remove the stash. But if you
2:13:01still want to keep a copy in the stash
2:13:04you can say get stash apply. It will
2:13:07apply but keep the stash also. So see if
2:13:10I now run
2:13:13get
2:13:17stash list you can see this is clean. So
2:13:20if you also want to keep a copy you can
2:13:22say get stash apply. Okay. And if you
2:13:26want to delete the stash you can just
2:13:28say get stash drop or get stash clear to
2:13:32remove all stashes. You it will remove
2:13:35all the stashes. So use this carefully.
2:13:40All right.
2:13:44Okay. So, we now know the purpose of get
2:13:46stash. It will temporarily save the
2:13:49uncommitted changes and put it in a
2:13:51stash with where it will be safe. You
2:13:54can continue with your other work and
2:13:56whenever you want you can come back and
2:13:59do get stash pop or get stash apply. It
2:14:01will bring back all your uncommitted
2:14:03changes and then you can continue your
2:14:05work.
Git Commands
2:14:07Okay. So all these commands
2:14:10remember whenever you want to discard
2:14:12uncommitted changes in a file or uh
2:14:16recover deleted files we use get restore
2:14:18along with the file name and there are
2:14:21different scenarios if you have not uh
2:14:25staged it and it's still in the working
2:14:27directory only in the working directory
2:14:29you can just say get restore file name
2:14:31if it is staged you can say hyphen
2:14:33staged command hyphen stage option with
2:14:36g get restore If it is also committed
2:14:38then you can also refer to the commit ID
2:14:41and recover it and undo local commits
2:14:44and rewrite history we say get reset. If
2:14:47you want to safely undo a pushed commit
2:14:49we use get revert and temporarily save
2:14:52unfinished work we use get stash. Now
2:14:55don't worry if this is something uh you
2:14:58are finding a little challenging to get
2:15:00everything in a single go. Watch this
2:15:03multiple times. do hands-on multiple
2:15:05times and then you will understand
2:15:07everything and then you will remember
2:15:09everything. You will not have to find
2:15:10out or search okay for this particular
2:15:12thing what I have to do but you have to
2:15:14do hands-on multiple times for that.
2:15:16Okay.
2:15:18Okay. So this question check this
2:15:20question I'm not going to tell you the
2:15:22answer and give me the answer in the
2:15:24comment section. You are working on a
2:15:27playright feature halfway through
2:15:29production goes down. What would you do?
2:15:30So what you will do? You can just check
2:15:32and let me know in the comment section.
2:15:35These are all the commands we have done
2:15:38and a lot more than this. So remember
2:15:40this. You can also just try to remember
2:15:44what will this command do, what will
2:15:46this command do. You can try to tell
2:15:47yourself and then check what you're
2:15:49understanding is it correct or not. And
2:15:52again there's a quiz. Do the quiz and
2:15:55let me know your score in the comments.
2:15:57I will see you soon. Please do hands on
2:15:59and whenever you face any issues please
2:16:01let me know. I will look at all your
2:16:04comments and answer you. So you can tell
2:16:06me anything you are facing. I will see
2:16:08you soon. Thank you for watching and
2:16:11never stop learning.