So we learned a lot about commits
and branches now and so far,
we only added data to our well, Git-managed project here.
Now is the time to also delete information.
And for this, I will leave our main branch here
and first create a new branch.
So we can run git checkout -b,
and now a new branch name.
I'll just call it delete
because we will delete well, data in this branch.
So we are in this branch now
and in this branch, I'll add a new folder maybe.
Not in here, maybe right there.
I'm deleting-data.
This one.
And in there, I'll add a new file.
So file1.txt.
We'll then add this file and commit it.
Added file1.txt.
Then we'll add some dummy text in here
just to show you how deletion works.
For this, we need some additional commits.
Just some text to dive deeper here.
We'll also save this.
Added, now it's working.
So git add dot
and git commit -m.
Added text to file1.txt maybe.
Like this.
And let's clear this.
And now we'll add a third commit
where we could say some more
text in here.
And finally, our last commit.
Added more text.
Not too important here what the commit says.
And now besides this first file here,
I would like to add two more files.
Let's quickly do this.
So file2.txt.
And also commit this.
Commit -m.
Added file2.txt.
And now we'll add a third file here.
File3.txt,
which we'll also add as a separate commit.
And git commit -m file3.txt.
I know lots of commits we just created here
but to understand how deleting works,
well, it's great to have these examples.
We can also clear this again
and now log our information,
and so we see well, we have some data to play with now.
I would like to get started with a very well,
simple operation actually,
and this is deleting files from the working directory.
As you saw, the three files we added now
are part of previous commits.
Therefore, these are all tracked files,
and to delete these, well, we have two options basically.
A very common one, of course, is to simply well,
right click onto the file
and press Delete, and then you can move it to the trash.
With this, and by checking its status,
you see that we have to add this deleted file so to say
to the staging area
because we have unstaged changes to be committed,
so we have to tell Git that this file should be executed.
Alternatively, we can also run git rm
and now refer to the file name.
File2.txt, like this, so git rm for remove.
And there you see this doesn't work.
Why's it not working?
Well, because currently we are in the git-basics folder
but to delete a specific file without specifying a path,
we have to navigate into this path,
so with cd deleting-data,
we are in the correct path now.
And in there, we can now run git remove file2.txt.
With that, you see that the file was deleted.
And by running git status once again,
you see that we have a difference here.
If we delete the file from the working directory
as we did it,
we have to add this change to the staging area first.
You can see this down here.
This is the deleted file with that red text,
and these changes are not staged.
I mentioned this already.
Up here, when we use the git remove command,
we see that the changes can be immediately committed.
So with this, this means Git automatically removes the file
from the staging area for us.
Therefore, if you just want to delete files,
git remove or git rm saves you the additional adding
to the staging area step.
Nevertheless, we can, of course, add this
because we have to add the file we deleted
with the right click
and then we can commit this and say deleted file2 and file3.
Like this.
And with that, if we check the status,
you see everything is cool, nothing to commit.
We can also close this window now
and focus onto file1.txt
because besides deleting working directory files,
we might also want to delete individual commits
or to be more precise here,
we might want to go back to an earlier stage
of our project, and therefore,
delete the latest commit, for example.
If we go to our git log here,
you see that currently our HEAD
is here in the latest commit.
No big surprise.
But what if we want to revert this change now?
What if this should be deleted
and we say well, actually, I would like
to have this commit back
where we had the second and the third file?
Well, this can be done.
And for this, we use a command named git reset now,
well, to reset what?
Basically the so-called head,
so the head is the pointer towards this latest commit
in our branch.
And this should now point to well, one commit earlier.
And to make sure we don't only reset the head,
but that we also exclude the latest commit changes
from the staging area
and to basically get rid of this entire commit,
we have to add this --hard command here.
This means it will update the so-called index
and the path.
I'll not dive too deep into this at this stage
of this module
because the important thing that you have to keep in mind
is with adding --hard to this reset command,
you will kind of be able to jump back
to an earlier commit.
And this jumping back now works
with the so-called HEAD command.
As I said, the HEAD is just the pointer,
and this pointer should now, for example,
go back to well, one commit earlier.
This would be ~1.
So the head goes back one commit, HEAD~1
and --hard also make sure
that this is a hard reset,
so this means we update the so-called index
and the path, and this means we simply kind
of delete the latest commit entirely,
so all changes applied right there
are removed from the repository
and also from the staging area.
If we do this now,
we see that well, magically,
our file2 and file3 are back.
And if we now check our log,
you see that the latest commit is no longer displayed.
Now our file3.txt here is the latest commit
in our branch, the HEAD points to this commit,
and also if we quit this,
clear it and check our staging area,
you see everything is clean, nothing to commit.
We could also jump back multiple commits.
This would then be git reset --hard and then HEAD.
Well, ~1, 2, 3 and so on.
This would also be possible.
But generally, this is how you jump back to earlier commits
by, at the same time, deleting the later commits you had
in your branch.
Now, talking about branches,
let me delete this quickly,
at the moment, we have well, three branches in our project.
This delete branch is a branch we can keep for the moment
but the feature branch is something no longer needed.
We merged the feature branch
into main already, therefore we can't delete this branch.
To delete a branch,
you have to check out another branch
than the one you want to delete
because otherwise you would delete
your current position so to say.
In our case, we are in the delete branch.
Therefore, we can just run git branch dash capital D,
that's important here, it must be a capital D,
and now simply add the branch name.
In our case, we add feature here.
We confirm this,
and we also get the information in Git
that the branch named feature was deleted,
and with git branch,
we see that we only have two branches left now.
So this is how we can change files
from the working directory,
how we can delete commits so to say,
and how we can delete branches.
There are only two core features you might typically use
in your project
and this is how we can get rid of unstaged
and staged changes?
Let's get started with the unstaged change here.
I'm here in this file1.txt file
and in there, I add well, more content so to say.
Let's save this.
And if I now check my git status,
then no big surprise,
I have changes not staged for commit,
so unstaged changes.
What if you want to revert these?
Then you simply check out the latest committed stage
of your project so to say,
so the situation you had
when checking out this branch.
This works with git checkout -- and then a dot.
This will simply reset your branch so to say
to the stage of the latest commit.
If you do that, you see this text is gone.
By the way, in case you're wondering,
what happens if I add some text here once again?
And then say okay, I can just check out the main branch
and then jump back to the delete branch maybe
to get rid of this text
and to have access to the latest commit in that branch.
Do this with git checkout main, for example.
But if you try this,
you see that this is not working.
We have to commit our changes or stash them.
Stashing is a bit more advanced
and therefore not the core focus of this course here.
Therefore, for us, this means,
we can again run git checkout --. to get rid of this text
as we just learned it a few seconds ago.
So these unstaged changes and how to revert these.
What about staged changes now as a final step
in this lecture?
Well, for this, I'll add some changes
to be staged so to say.
Saving it.
Now I'll add it to the staging area.
I just add all changes with git add dot again.
And now I say well, actually I see
that these changes are ready in the staging area
but actually, I don't want these changes to be part
of my next commit.
You could use this git restore command for this case here.
That's one approach.
I'll show you an alternative one though.
And this is simply git reset,
and now we refer to the file name,
so file1.txt in our case.
This is step one.
With this, if we now check our status,
you see that now we have changes not staged for commit,
so this is basically the same logic
that we had before when undoing unstaged changes.
So in this case, we can simply run again git checkout --.
to undo all unstaged changes.
Or we simply reset the individual file,
so this would be file1.txt in our case,
and with this in place, you see that the text is deleted,
and if we clear this now,
and check the status, you see everything is clear,
nothing to commit.
With this, I also want to conclude this Git part
of this module.
So far, we only worked locally on our project
but there is more to explore in the Git world,
and to explore more, we have to leave our local environment
and dive into the world of GitHub now.