Hey everyone so in this video I compiled a series
of popular Git commands and how they're executed in VS
code. Specifically in this video. Commands including add, reset, commit,
update, rough stash, and clean.
So check it out. The good ad command is used
to add, also known as stage file content to a
staging area. To do this in VS code, if you're
under source control, just make sure on the file that
you want to stage you hit the + and in
the output you could see that Git add was executed
on the first line. The Git Reset Command removes file
contents from a staging area, and if you're in VS
code under source control you can.
Click the - next to stage changes to unstage all
files. If you have multiple files or just over a
specific file and click the - to unstage and towards
the bottom under the output for git you could see
that the Git reset command was executed. Sometimes when you
write a new or make changes to existing code and
commit, there might be something you forgot to add and
you really don't want to commit again.
That's when the commit amend command comes in handy. And
to demonstrate, I have a folder here with the file
that I just initialized O. The first thing I'm going
to do is just stage this index file and then
I'm going to add first commit and before I actually
hit commit you could see on the output the git
add command at Visual Studio executed. I'll clear that and
when I hit commit now we'll see the commit command
that was executed. So at this point.
Let's say I forgot that I wanted to add some
additional code that I wanted in that first commit. So
for example, add this line in the first commit, change
that to include. All I need to do now is
stage here. These commands are executed in the background, I'll
clear that.
But at this point I can go to the... and
select commit and choose commit, stage, amend.
And accept commit message. So now if we came here
and searched for.
Amend will find the line that Visual Studio executed.
We can publish this.
And you'll see there was only one commit and on
line 13 include this line in the first commit is
present.
There are times you might commit accidentally or too early
and you want to undo this. The update REF Command
allows you to do this by updating your branch head.
But to demonstrate an easier way in VS code, I
have here one modified file that I'm ready to stage
and commit to demonstrate this. So I will stage this
and add my comment to commit first commit.
And now let's say this was a mistake. We can
undo this last commit. So to show you the command
it gets executed, let me go to the git output
and clear the screen. There. All we need to do
is go to the... and navigate to commit.
Undo last commit and now our index file is back
and it's untracked. And if we look at the second
line here on the Git output, we can see where
the update REF command was executed. If you've committed your
code a few times and want to revert several commits
earlier, no problem, we can rollback to previous commits. So
let's say I made three commits. I'll open this file
to make my changes by adding my first.
Commit change. I'll stage that, add a comment and commit.
My next change is second commit. Will stage that.
At our commit and commit, and lastly our third change.
Well, stage that.
And at our comet to commit at this point we
made 3 commits, and if we wanted to see that,
we can go to the explorer and look on the
timeline here.
And if we hit refresh, we see that we have
3 commits. But to demonstrate undoing the commit, we'll hop
back to source control and all we need to do
is go to the... go to commit, undo, last commit.
And if we select index, you'll see that the third
commit has now been undone. If we wanted to do
it again, we can just go to, repeat it by
commit, undo last commit, and you'll see that the second
commit has now been removed. So what's going on is
that we've reverted.
That commits, as you can see, on the left hand
side, but on the right hand side there's still data
there that's saved as a backup. But if we're confident
we just want to be at this state with just
the first commit, all we need to do is unstage
and then discard changes and confirm by clicking discard changes.
I'll delete this text here, and if I now go
to the explorer, we could witness that on our timeline
we just have our first commit.
Use Git stash when you want to record the current
state of your workspace, which you could then restore later.
Here in this workspace I have three files, two HTML
files which I made text edit changes to, specifically a
number change ending in 000 which was previously ending in
5555. Also, there's one JPEG that is currently untracked. To
interact with stashes, you can go to the... at the
very top of the screen.
And there's an option for stashes.
And the most relevant ones here are alley and O,
which are used after you've created stashes but to create
a stash.
You just click stash.
And give it a name like a simple edits and
if we were to look in the outputs you could
see the command that was used to execute the stash
and on the main screen here a stash was created
for the main edit and as you can see on
line 12 the code was reverted back to the number
ending with 5555. Now the JPEG is still there because
we didn't include untracked. So if we went back and
created another stash that included untracked just by choosing the
second option here.
We can give that a name like add photo.
Now we have two stashes on the disk.
Now to restore the stash that we can either use
alley or O command.
Now there's a few variations of alley and O, but
essentially OP will remove entry from the stash.
And apply just applies changes but leaves it in the
stash. So I'll choose O stash and select simple edits.
And notice our phone number has changed and both our
HTML files are visible again.
If I do O stash again and select add photo,
the JPEG is back in our workspace and essentially we've
reverted back to our original workspace. One more thing to
mention is that these commands are available from the command
palette, so if I were to come over here and
just type stash you could see some of the variations
that are available and sometimes that might be a quicker
approach.
The Git Clean Command cleans the working tree by recursively
removing files that are not under version control, and in
VS code that's referred to as discarding. Here you see
I have the version control icon selected and there's one
photo file that is not under source control, AKA not
being tracked. So if I right click here and choose
discard changes.
I'll be prompted, am I sure that I want to
delete this photo JPEG? I'll choose delete, and essentially what
it did is it ran the clean command in the
git output to remove that untracked file. So that's it.
If there's other git commands that you'd like cover in
future videos, drop them in the comments. And if you
got value out of this video, don't forget to subscribe
and like see you in the next one.