00:00:00
hey there everyone my name is AES and
00:00:02
welcome to the series on the git our
00:00:05
today's topic is pretty interesting uh
00:00:07
which is a little bit scary as well uh
00:00:10
we're going to talk about the git rebase
00:00:12
I'll walk you through what are the cits
00:00:13
of it what could be the accidents that
00:00:15
can happen with this command but all
00:00:17
under control you'll have full knowledge
00:00:19
about when to use this command when to
00:00:21
avoid this command and you'll not be
00:00:23
scared of it and there are a couple of
00:00:25
scenarios that I would like to talk uh
00:00:27
before jumping into this command first
00:00:29
of all if you haven't checked out my
00:00:30
Channel or haven't yet subscribed go
00:00:32
ahead hit that subscribe button it would
00:00:33
be really really motivational for me or
00:00:35
if you can share this series that would
00:00:37
be fantastic I have put a lot of effort
00:00:39
into this your one share that that's the
00:00:42
price of this course that's it all right
00:00:44
so first of all let me share a simple
00:00:46
scenario so what we're going to learn
00:00:48
about this one this get rebase command
00:00:50
you'll find the entire internet is kind
00:00:53
of divided into two halves and one half
00:00:55
is on the 90% which is scared of this
00:00:57
command and only 10% of the folks
00:01:00
actually love this who have used this
00:01:01
extensively and they really know what
00:01:03
they are doing it's not that scary of a
00:01:05
command but it can it always does
00:01:08
rewrite the history and when you are
00:01:10
rewriting the history you should be
00:01:12
cautious and that's what this command
00:01:13
exactly does I'll tell you a personal
00:01:15
scenario uh when I was working as a CTO
00:01:18
in one of the company here in India and
00:01:20
U in that we were organizing a lot of
00:01:23
hackathons in our company and a group of
00:01:26
students uh came in and they
00:01:28
accidentally ran the rebase command man
00:01:30
wrong way in the last 1 hour of the
00:01:33
hackathon and their entire project got
00:01:35
messed up and they couldn't participate
00:01:36
so yes you should be careful with this
00:01:39
I'm not sharing the horror stories I'm
00:01:41
just sharing what happened there the
00:01:42
truth happened there and uh you should
00:01:45
be cautious about it that's all I'm
00:01:46
going to say and now let's go back onto
00:01:48
the code and walk you through with how
00:01:49
actually it goes on and work with that
00:01:51
so let me share the screen all right so
00:01:54
where we are right now let's go ahead
00:01:55
and check the G status for this and my
00:01:58
keyboard is too far on the left I I
00:01:59
don't like it that way but but what can
00:02:01
I do uh so let's go ahead and go ahead
00:02:04
and say I want to run the git status
00:02:05
everything is good on the master branch
00:02:08
and how many branches do we have and by
00:02:09
the way I love this feature that I can
00:02:11
scroll this and can say I want to learn
00:02:14
or I want to run the command the G
00:02:16
Branch so I can just go with that I love
00:02:18
this uh so we have couple of branches
00:02:20
couple of things going on everything is
00:02:21
well sorted well centered up here let's
00:02:24
go ahead and first run about what
00:02:25
happens when you run the git rebase
00:02:27
command again the red here with saying
00:02:30
hey be cautious about it so this command
00:02:33
can be used as an alternative way of
00:02:34
merging or can be used up as a cleanup
00:02:36
tool cleanup tool because it rewrites
00:02:39
the history and an alternative of
00:02:40
merging because it literally performs
00:02:42
the merging but uh in the merging you
00:02:46
actually keeps your branches uh
00:02:48
separately but it just merge everything
00:02:50
into a single timeline this is how the
00:02:52
output is is something which you'll see
00:02:55
when you rebase and this is kind of an
00:02:57
output you see when you actually do the
00:03:00
Branch merging so yes this is exactly
00:03:03
like merging we already have seen quite
00:03:05
in depth about how the merging Works uh
00:03:07
but now we're going to see the how the
00:03:09
rebasing of the stuff works all right
00:03:12
I'm not going to go through again with
00:03:13
the merging process you are quite
00:03:15
familiar with that whenever we do the
00:03:16
git merge it actually merges the code
00:03:19
into the main branch or wherever you are
00:03:20
merging you can just go ahead and work
00:03:22
with that uh now we'll focus more on the
00:03:24
rebase but I'll walk you through with
00:03:26
the branching scenario as well a little
00:03:27
bit we'll go through with the demo quite
00:03:29
in dep now here's a caution note uh
00:03:32
whenever you are either on the main
00:03:35
branch or in this case we are going
00:03:37
through with the master quite a lot or
00:03:38
the master Branch never run this command
00:03:41
I repeat this again if you are on the
00:03:43
main branch or Master Branch this
00:03:46
command is not meant to be run from that
00:03:50
Branch this command is usually meant to
00:03:52
be run from the side branch that we are
00:03:55
whether that's a bug fix footer uh
00:03:58
feature whatever that branch is usually
00:04:00
this is meant to be run from the branch
00:04:03
itself not the main this is usually the
00:04:05
guidelines which everybody puts out
00:04:06
Whoever has worked in the big large
00:04:08
scale Corporation this is usually the
00:04:09
guideline but again we're going to go
00:04:11
through so again don't worry about all
00:04:13
these big long commands that we have
00:04:15
here we have a really long on so you
00:04:16
don't have to worry too much this is
00:04:18
just walking you through that how the
00:04:19
whole flow actually works but since I'm
00:04:21
with you uh I can just go ahead and work
00:04:24
through with that all right so let's see
00:04:26
what happens so first of all we are on
00:04:27
the master so let's just say what happen
00:04:29
happens now is uh we go through and by
00:04:32
the way we'll be keeping an eye on this
00:04:34
one so this is whole things in going on
00:04:36
and how the things are working with we
00:04:38
are more interested in this diagram
00:04:40
because this is the only thing we have
00:04:41
all right so first of all let's go ahead
00:04:44
and work a little bit so in the index
00:04:46
section we come up here and we say uh
00:04:50
this looks nice oops again keyboard is a
00:04:53
little shifted this looks nice I save
00:04:56
this I go ahead and check the G status
00:04:58
you obviously know what's about to
00:04:59
happen happen there is one untracked
00:05:00
file we're going to go ahead and commit
00:05:02
this this time I'm going to use- am
00:05:04
which is another shortcut of adding and
00:05:06
committing at the same time so we're
00:05:08
going to say updated uh main website
00:05:12
looks good and we have done the commit
00:05:14
now where we are at this position looks
00:05:16
good if I check this one and I checked
00:05:19
my the UI now we can see that uh there
00:05:23
is some work that is being done in this
00:05:25
one uh we have some stashed being on to
00:05:27
this Branch as well uh what this
00:05:29
branches if you can just go ahead and
00:05:31
click on this you can just see this is
00:05:32
the footer branch that we are working
00:05:34
through we're not much worried about the
00:05:35
footer Branch that's okay that's fine
00:05:37
I'm not worried on that Branch uh let's
00:05:40
just say there is another Branch let's
00:05:42
just go ahead and see this one uh there
00:05:44
is another branch which is the bug fix
00:05:47
and let's go onto this bug fix Branch
00:05:49
I'm going to go ahead and say hey let's
00:05:51
check out the bug
00:05:54
fix bug fix and now I'm am on the bug
00:05:57
fix Branch let's go ahead we work a
00:05:59
little bit on the bug fix Branch I fixed
00:06:02
the navigation bar bugs maybe we have to
00:06:06
contact us was not working I will just
00:06:09
cut this out I'll just use an anchor tag
00:06:12
it's not going anywhere this this was
00:06:14
the bug let's just assume I'll just say
00:06:16
contact us now now the bugs is bug is
00:06:18
being fixed all right uh but if I go
00:06:21
ahead and check it out at this point uh
00:06:24
my bug is fixed let's go ahead and get
00:06:27
and we're going to go ahead and commit
00:06:30
and we're going to go ahead and say
00:06:32
let's just say the bug is fixed but I
00:06:34
still need to work a little bit more
00:06:35
onto this one so I'm going to go ahead
00:06:37
and say updated
00:06:38
navbar that's nice there we go in the
00:06:42
meantime I want to go ahead and do some
00:06:45
more work on the master Branch so let's
00:06:47
just go ahead and go ahead and do get
00:06:49
checkout and we want to go on to master
00:06:51
Branch there we go and in the meantime
00:06:54
the master is also working if I check
00:06:56
the git Branch we have this one working
00:06:58
on this one so I guess that's my uh not
00:07:03
in the Commit This one is but we have
00:07:05
updated the main website so this is the
00:07:06
pink one and in the meanwhile there is
00:07:08
some more work on the index file that
00:07:11
was done and I want to
00:07:13
say what do we do on this main website
00:07:17
uh maybe some Gallery was added some
00:07:21
images were added images added so there
00:07:24
we go images is being added and what do
00:07:26
we want to do commit get
00:07:30
commit and same and we're going to say
00:07:33
images added all right now let's check
00:07:36
out the another branch and again this is
00:07:38
kind of a workflow that we have to go
00:07:39
through I'm not making this up so let's
00:07:41
go into the bug fix again and there we
00:07:45
go now when I go into the buck fix
00:07:47
obviously the index file is having
00:07:49
nothing but it would be really nice if I
00:07:52
could bring in the whole code base uh
00:07:55
from the main branch into this branch
00:07:57
that would be really nice and this
00:07:58
happened quite often more than you think
00:08:01
that we go ahead and merge the main
00:08:03
branch into our bug fix Branch so
00:08:05
whatever has happened on the main branch
00:08:07
can come onto my branch and I can see
00:08:09
what's the latest update quite common
00:08:11
thing so we're going to go ahead and say
00:08:12
get merge and this time I want to merge
00:08:15
so wherever I am first of all I need to
00:08:17
check that I'm on to bugfix and I'm
00:08:19
going to go ahead and say get merge and
00:08:22
this time I want to merge the main
00:08:24
branch or the master in this case by the
00:08:26
way the main and master you might see me
00:08:29
often uh exchanging the name between
00:08:32
Main and master there's a history of it
00:08:34
let me share it with you so initial days
00:08:36
when the git was designed everything was
00:08:38
called as Master branch in fact in a lot
00:08:40
of other uh Dev Ops stools like
00:08:42
kubernetes and all these things we used
00:08:44
to call one master and others as slave
00:08:47
and somehow some people find that
00:08:48
offending so nowadays we call everything
00:08:50
as Main and others as regular branches
00:08:53
so that's why git is a pretty old
00:08:54
software still does the main branch as
00:08:56
the master but on the GitHub they don't
00:08:59
want to have have any conflict so they
00:09:00
call it as main branch so Main and
00:09:02
master are literally the same they are
00:09:03
just the branch name okay so let's go
00:09:05
ahead and do the merge uh Master once I
00:09:08
go ahead and do the merging of the
00:09:09
master uh it says hey there's now editor
00:09:12
this is usually the default message I'll
00:09:14
go with this only merge main BR Master
00:09:17
Branch into the buck fix I go ahead and
00:09:19
save this and close it and go back up
00:09:21
here so files are being added now
00:09:24
everything looks good if I go ahead
00:09:26
where I am right now I am onto the bug
00:09:28
fix if I go ahead and check out the bug
00:09:30
fix onto the index all of these images
00:09:33
and everything came into my picture but
00:09:35
what some people don't like is this guy
00:09:37
so now this there's this extra commit
00:09:40
that hey there's whole things going on
00:09:42
now again I can start working on this
00:09:44
I'm still on bugfix I'm still working on
00:09:46
the Navar bugfix and this about us is
00:09:50
the link which is was not working so I
00:09:51
added an anchor tag goes nowhere and
00:09:54
about us so we have added some more
00:09:57
stuff and we worked onto this one
00:09:59
we will be adding a comment message and
00:10:01
I'll say bug more bug fixed or I'll say
00:10:06
about us fixed these are really bad
00:10:09
commit messages but somehow we we work
00:10:11
with that and if I check this kid branch
00:10:14
that again uh Master is doing nothing
00:10:16
and all of these things but in the
00:10:18
meantime you went up and changed this
00:10:20
one yes we need to go through with this
00:10:22
whole workflow so that you get the idea
00:10:23
of how things actually works in the real
00:10:25
life I'll go ahead and do a check out
00:10:28
and I'll check out
00:10:30
out onto the
00:10:33
master and uh get to check out and my
00:10:38
bad need to run Master now we are on the
00:10:40
master Branch we are still working on
00:10:42
index file uh
00:10:45
images prizing
00:10:47
card
00:10:49
added all right so we have added the
00:10:52
prizing
00:10:53
card the throat set still itchy but
00:10:56
anyways a pricing card being added I
00:10:59
want
00:11:00
to uh add this and I'll say
00:11:06
pricing card added all right uh let's
00:11:11
see what's the situation of this all
00:11:13
right now more work is done on this one
00:11:16
so there will be new commits being added
00:11:18
this is how the life is uh now you want
00:11:21
to get all those commits that are done
00:11:23
being done on the main branch being
00:11:26
migrated onto your feature Branch or the
00:11:28
bug fix Branch wherever you are so this
00:11:31
is something which is bothering to a lot
00:11:33
of people that if I go ahead and check
00:11:34
out the git log uh git log and on line
00:11:38
uh you'll notice a lot of commits are
00:11:40
just the merging commits so this is the
00:11:42
merge of the Navar this is merge of the
00:11:44
footer Branch these commits are not
00:11:47
actually putting any work they are just
00:11:50
merging and same thing is happening if I
00:11:52
go ahead and do check out onto the
00:11:56
bugfix
00:11:57
branch and do the git log again why my
00:12:00
cursor went away and I go ahead and do
00:12:03
this again notice here uh the same thing
00:12:05
is happening here we merge this so these
00:12:08
are unnecessary commits that are going
00:12:10
through and a lot of people don't like
00:12:11
it so what do they do uh let me show you
00:12:15
now first and foremost the most
00:12:16
important thing to learn about this is
00:12:18
where you are so never be hesitant to
00:12:20
run the git Branch I am on Buck fix this
00:12:23
is where I can run the command of rebase
00:12:26
if you are in the master and run the
00:12:27
rebase the whole mess is on you not on
00:12:31
me make sure you understand this key
00:12:33
point so now what we're going to do is
00:12:35
now we want to bring all those changes
00:12:36
here so I'm going to go ahead and say I
00:12:38
want to do a rebase all right but rebase
00:12:42
uh needs to run on the master brand so
00:12:46
this is the command that you usually
00:12:47
want to run git rebase main or git
00:12:49
rebase Master this indicates that I'm
00:12:53
rebasing my Branch with the master you
00:12:56
don't want to mess up the master Branch
00:12:58
or the main branch now notice here what
00:13:00
happens when I run this
00:13:01
command rebase successfully and now what
00:13:05
you can see the timeline is being
00:13:07
Rewritten the timeline is Rewritten this
00:13:10
is scary because now the main timeline
00:13:13
is it's literally rebasing what we are
00:13:15
doing is rebasing that means uh we still
00:13:18
have two branches here but this is my
00:13:21
main branch and its base is now here so
00:13:24
this is the point of its base so this is
00:13:26
the most important part we still have
00:13:27
Branch we still have two heads these are
00:13:29
just the pointers which can point onto
00:13:31
different uh points or different commit
00:13:33
IDs but this is my Branch this is
00:13:35
literally you have you have taken the
00:13:38
branch and have replanted it on the
00:13:41
master this is scary this is because you
00:13:44
are rewriting the history at this point
00:13:46
probably you might not be able to
00:13:47
understand the severity of it but notice
00:13:49
here the branch is entirely gone it's
00:13:51
just the master and if any more changes
00:13:54
we will obviously be diverting into the
00:13:56
branches if any more work is done on the
00:13:58
bug fix more branches we'll see those
00:14:00
colors and line it's not like we have
00:14:01
entirely removed it for the future as
00:14:03
well they will exist I'll show you that
00:14:05
but this is the point where things
00:14:07
actually gets a little bit scary so
00:14:08
again uh checking where I am I am onto
00:14:11
the buck fix brand so if I go ahead and
00:14:12
add some more stuff uh maybe I'm fixing
00:14:15
the same nap bar uh let's just cut this
00:14:18
out oops not like that I'm going to go
00:14:22
ahead and cut this out add the anchor
00:14:24
tab goes
00:14:26
nowhere Home tab is also fix fixed now
00:14:29
so let's just go ahead and Commit This
00:14:31
get commit and we're going to go ahead
00:14:33
and say that uh Home tab fixed not
00:14:38
really big fix but maybe that's a th
00:14:40
line code I'm going to go ahead and
00:14:42
you're going to see this in the commits
00:14:44
ID get graph now that uh yes our uh the
00:14:47
fix actually is now the same on the home
00:14:49
branch and Al as well as on the buck fix
00:14:52
but the moment you'll do some more
00:14:53
things on the master Branch you'll see
00:14:55
the commments ID being changed so we're
00:14:57
going to go ahead and say hey let's go
00:14:59
ahead and check out the master there we
00:15:03
go do some changes on the master we are
00:15:05
doing more changes in the index pricing
00:15:07
card what else uh we have y a section y
00:15:12
a section being added go ahead save this
00:15:16
do the
00:15:19
commit y Us section added go ahead and
00:15:25
run this one and let's see the get craft
00:15:29
there we go we can see the branching is
00:15:31
here so it's not like we have completely
00:15:32
removed that you cannot have branches
00:15:34
ever but here is the interesting part
00:15:36
where we have no idea of how the
00:15:38
branching came in bug fixes we have just
00:15:41
merged it and even if you look at the
00:15:42
commits the commits the unnecessary
00:15:44
commits that were there like uh merge
00:15:47
the branch merge the branch those
00:15:49
commits are gone that's the whole idea
00:15:51
about the rebasing and in case you are
00:15:53
wondering what happens when there is a
00:15:55
conflict uh exactly same what happened
00:15:57
in the branch when you merge the
00:15:59
branches when you merge the rebase it's
00:16:01
exactly same if there is a conflicting
00:16:03
file you have to remove the conflicts
00:16:05
manually or use the vs code to accept
00:16:07
the changes and all of that and you have
00:16:08
to run another command so interested in
00:16:11
doing the conflict why not let's go
00:16:13
ahead and do the conflict uh we are on
00:16:15
to the branch of Master uh all right so
00:16:19
let's just say the master has worked on
00:16:21
it let's go ahead and check out onto the
00:16:23
bug fix as well and we're going to go go
00:16:26
get check out and we're going to go on
00:16:28
to BU Buck fix and this Buck fix guy
00:16:32
comes into the index and says uh prizing
00:16:35
card added and he also says a new
00:16:38
pricing card new prizing card added so
00:16:42
we are working on the same file some
00:16:44
stuff is being added which one to keep
00:16:46
which one not to keep it's up to you
00:16:47
I'll just save this because we are here
00:16:49
to make conflicts let's go ahead and do
00:16:51
the commits and I'm going to go ahead
00:16:53
and say uh card added to index file not
00:17:02
added add card to index file uh usually
00:17:06
you want to give the command to get it
00:17:08
on that again uh let's go ahead and work
00:17:10
with that so now what happened is hey
00:17:13
there's a conflict in this and uh let's
00:17:17
just go ahead and run the branch we are
00:17:20
on the bug fix so it's safe to run the
00:17:22
command if I'm on the branch I can merge
00:17:25
or rebase from the master let's go ahead
00:17:27
and see what happens when I try to
00:17:29
rebase uh let's go ahead and find out
00:17:32
the command that we want to run rebase
00:17:33
the master I do this and it says
00:17:36
rebasing couldn't do the rebase because
00:17:39
there is a conflict here and here are a
00:17:41
couple of hints you really don't want to
00:17:43
skip this part read this entire uh whole
00:17:46
message if you're not going to be
00:17:47
reading it you'll not understand the
00:17:49
rebase so go ahead and follow this it
00:17:51
says re resolve all the conflicts
00:17:53
manually mark them as result uh with
00:17:57
hint uh then you have to run the the git
00:17:59
add or remove the conflicted file so you
00:18:01
have to run the command git add yep
00:18:04
that's the thing and then run the git
00:18:06
rebase and my terminal is a little short
00:18:09
can I expand it yep and there we go
00:18:14
whole thing in one line now it's better
00:18:16
all right notice here this is the one
00:18:18
that I want to get your attention to you
00:18:20
have to add this this is the first part
00:18:22
of it and then you have to run the get
00:18:24
rebase D- continue that means the rebase
00:18:28
right now is in the halt phasee you have
00:18:30
to resolve your conflicts and then I'll
00:18:32
can continue the rebase if somehow you
00:18:34
find it little bit scary uh you can go
00:18:36
ahead and use the Das Das abort command
00:18:38
as well to abort the entire thing if I
00:18:40
go back onto this one here notice here
00:18:42
this is the resolve all these things and
00:18:45
you can just click on this accept
00:18:46
changes last time I showed you how you
00:18:48
can do this manually by removing all of
00:18:49
this this time I'll show you how you can
00:18:51
use vs code I'll say that accept the
00:18:53
current change accept the incoming
00:18:54
change or accept the both I'll accept
00:18:56
the incoming change which is coming up
00:18:58
from the Master Branch I'll accept this
00:19:01
this is how it looks like new pricing
00:19:02
card added and I'll go ahead and save
00:19:05
this but that's not enough I need to go
00:19:07
ahead and literally uh come back here
00:19:09
and add this and again you don't have to
00:19:11
commit this you just have to add this so
00:19:13
I'm going to go ahead and say get add in
00:19:15
this case it's just one file but you can
00:19:16
use the dot feel free to use that I'll
00:19:19
just go ahead and add this but that's
00:19:20
not enough now it needs to be only in
00:19:22
the staging area now you can continue
00:19:25
this but if you go ahead and check the
00:19:27
git craft notice here uh uncommitted
00:19:29
changes and all these things it's it's
00:19:31
an ongoing thing so don't just worry too
00:19:33
much about it and we're going to go
00:19:35
ahead and say get rebase and the dash
00:19:38
dash
00:19:40
option dash dash continue thank you so
00:19:42
much and by the way if you h on to this
00:19:44
one it says uh restart all these options
00:19:46
I love this editor uh restart the
00:19:48
rebasing process after having resolved
00:19:50
conflict merging really nice I hit enter
00:19:53
and it says uh add card to the index so
00:19:55
now it's asking you to for the commit
00:19:57
message I'm happy happy with this commit
00:19:59
message I'll save this close this file
00:20:01
and you get that now finally the
00:20:04
rebasing is completed so there is no
00:20:07
such thing as like automatic M conflict
00:20:09
resolver or something but we we get to
00:20:11
see this that whole thing is again uh
00:20:14
just a one timeline so literally we are
00:20:16
actually changing the timeline so it
00:20:18
took us little bit time to actually
00:20:20
understand that how the workflow and
00:20:22
everything is done but I think by now
00:20:24
you actually truly understand what the
00:20:26
rebase is and uh again I would like to
00:20:29
bring you one more thing up here that
00:20:31
again uh never rebase the commits that
00:20:34
you have shared with other people and if
00:20:36
we are pushing to the GitHub never
00:20:38
rebase is usually the idea uh but again
00:20:41
uh it's not like I'm against the rebase
00:20:44
uh you can rebase and some companies do
00:20:46
actually ask you to rebase all the time
00:20:49
some people like to keep their commit
00:20:50
history being very very clean and not un
00:20:52
necessary commit that's okay that's the
00:20:54
workflow you will learn some people have
00:20:56
the have the workflow or their
00:20:58
experience with too much rebasing some
00:21:00
people have rarely have done the
00:21:02
rebasing so it's up to them they are
00:21:04
scared of it or not I am little bit but
00:21:07
I know when I'm rebasing what I'm doing
00:21:09
so that's the whole idea so the whole
00:21:10
point of the picture is rebase is not
00:21:12
that scary it's just you need to know
00:21:14
what you are doing if you know that what
00:21:17
you're doing it's always good idea and
00:21:19
again never do the rebase when you are
00:21:21
on the master Branch always do the
00:21:23
rebasing when you are on the buck fix
00:21:25
branches or some other branches that's
00:21:26
the workflow I've have done but again
00:21:27
your workflow might be different or
00:21:29
something else so that's your rebase I
00:21:31
hope you have enjoyed it if you have
00:21:32
enjoyed it do tweet out this video or
00:21:35
the entire video however you like that
00:21:36
uh let's catch up in the next
00:21:42
video