Git CLI : Git-ting to the Heart of it @ GoLang Kathmandu

October 5, 2020

< 1 min read

Git CLI: Git-ting to the heart of it: Are you using GIT CLI yet?
Have you been hesitant to use GIT CLI? What could go wrong while using innocuous GIT CLI to manage day-to-day activities related to your source code versioning? In this session, we will try to focus on understanding what GIT is all about and how it works. We will also tackle some very trivial scenarios such as:
·  Changing the last commit’s message or changing the author of the commit
·  Keeping a linear commit history
·  Deciding whether you should merge or rebase

View Slides Here


Rahul Kashyap

Senior Software Engineer - JFrog Pipelines

Rahul Kashyap is a Senior Software Engineer at JFrog. His areas of focus include distributed systems, cloud native architecture, and developer best productivity practices. Rahul loves to solve convoluted business problems with simpler solutions, in order to make the world a better place. His personal tech goal is to find the right equilibrium between technology and people.

Video Transcript

history of the same file you can go in time and you know try to track okay what all the changes you have made to it
the same thing can be applied to the directories and uh ultimately all of your files would be
stored somewhere and it follows the version control system which means again whatever changes that
you are doing over the time you can specifically go to a particular version and try to see the content or
try to measure the changes that you have done there so that’s good for you um but
when i talk about git a lot of people have one confusion about it hey i know what gate is now but what do you
mean by you know github or bitbucket then or does it mean that when it is on cli
it is getting on when it’s on ui is it github so basically the main difference lies between the
implementation of it so git is a system which can be implemented by any of the users so you
can also implement something on top of grid and you know uh put it as an offering to anybody as an open source world you would like
to have more and more users using qr implementation so that’s where you see a lot of
competitions are coming up you know with respect to github bitbucket git lab and so on
so in general github or bitbucket target lab they are hosting servers which implement
gate of course and they offer you a nice ui to manage things for example you have the features like
requests so git doesn’t have any concept or pull request rather this implementation offer you the same
and how do they manage it it’s up to them they also provide you issues so you can raise an issue
against a repository saying that hey i don’t think this is working right but can you have someone look at it so those
nice uis you know working i’m sorry yeah working with respect to
issues is provided by either the server hosting implementation but not from the kit
so git is just the version control system which on top of you can write something as an
implementation and give it to people so that’s how try to explain to people you know how
github or kit are different in nature i hope that clarifies things but
still we have this as a hesitation that hey i don’t want to use gate cli
because maybe i have seen the obnoxious side of using it and then messing around with some of the things
and things stopped working or maybe i lost my changes i lost all the code that i had written
in seven days and now it’s gone so that’s true because personally i also
started using cli i think last year itself before that i was using some gui
tool to manage all the basic operations like pull push rebase and so on so
so this motivation up to ucli was very i would say funny because once i
was working with my co-worker and he said uh hey dude why don’t you cli and i was like uh
personally i’m a little bit afraid of using it because maybe i’ll mess around things
and then he told me that if you don’t mess around things you know you will not learn you know how to
rectify that and that really helped me to get started with the same thing i mean i
do that for other things but git cli was something that i didn’t really want to mess up with it’s it’s
really uh it can become dangerous that hey whatever you did for past few days everything is gone so
maybe that kind of fear that you have it’s natural but
uh if you go in the right direction of learning it and how to use the cli i think things
can be simplified have you been said that i think what important is
to learn concepts instead of learning the commands so uh if somebody say that hey there are
five commands that you need to learn about using it that okay get push pull check out and all
no you’re not gonna maybe scale this knowledge you you will end up having some confusions and ultimately you’ll switch
back to some ui again or personally the the uh the uh
i i would say uh my life lesson what i have learned in hard way is we
should focus on what instead of how so if we really get to know the answers of
what we need to do then doing the how part is easy you know how
to do changes that becomes easy and half the battle is already done so that’s kind of a i would say
mantra i follow in my life and uh again helped me to learn this git flow get cli things
very efficiently so i hope uh with this session also i
would be able to provide more insights on what need to be done or what the concept means and then we can
all contemplate about hey how those things will work all right so moving on um
so now i’ll start some of the uh pictorial representation of the things so that things get interesting it’s
basically when we talk about git we are talking about two different things which are very important right we
are talking about a remote repository in general and we are also talking about a
development machine or a developer’s laptop desktop or whatever now
how these two these two things are related to each of them so remote repository is something that
maybe you have created on your own or maybe you are interested to contribute to for example you you went to github and
you saw very useful uh framework or a library or anything
now you want to contribute to it so how do you do it you basically try to get the same thing on
your local machine try to do the changes and then try to get those go those
changes shared with others as well right how these things can happen there are
many ways to do it but we will talk about how you can make yourself get cli to do it
so if you see this diagram you see two big boxes one says dev uh which is representing
your local machine another is saying remote which is not on your local machine but rather on the
uh remote side of it so ultimately on the remote side you have a repository
which you maybe you have created or you have you you are you have you want to contribute to that
repository too so on the dev site if you look at this box it contains three things and
this is very important to understand that we have a working directory we have the staging area and then we have a local
depository so how does these uh three pieces uh like work together will
come to knowing in the following slides so if if i ask you to you know start
contributing your to a repository so what what is the first thing that you do you will say hey i want those things in my
local machine right so that i can maybe add a file or make some changes in some of the files and so on
so what we do is we do something like clone our repository so what we do is git clone and the
repository url and internally what it what does it do is it close or it takes all the content of
the remote repository and actually puts in two places so one is in the working directory and
another is in the local repository so all all these three boxes have their semantics associated to them
and they work very efficiently with this kind of an architecture that you will come to know
or that you will realize in in following slides so bottom line is when you do kit clone
what happens in background is the files that are stored on the remote repository are let’s say copied
to in two places one one is in your working directory and another one is your local repository how
do we connect these two let’s see you know following slides so once you have cloned the repository
what’s the intention behind it you want to make a change right you want to maybe add a new file make a change in some of the files or
maybe remove a file so let’s try to depict a scenario here
uh let’s let’s consider that your remote repository has one file in it called ls.txt now you have already cloned the
same so you have that change in your working directory right now what your intention is to actually
create another file which is bob.txt so that’s why you see a plus sign in here which represents that you have added
this as a new file now in order to make the intended work done which is
share this bob.txt with rest of the world it needs to be a multi-step uh
process so how does it look like and how it actually works with respect to
multi-step things is first of all you have to change with you so nothing will be directly shared with
the uh external world it needs to go with one stage to another and another from another
so first thing is you need to actually put this changes from working directory to
actually uh you know stating area and how do we do it is very simple what you do is
you get add and whatever the file name that you want to add to stating area is this that’s it
so what you do is get add bob.txt get takes this file from working directory
and pushes it to staging area now what the staging area is it’s a
place in which you want kit to track all the changes or it’s a place where all the track
changes are present so right now when you added a file in your working directory
it internally means that you have an untracked files git doesn’t know about it yet but once once you add it to
a staging area git now is tracking this file you know it knows about the changes being
done to this file so this is one step to your uh the whole process the other step
would be to actually promote this change to local repositories you know so that this guy mirrors the
remote repository somehow and how do we do it is something we call
so what you do is you just simplify uh simply put a command called git commit hyphen m
providing a beautiful commit message representing what exactly the change could be what
you’re saying is hey i have added a bob.exe file and what internally it does is uh
whatever you have in the staging area it’s gonna make it as a commit
and it’s gonna push it to your local depository that’s it now still the changes that you have done
are not shared with the rest of the world if you want to do that you have to do the final process to it a final step to
the process is pushing the changes okay so once you push the changes from your local
repository to remote repository it’s gonna be available for everyone and how do you do it is
let’s say for simple use cases just a good push command so now we understand um hopefully we
understand that hey what do we mean by you know running each and every command
how does one chain transist from one state to another and uh yeah
that that’s how you do your first push so once everything is done you can see that what
you have in your working directory is also represented in the remote repository
so that means you have a middle copy of everything that means yeah like you are done and by
pushing your changes you have pushed your you know changes to one of the branch
existing in the remote but uh i just mentioned about
something called branch and it’s also very interesting concept
of great you know if we understand what a commit is we can really understand what a branch is as
well so basically this is a pictorial representation of a tree actually but it doesn’t look like
a tree though so what happens is we have series of
comments arranged in some particular order and we try to associate some pointers to
any of the comment and those pointers could be representing some of the things so in here if you see we have so first
of all this commit was added 25 e1 for 3d after that we added another commit on
top of this which was 745 and so on then another commit and another commit
now what we added as a pointer was this called master that means we have a
master branch which is pointing to a commit and having said that it should also
simplify the reasoning behind the concept of branch which is branches are nothing but the pointers to
very specific order of the commits or comments which are connected or arranged in specific order
so you see there is a comment arranged in some order uh and there are two branches
pointing to it so one is the master branch and another is a branch called branch foo
and there is also some another pointer which is a helper pointer pointing to master right now so
generally branches don’t move from one comma to another just like that what we do is we move the
another helper pointer called head so that will come later but just for simplification of this
consider that branches are nothing but pointers to the comments uh
with that let’s look at the other concept which is merge
okay so what what’s the motivation behind merges so if you remember the first use case where
we wanted to share our changes with the rest of the world what we wanted to do was to push one
certain file to the remote repository that works if you’re working with only one branch you
know consider that you’re working only with the master branch and when you make another commit you just move this pointer from this
commit to another comment that works but in reality you don’t work with only one branch because you might
be interested in doing parallel things in maybe some breaking changes happening with some guy and
another one is working on some other things which is very cool stuff and you don’t want to disturb the master
branch itself so that people looking at the master branch should only get these stable changes
okay so that’s where the concept of branches come and ultimately the motivation of having
a branch is that at the end of the change is done maybe you want to merge or you want to
share those changes in the master branch itself that’s being said
consider that somebody has created a branch called branch foo which was taken off from this commit
called cebe4545 and what they have done is they have added a new commit to it
now if you imagine this as a tree that you have branched off something from him and added another
commit to it and now your branch is pointing to this new comment what your intention could be that i want
to merge this change which is 270 3866 commit to actually the master branch
okay so when it comes to merge there are various approaches to merge and the most simplification approach of
that is called fast forward merge what happens is when you merge very
simple changes in which you see that master pointer was pointing to this commit earlier
and you just have another branch created having only one commit so if you ask it to you know merge this
injury is saying hey get please merge whatever is there in my branch
with a branch called master what is go git is gonna do is it’s gonna
simply see if it can merge the changes automatically and if it can that approach is known as
fast forward merge so what it’s going to do is promote this master pointer
from this commit to actually this commit and that’s it the whole commit history follows so
master is pointing to latest comment which is 270 3866 this points to another comment to
another to another and so on so whosoever looks at the commit history will come to know that hey
these were the changes being done on master branch and there was another comment on different brands but this got merged to master so
all the changes available in branch four branch uh well it’s recursive but okay so
branch called branch foo is available in master as well so that’s really simple
but generally it’s not the reality as well what happens is uh people try to work in
parallel okay and they work in n number of branches okay and they will have
n number of commits as well and that’s where the inter cases start to emerge so
consider the scenario in which somebody created branch foo
from this commit and they added another comment which is 270 3866
now when this guy was working on branch who somebody actually merged or let’s see
added a new commit into branch master so now the master branch is not pointing
to this commit rather it is pointing to another comment right and this is how our real world looks
like you can’t stop someone to merge something in master unless you’re done
it’s not natural right somebody will be working on their pace and maybe that task is small so they will be
merging as soon as they are done so if you ask it to actually merge these kind of
changes into one gate is how gate will be having a hard time to do it
and what we call the situation is that your branch has diverged from its
original nature because right now the tree doesn’t look natural so in order to merge the diverge branch
this is a very you know complex thing to happen from the git
site but it’s very important to understand as well what kit does is if you happen to merge
these two changes into one it is going to create a new commit actually
which points to these two children okay and now you can follow the changes but
with only one exception of one caveat which is now you see this guy has two parents
c e b e five four five has two parents to it like which is seven five zero three eight four zero
four and two seventy three eight six six and also this guy which is a new commit
it has created called merge commit is pointing to two children now this is again a simple scenario but
in real life you can have a lot of these cases where merge commits are
pointing to let’s say three or four comments and you do not want to you know see that and maybe try to
understand what’s happening so what do we do so just like we have merch
we also have a concept of rebase and this is the most uh confusing part
of the system which i have seen people making mistakes in they sometimes forget about here what’s
the ebasis and what the merge is but i must say that idea behind that is
very simple so much we have already seen that how does it work okay but rebase is really interesting
and what the idea behind rebase is you really want to keep a linear commit
history linear means that only one direction you have all the commits pointing to
and in order to do the rebase the process looks very simple what do you want to do is
you say that hey i have a branch which has one commit to it but you know it was branch of branched
off from this commit but i want to rebase from the master now the master is pointing to a commit
called 7503804 and when you run this command called get replace origin master
what happens is kit is gonna replay your changes commit by commit
after this point so git is gonna say okay i know where do you want to rebase from
which is this whole series from here to here but in order to rebase i will try to
replay or you know apply your changes one by one commit by commit
after this guy since branch foo only had one commit to it
after get applied it changes it just place that commit after the master branch commit that’s it
you see the linear history how how beautiful it looks like and
how linear history can also give you the picture that hey okay i know how the changes actually
are flowing in the system apart from that whenever i speak about rebase and merge
to people people always prefer rebase you know in their even production
scenarios they they try to keep the committee as clean as possible
just give me a moment i’ll just drink quarter
okay so yeah say i’m saying that three basis really
helpful it gives you very clean and linear history commit history and
this is also most preferred merge strategy i would say over original merge
so this is yeah another interesting thing which is
if you again recollect the scenario in which you happen to do the rebase
what do you do after that is push your local changes into remote right now if you try to do
git push you’re gonna see this kind of an error which says hey i can’t push to remote because the changes that you have in
your local doesn’t look like the changes which are present in the remote and changes doesn’t mean the you know
change is happening to the files what it means is the git tree which looks
in your local folder your branch it doesn’t match the great uh get tree which looks in the remote
so in that case what happens is you you can actually avoid this warning
and it’s not actually the warning it’s it’s an error so can’t really ignore this and proceed
further but you rather can suppress this and how do you do it is you can do
get push with force but this also has some caveats attached
to it you can use the force push by saying get push hyphen if in force but
you should also understand about the consequences it’s gonna do to you
very basic thing is when you uh when you perform uh rebase or merge what it what
it does is it changes the tree as we all already have seen right so the tree
looked like this then it became this maybe with merge thing with rebase it looked like this
so it doesn’t represent the tree which is on the remote side but when you push with force
what it does is it’s gonna replace the tree on remote from the tree which you have in your
local so it’s very very dangerous you know if you do get force push
whatever tree get tree that you see in your local machine is gonna be the same in your remote
so you have to be really really careful while doing that because what can happen is maybe you
remove the comet from the tree and it was very useful now when you force pushed it
you’re gonna lose that comment okay it’s even you won’t be able to find that in your remote
so again a warning uh when you use get push force
with force flag very be very careful so that two is related to uh like phase
one of the session meaning how to push your changes to remote what we are also interested in to get
the changes from remote to local so if you recollect your diagram again
we had three uh components in your dev or three partition of your dev machine working
directory staging area and local repository and i said that local repository is kind
of a mirror of what you see in your remote repository which is true but it also has some of the internal
representation of the things which you might be able to see now okay
so what what happens is you have let’s say certain branches on your remote repository when you initially cloned it what kit is
gonna do is create two sections inside to uh inside your local repository called branches and remotes so
it’s gonna attach a pointer from your local branch to the remote we say that hey whenever
you push changes to your local master branch it’s gonna be represented inside origin
master branch so origin is the remote and master is
the branch there and this is having a connection to the master branch present in the remote repository
similar to master there is another branch called foo like branch hyphen foo which also is connected in the same way
but if you see uh when we cloned the repository this was the case but now
somebody created another branch in the remote called branch bar now how do we get to know about this guy
so there’s a another like there’s a command called get fetch and what it what does it do is it’s
gonna fetch the branches on the remote but it’s gonna update only one section of your
local repository called remotes which is that there is a branch on remote and i know
what uh like how do i know that branch it’s because origin
slash branch bar so if you wanna learn more about emotes it’s very interesting topic as well
uh it can also become lengthy uh you can google it you will really find interesting that how can you have
different or more than one remotes attached to a repository and how or some of sometimes i
i had people asking me hey that hey this origin does it mean a keyword that
is that is reserved to get and that’s not the case you can have anything
instead of origin as well so if you do get fetch it’s gonna only
update the remotes part of it but it’s not gonna give you the branches
section so if you want to if you want to let’s say you’re you’re working in master branch right now okay
and you want to switch to another branch which is branch bar it will not allow you to do that because
it doesn’t know that this kind of branch exists what do you do in that case so instead
of get fetch what you do is you do git pull okay
and it does whatever git fetch does but it also uh updates your branches
section so it updates the link that hey there is a branch bar in your local repository which is pointing to
branch bar in your remote and that is connected to your remote deposit directly like that so uh i think
that’s how you differentiate between git fetch and get pull based on need what to do is something that you need to see
okay so when you do get pull it also runs some things in the
background right what it does is it tries to actually see how many changes that you have done
in your local remote repository and it’s gonna try to merge those changes in your local
repository okay and when i say merge it could happen that you have five comments right now in
your local repository but remote has seven now it’s gonna merge those two comments somehow and by
somehow i mean if they are diverse again you know that get merged is gonna create a new merge
commit to it and then you will have multi branch kind of a tree
since we have already know uh we already know that there was an ebay flag that we could use
you can also you know influence this behavior of get pulled that hey don’t
run get fetch and get merged rather use git fetch and get rebase so to use k3 based kind of functionality
while you’re pulling the changes you need to supply that flag so you can say get pull hyphen iphone rebase or
even hyphen r for short it’s gonna pull all the changes from remote repository to your local
but it’s gonna apply the rebate strategy to merge the changes that you have on your remote to your local
well that that’s really interesting to know but in real life what happens is you are working on some
of the changes okay and you are in mid middle of something
very important and some of your co-workers say that hey i have pushed some changes try to
just pull them and see if those work for you and this is how things can become
awkward that hey you are in middle of something you have your changes unsaved how do you kind of
you know put them in a temporary location and then pull the changes from your remote and
then again try to reapply or local changes or unsafe changes so this is where git stash comes it
comes into picture so some of not some of but most of the times these are the commands that you
would use but instead of commands we we are targeting to you know understand what is git stash okay so
the most of the times i have used git stashes i did some of the
changes which i need again and again but i do not want them to be pushed on remote
i save those changes in a stash and i try to reapply those changes in my
current change so that you know i’m done i mean i apply those changes directly on my
changes and i do not share with them uh with anybody so very basic things to do here is you can
create a stash with this command called git stash save and provide a nice description to it so
that when you do get stash list it’s gonna show you how many stashes you
have saved and what are the descriptions to them if you just use git stack it’s gonna create a stash for you
but the message will not be nice for you you know you won’t be able to recollect here what does this change mean
so when you do get stash save or get stash it’s gonna create a
stash stack and on for the stack we know that the property is
all the changes which are done on the latest would be the ones will be applied first so
when you say git stash pop it’s gonna take the latest or the last change happened on the git
stash stack and it’s going to apply the same to you to your current working directory but you can also
modify this behavior saying that hey i want to apply very specific index of the change
into my current changes so they can do that with apply command so when you stash when you apply the
changes from stash to a current change within index and an apply command the
stash changes will also remain in a stack but when you do pop
the changes are gonna vanished from your stack and those would be applied to your local
current state of the repository so again a long story short if you
are in an awkward situation where you want to temporarily temporarily uh temporarily park your changes get a stash to the guy that you
are looking for also another interesting topic is cherry
picking which means that uh you want to share some of the changes not the whole branch
with any other person so let’s consider the scenario in which you have this
simple tree okay looks complex but trust me it’s very simple so let’s see uh how to change this went
like this somebody created one first comment like 2 5 e 1 4 3d then another commit another then
another and at this change two people created two different branches so one created
branch foo another created branch bar while they were working in their branches somebody
also pushed change in the master branch which is seven five zero three a zero four now
okay now it could be possible that
the guy who’s working on branch foo uh found out that hey he needs some of the
changes which the guy who is working on branch bar maybe he’s also doing so instead of
duplicating the work here in this branch he will ask this guy that hey when you’re done
just make a comment of it and i’ll there and share that commit to me so what it what the intention behind
that is the changes will be done only once and then you will borrow those changes from
this branch to this branch or vice versa so in this example what we are going to see is you are on
branch 4 and you want to borrow the commit called a216e0 which is
yeah this one so you want to borrow this commit from another branch to your branch what you need to do is just do the
simple command get cherry pick and the commit number the changes are gonna look like this
so this comment is now represented in your branch as well but the commit number will be different
because you can’t have same commit number being referred in multiple branches so
commit number is different but the changes are exactly the same and this is one of the most useful
feature asking people using you know to borrow the changes or maybe applying the changes from one
branch to another and when they when they realized that if somebody was work was working on some
bug fix and they also need that fix on their branch for the feature to be completed
they asked they just cherry picked that bug fixed from another branch to their branch and they’re done all right
very interesting topic in gate called rewriting this tree and important and at the same time
very complex to understand if you don’t get it uh in one shot well that’s fine if you
don’t get in one shot but if you if you uh you know
if you forget what do you want to do then believe me it will be very hard to
do the changes with respect to writing or rewriting the history on just you know firing some of the
commands it’s not gonna work uh maybe at all or maybe you will
end up having uh some of the uh unwanted state in your system or maybe
you’ll end up ha you know hating it but the question is why do you want to
rewrite the history and uh i just mentioned few points which
i thought people you know make as an excuse that hey
i want to change something in my past comment because of this this this it could be any other xyz reason that
you want to do that okay so it’s not very limited to this uh this list uh very
very often i have seen that people didn’t write a good commit message and then they have got uh uh you know
the comment from someone that hey please write a good commit message so that it depicts
exactly what this change would be or people working in multiple um with
multiple git accounts get users they forgot to switch the user and now
they have a comment representing another user as the author and they want to change it
so all of all of these scenarios i have listed these are very trivial but again these
are the these are not the only reasons why do you want to change the history
so uh changing would mean that you want to change either last commit
or the commits behind the last commits or any any comment that was in the past
okay so
changing the last commit is very easy what happens is
if you know that you are on a state in which you just committed something
or you just pushed your final commit and you realize that hey there was a
mistake in that commit you can easily make the changes
what you need to do is just fire this command called git commit and a flag called hyphen iphone
what it does is it like takes you to or let me put it in this way
by default what it does it it asks you that hey did you forget to put a nice message
to your commit if yes then please write a new message to me uh now so it’s gonna open up a new or
your favorite uh text writer or git uh configured editor
and it asks you for very uh brand new commit message to your
commit if you’re not interested you know in changing the commit message you want to
keep the same either you can save the file in which or you can say you can just you know press
uh enter or you can just say that hey i don’t want to change the commit
message please skip the screen or you explicitly specify hyphen iphone no edit flag
in which you won’t be asked to uh change the commit message but what
exactly what do you want to change in the last comment is you maybe you forgot to add a file so
you add a file in your staging area with git add command and then you fire git commit hyphen
fundament and you’re done your your past comment will now have the
new file but as i mentioned earlier maybe you forgot to have
the correct author for the comment so how to do that is hyphen knife an author flag will be your
uh helper guy here you know so the format is you you should have your username
and user email provided in the angular brackets and very interestingly if you wanna
update the time of the commit you can also do that you can fake that hey i did that comment
at this time not this time so you can provide any iso eight zero eight six zero one
format in hyphen iphone date flag so very simple changing last commit is okay
simple but not always you would want to just change last comment what you are
interested maybe in is changing the past history okay like you wanna
change more than one comment or you wanna
maybe uh remove some of the comments from your history and so on
so it also has very nice functionality to do that it’s called it’s called interactive
ebase and it’s really simple to do if if you know
what needs to be done otherwise it can become difficult so the way you can do interactively basis
like syntactically you can do it in different ways okay so consider that you are in a branch you have made several
comments now you want to make some amendments to your commits so what you can do is
a gate rebase from master branch which is present on origin and i want to start the interactive
rebase with hyphen i flag there so what uh
kit is gonna do is it is gonna place the pointer right now on the master branch and it’s
gonna replay all the comments one by one as we as we as we have seen earlier
but with the interaction interactive rebase flag it’s gonna show all the changes that you
have done in your branch and which are not present in master branch
and it’s gonna give the decision back to you that hey do whatever you wanna do so specifying a branch is one way to
start interactively based another way is to specify how many comments do you want to go
behind see what you’re saying here is hey wherever the head is pointing right
now i want to go four comments behind to that and start the interactive debates another way to start
interactively base is specifying the commit hash itself okay so you can just say that hey find
this commit id and start rebasing interactively so when you specify hyphen
i flag the next screen that kit is gonna show to you would be like this
okay looks very big and scary but trust me it’s very simple
so what what it depicts right now as you have one two three four five six
seven eight nine so nine comments were present in your branch when you started the rebase process in
in any manner be it a branch uh interactively based head and some few comments or
let’s say a commit id itself so let’s say you are you want to rebase
your changes on top of master branch and after master branch these are the nine changes that
you have done but what i am really interested in doing is to change some of the things in these
comments for example if you see i added a file called alice.txt
then i added some text inside that but while i did that i realized that i i forgot to put some
info important information inside alice.txt
so i made another commit uh providing the missing information
but ultimately on the remote side you will see all the changes there but from the commit side if if you see
maybe you think that hey maybe if i can you know change some behavior on this
comment would be very good then you added a file called bob.txt but
you see we did a typo in the comment message not good to look at when you look look things back then
somebody actually pushed changes in in in your branch saying that a very weird uh you know comment message
maybe the comment doesn’t have any changes at all it’s just a dummy commit message commit having no changes
but just this commit message and what you want to do now is maybe to get rid of this somehow
then you added a comment which uh which is having wrong username okay so you know now a
uh you amended this commit saying that i don’t know how to change the user but
i know the user was wrong and there was a comment that you pushed by mistake so
these are the commits which happened in order top to bottom for shop now what our job is to
do all the amendments that we are interested in and what gate us you know shows you is
the commands that you can use to rectify those mistakes so i think
from starting from pick to fix up and including drop
these are most useful commands i have seen people using including me so what the default thing you see in the
first column is pick so big means is you want to use this commit in your branch as well
okay this is very simple but if you you know uh replace this pick with something
called as reward what does it mean is you want to use this commit but you know you want to edit the commit message so and
from our situation we have seen that we had this kind of a requirement somewhere okay then we also have edit which means
that you want to use the comment but you forgot to
add some of the changes in that commit so you you want to rectify those things
maybe you forgot to add some of the text inside the uh file maybe you want
to change the author at that point of time and so on so if you say edit and place it in
any of this comment when git applies these comments one by one it’s gonna
stop on the comment which says edit and it’s gonna behave like uh as if you are changing your last
commit and changing the last comments is that easy you can just say
it commit an iphone 8 and that’s it and when you continue rebasing it’s
gonna again apply the change after that you have uh the comment another one which is
squash my favorite what happens is you have added a lot of comments
but you do not want those micro comments to be available in your branch what you want to do is combine all of those
changes into one commit itself squash is the guy that you’re looking for
uh but when you do a squash it’s gonna combine all the you know uh commit messages into one so
let’s say if what we were to do it was we’re gonna squash uh these
this last four comments into one so we will put squash in in front of
them oops and what it’s gonna do is it’s gonna create a commit message
having all these four lines separated by slash n
but maybe you want to squash them but have a nice commit message to it so
the guy next to it is just the same thing that we want to do is fix up so you want to combine the
message combine the comments into one but also want to replace the comment message fully you want to say that hey i do not
want the these combined commit message rather it should be like this
another favorite is drop if you do not want one comment to be part of your branch at all you just drop it you just
say hey instead of pick its drop and you are done so i think after
this if you guys are with me you should have this kind of a situation
where you want the intended changes to be done so we wanted to have the ls.txt file we
wanted to have a text inside the list.txt but you know when we forgot to add a few more text inside ls.txt we do
not want this commit to exist at all what we want is to combine this commit with the previous
one itself what we want is the changes happening in b4 bbf f8e to be available inside b8
c2f937 itself that’s it so it says that just one commit saying added text inside ls.txe
that’s it it has all the text what it needs to have then we wanted to change the commit
message of this guy because we made a typo so of course reword then we wanted to
have this uh comment called that’s why it’s pick pick we didn’t want to have this guy into our
commit history so we dropped it we want to drop this comment
since the next comment is something which we want our username to be changed we
have done edit so that when git applies all the comments one by one it stops here and we do get submit hyphen
if and supply the correct author to it
and last but not the least we do not want this commit to exist in our branch at all so if you
save this the base uh history or rebase specification and you just
proceed further there are no conflicts what you’re gonna see a commit history like this
and trust me it’s it’s the most beautiful thing that you would like to have in your commit history
and also in your repository right that each comment represents what would be what could be the best
thing uh that they depict uh happened to them yeah there could be improvement with
this commit for sure that commit with wrong username might not be the correct right now so you
might want to also sway this commit message to something else but to get that just right and this is where
i have seen people struggling as well they do not know when to fix up when to reward when to squash
and sometimes they lose commits and their rebase process go in haywire
so people avoid it with git cli and maybe they ask someone else to or they
don’t do at all simple so one caveat i would like to
highlight here is when you need to do fix up or squash you need to have a commit called pick
just before that meaning when i did fix up in this third comment i needed to have a commit before that
which was pick otherwise it wouldn’t have worked so i cannot fix the fix up but again there should be a big commit
somewhere top or yeah top of the tree so that’s one and another
is you can actually do fix up to all the comments but you must have one comment which is pick
see that’s one of the caveat using squash and pixel
all right so that’s where we have seen how can you influence a history and how can you make your mistakes and
be happy about it in the future having said that about history uh
it’s also applicable for when you are working with open source projects and uh as i mentioned that you can also
force push uh when it doesn’t allow you to push because of the tree being different in both of the uh
your local repository and your remote repository uh so how do you change the history in
your open source projects so the guideline is you should not
so if you have done some mistakes you have to live with those mistakes because
what hap what can happen is you have a history and somebody is working based on that
history okay but when you update the history the history on on the someone’s computer is gonna be screwed
up and they they will have some commits maybe they are pointing to some other location which that does not exist at
all okay so you sh when you are doing git force as i said earlier you should be
very very careful and with public repositories you should not
do force push at all but if at all it is required to do
there is another sibling of force push called force with lease which actually avoid race conditions
meaning uh let’s say you are changing the history right now
based on some commit and in between somebody pushed a commit to the branch that you were working on
and now when you do hyphenation force it’s gonna overwrite the history and the commit that someone has pushed is
gone but if you use force with lease it’s gonna check for these kind of race conditions
and it’s it’s gonna tell you that hey you are trying to do force push but trust me you do not want to do it right now
because somebody pushed a change that you might want to consider in your local as well and
the way you need to move forward in that case would be to do get pull first take that new change
and then do force with leaves flag again again uh mentioning the same thing uh
should not change the history of open source repositories but it’s still if you have to do it
forced with lease is something that you must use all the time okay so uh
writing history something but you can also read the history okay and get also stores each and
each and every activity that you are doing in your in your local repository or remote repository
in terms in terms of a ledger which is like commit change by change history of things and
storing those kind of changes in into local commits so how do you access
those things as with get refloc refloc command this reference log
so it’s the mechanism to uh store the changes or maybe read
those changes uh stored by gate in some emergencies so the usual
uh use cases i have seen for uh reflog is you started your ebase process and you
know that you have screwed up somewhere now you want to go back to the time where you started your
rebase at okay so if you just do get real vlog it’s gonna show the
history of each and everything that you did including get rebase when it started get rebased
when it was aborted on finished and so on so you can take that commit id and just to get reset at this point
what git reflog is very interesting to use for is also with respect to time reflux you
want to see that how many changes that you have done since yesterday so hyphenation since
flag if you specify with gif gate reflog it’s gonna do the trick so again one of
the very useful feature if you know what to make use of it and how it’s very simple you just go to
the documentation of gitrat log and learn what different flags you can make use of
and yeah then you’re done with that so with this i think uh
i moved to the closure of the session and at this time i would love to take
any questions if you have
thank you any questions please go ahead
i have you know one question uh regarding the right clock so so does it have uh the log
for uh four screws if you do four screws uh does it save that as well uh it depends
so based on different configurations of git it may and may not be there because force pushes like you are
overwriting the history with all the things that you have in your local so it might also lose the comment but
again that’s being said if you can try it out and see if your machine is or your your good setup is supporting
having also the the comments which were lost in first push
uh any anyone else has any question
uh like what is the difference between uh move fast forward and fast forward news can you do that and
thank you for your time if i understood your question so
you wanted to know the difference between the fast forward margin the normal merge yeah okay it’s a fast forward merge as i
explained was when git is able to recognize your changes and it’s able to merge those changes and
by changes i make i mean comment uh commit so if it is gonna merge
comments based on the history or based on the commits happening on the
branch itself if it can’t do it on its own it’s gonna the name of this approach is fast
forward merge that means if the master branch that you’re trying to uh
you know push your changes to right now according to this diagram has not diverged at all so nobody has
made any new commit to this and you wanna merge your commits from your branches to
master branch gate is gonna make use of the strategy called fast forward merge and it’s gonna just move the pointer from
this commit to the commit that you just have added to your branch and now it’s part of the master branch
as well so this simple approach of kit merge is called fast forward merge when
it is able to merge the changes on its own but having a complex scenario in the picture where
the branches were diverged it’s not very simple forget to identify the
changes because it doesn’t know which could be the mast uh of the parent and
which could be the children potential children it’s gonna ask you maybe or it’s gonna
create a new comment uh and it’s gonna point to all the children
which could be there so this is not very simple strategy to use and get
uh so this study is not fast forward merge rather just merge i would say and in
this strategy there would be a new commit created called merge commit and maybe you have seen
that when people merge pull requests in the repositories it’s it creates a new merge commit having all
the changes done in the branch also being done in the uh master branch as well so you copy all
the commits from your uh what is your master the feature branch to the master brands
and also a new commit called merge commit which actually representation of connecting these two trees
together does that answer your question
thank you all right thank you
just one question on chat can you give some tips on any shell gift
configuration that helps in its cli workflow um
yeah of course uh one of the thing that i like with git cli is it has something called as hooks
so before you do any operation which is git commit get push and so on git has some
hooks associated to it that means you can hook your implementation before
and after some of these events for example let’s say you are go developers you are writing some of the
code and trying to commit those code into the repositories okay now good
go has a very good linked formatter and you can also do let’s say your
code quality check and all so what you want to do is you want to have all the changes in your
repositories which meet the criteria of all these things like if the code is formatted the
code is linted and so on what you can do is you can make yourself get hooks
to run get lend oh sorry not co-linked go format and so on before
actually you uh you know commit to the repository so you can google about git hooks but in
short if i have to say that is whenever you clone a repository in your local machine
git is going to create a hidden folder called dot kit inside that folder there is another
folder called hooks and in that folder you would see a lot of sample files which are bash scripts
in linux systems so those those batch scripts if you
you know rename those from sample to just bash or sh they would be invoked whenever
you whenever git pro does any operation for example if you rename
uh before commit or before push file to its uh own shell
script that shell script is gonna be fired as soon as you do git push and uh before the changes are
pushed to the remote repository so now if your script returns status other than zero
the push will not happen so your changes will not go to remote remote repository and you will have a
very nice workflow in which you prevent making some of the mistakes that you
might have to investigate in your time in future so i really like that feature and it’s gonna maybe help you also in
your development environment to increase the productivity maybe so that could be the tip i would like to give
uh i think it is can we squash and reward top
comment at once uh i don’t know if i understand the question the problem can you uh i’ll move to this
slide okay so you wanna uh revert and squash uh like perform both
yeah yeah that’s the question okay so when i started the star uh like
session i said if you know what needs to be done then how or answering how would be easy
right so if we understand what do we mean by having a squash and reward
then it should be clear that whether it would work or not okay so the motivation of squash and
reward would mean that you wanna squash multiple commits into one and then you wanna also
you know change the commit message but if you see to do these two things into one there is
another guy called fix up so if you just use fix up it’s gonna do the same thing that you wanted to do
with squash in the world so maybe reword is something else
sorry fix up is something that you are looking for to do those things
i think thanks this this message um any questions
uh i have one last questions [Music] so so if you have to revert uh
a merge uh like let’s say i merged something in the master and i have to
revert it so if it’s a normal merge i could just uh like
revert that comment and it would uh revert that
but in case of uh rebase how that uh okay how could how can we do that
so revert from gate is also a commit to it right so if you just consider
uh let’s say this example okay let’s say you’re right now a master is pointing to
2 7 d 3 8 6 6 okay and then you realize that hey you didn’t want to have the scene in
your repository at all so what you do is you do get revert and this commit message spit is not
going to remove this commit from your history rather what it does is it’s going to create a
counter commit to this commit you know this commit will be having another parent to it which will be having
changes reverted back to the original state where uh which which was present in
maybe up till this point but it would be a new comment so now rebase you can have on top of
that which follows the same procedure that i explained earlier so i mean just to answer that question
rebase and the word would be two different operations what you need to know is what reward
does and what rebase does and when do you need these two things so reward will create another commit
after your original commit the changes will not be there in your remote repository of course but in your
commit history if you see first you will have the plus changes and then you’ll have a commit representing the minus changes
now rebase on top of that can work in the original fashion how does it work
thanks any final questions
request and what’s the difference in that scenario for merging the master branch with your home
runs and releasing your brands with the master branch after pulling the changes from your master
okay um i think i really understood the eu because of the
voice was breaking but let me rephrase what i could understand so maybe you were asking about what’s
difference between having a feature branch and master brand scenario where you want to merge changes
from feature branch to master with the merge strategy and what would be the difference with the ebay strategy
was that a question
yeah so again um if you have yeah i’ll just go to this diagram
diagram again yeah if you do just merge of your
feature branch to the master branch okay what it’s gonna do what kit is gonna do is take
all the commits available in your future branch and placed side by side with master branch starting
from a point where your branch was you know branched out for example
you had your branch started from this comment called cev4545
okay now you did a lot of comments so you had 270 386s and so once
so many comments after that as well so that’s where your branch is pointing at when you merge your branch with master
with mud strategy what is what kit is gonna do is something like this it’s gonna create a new merge commit and
have your comments available in the series right beside master brand so you will have all the
you know though that linear history of comments in your branch like this and up till some point where
your the branch pointer is pointing to and there there will be a new merge commit
created like this which will be pointing to the current state of the master and your branch point out
but if you had done rebase it would have looked like this somehow that let’s say this was the
master branch and you had five comments so all those five comments would be
arranged in the linear fashion just after the master branch itself so while look look by if you look at
this kind of representation in merge strategy it’s gonna be a multi-branch kind of a tree but with
rebase it will be a linear tree i mean linear something that
that is a tree but doesn’t look like a tree so maybe a link list maybe so having rebase done there would mean
that you will have a linear come industry which is neat and clean and you might want to understand okay this comment does this
this commit does that and uh yeah you don’t have any confusion about it when you look at the committees
free so up to you what do you prefer what kind of strategy do you prefer if you prefer merge
over ebay that’s fine uh if that suits your use cases but just a guideline from the industry
as per my experience people already always prefer the base over merge
so does that answer your question yes i do prefer release overnight
uh i guess that’s uh that will be the last question um thank you everyone uh for
attending and yeah there’s this uh um t-shirt raffle
uh provided by g frog and i’d like to thank uh j for that uh so do check out the chat if you haven’t
done that yet uh and thank you rahul for uh for the amazing talk a lot of
takeaways i usually use git but i have like done some of the things wrong way
and like now i i learned a lot of takeaways thank you so much thank you arie for
uh for connecting and for this presentation um thank you good to know
thank you guys and uh take care and be healthy
thank you thank you bye bye everyone bye bye everyone