COC
git
reset / revert
Let's UNDOCurrent state - The local repository is in-syne with the remote, we have 2
text files (demoOne & demoTwo) that already exsists on remote and our working
directory is clean(no local changes at the moment).
Let us learn, (master)
er
Se Cle ree a cee eae
Date: Tue Apr 26 21:39:58 2022 +0530
Ce Coan
eee Coreen eee eats
eC es esas herr eerer)
adding few Tines to demoOne file
rset ace
SUL ee berate eet
CCRC ea
a learn, Oro)
cars
Ce eens
Sa Oro)
eee eats
aie
en Rt
ae eR
acc
t us learn Oro)
Cad
VOC Rn
aera) forts]
eect
lon branch master
rc Ue een MC yee
nothing to commit, working tree cleanLet's add a new line in each file and ereate a new commit for each change.
eres (master)
ee eee Coots
eo (coe)
re
Let us learn, coe)
ee OC eC
eC eC)
1 file changed, 1 insertion(+)
are fore)
Pe i a ee Cr acd
oy cca)
rae oa (coe)
Case C CT Aunt Le
(master)
S$ git log
een ener eens nee Pare
pate: Tue Apr 26 2 eRrorietrrn
This is how th
pee ere meee is is haw the
commits look like
eee eee eee ren een eae now
CC tat eee Set cee cce)
Ce Ree aary Pec ornite
i added in our local
CUCM ae eee Re Cus eared
elas DC eed ees Pree repo.
adding demoTwo file
We are actually
eee eee ea now 2 conuuits
elas a ees fh re Ee)
ahead of the
coe
remote.
ee eee
Penta elas Perret Es
EC ot aa mayank Sgit reset --soft
Let om
(2th eee ele eee er Cee i)
eae rep)
caer)
POR eee eer aa ceec te L eare
tea Tue Apr 26 21:39:58 2022 +0530
ee ea
Se Cree eR ec ae
Tue Apr 26 21:39:09 2022 +0530
CC a ea
eee Ree eee
Tue Apr 26 21:38:20 2022 +0530
Cr Cut aa
Let us learn, cer)
Peer
Pu
Nour branch is up to date with ‘origin/master’.
GO Coe
(use "git restore —-staged reset to 2 commit after current
HEAD, HEAD~2 -> 2 commits after HEAD ete.)
When you perform reset, it always undo all commits after specitied commit
and not just that commit or a specific range. mayank_at a)git reset --mixed
ee
ee ee
ieee oad
In Cees
Im Cees
ey crap)
est
lon branch master
ree ee ar re ae
eee ese ect sd
Cr ae as oe a Mn)
(use "git restore ..." to discard changes in working directory)
ae cre]
rae)
Author: mydemo222
Conn seer re oraneLe
Sean
Mee reer Cee ce
fo Ce ed seer Relea)
Ce Re un
Me eee Cee ese
Ce ee oREL Eee eee)
Eee aa
What actually happened?
When you pass the flag ‘mixed! (which is default), it behaves similar to ‘soft! in
the sense that the changes are not lost from the local but are unstaged. So, you
need to add thera again if you need them back before doing commit or may
be stash them. Here we moved 1 commit ahead from prev HEAD. Or
in other words removed all commits after HEAD-1 . >git reset --hard
As in the last change we did soft reset, let's add back the unstaged files and
also commit them. Added a new commit '8b2¢a... HEAD pointer updated
[Crt s ia)
ws Tear Crp]
ECU
Se ec
Tue Apr 26 22:14:30 2022 +0530
Ce er
eee aoe eae
Sete Esme eerie tet)
Se
Now, when we use
Deets eed in
Tue Apr 26 21:39:09 2022 +0530 the flag ‘hard’ in
Roe Cee ee eae the reset command,
It vesets the
Sere eee ener ee ea ence ee commit but also
elas SCT es eee bear Lt)
removes all
Cm Cent aE changes from
rer (master) local which where
Feehan
eee eC Coc naa part of those
eae a) coop) commit(s).
eat
eee Working directory
Your branch is up to date with ‘origin/master’. .
is clean,
Inathing to commit, working tree clean
mw nk_ahu jaa og
git revert
This is the current state, we are in syne with the remote repo.
Seer Coe)
git revert command sounds similar to reset, and yes both are like performing the
undo operation, but reset was much cleaner that revert.
Why so? When you perfrom the vevert operation, it will undo the commit (or range
of commits) but it also adds extra commits te perform this revert.
So, basically git history would have the commit that had the change plus a commit
which was used to revert the change.
Let's see that in the next slide, what happened when we fired the revert, = >hen we fired the revert command in prev slide, it actually asked to add a
commit message for the revert we are going to perform. With revert
Sera en we can actually
Eee eta ter undo any specific
Please enter the commit message for your changes. Lines starting commit or range
eC UU me ae .
. 2 . of commits.
With reset
it was resetting
Om
Pee ee are
is
Se
3 ad everything
after the specified
commit. We
can't specify a
range there.
(master)
ar
Pe irre ec Reece eeece
Date: Tue Apr 26 22:15:33 2022 +0530 A new revert
a commit has
been added.
This is a Revert command demo ~ Reverting “adding demoTwo file
This reverts commit ca54bc9c0486922e6683eaf8442d1 eSdea8Safb9.
ee ree eer Rone Cae
Tue Apr 26 21:39:58 2022 +0530
Se a Now, doa
i ‘git push'
ee ree aera Rene eee gt P
ree eee eee) if you Want
ee a this change to
appear on the
: mydemo222