- All right, let's talk about some common usage
of Git repositories.
First is a basic repository usage,
where you can use git clone on your Git repository
to clone the contents of any remote repository
to your computer.
And if you have done that,
and ever you need to fetch the latest changes,
you can use git pull from within that Git repository.
That's pretty easy.
You've probably done it before.
Now, how do we upload changed files?
Well, modified files need to go through the staging process.
So after changing files, you should use git status
to see which files have changed.
Next, you can use git add to add these files
to the staging area or git rm to remove the files.
And then you commit the changes, no matter if it's a change.
And then you commit the changes, no matter
if you've added files or removed files
using git commit minus m followed by your commit methods.
Next, you synchronize, git push origin main, for instance.
And then from any client you can use git pull
to update your current Git clone
and make sure that you are completely up to date.
Now, sometimes you may have removed the file
and you regret.
Well, then Git is offering a couple of options
to get it back.
Let's have a look at the simple option.
So your ability to undo a change really depends
on the current state of the modification.
If the file is staged but not yet committed, it's very easy.
Use git restore minus minus staged
followed by the file name.
So first, if you have used git rm myfile and git status,
you will see it as removed,
and if then you think "oh no I want to have it back,"
git restore minus minus staged
is going to do it for you.
If a file has already been committed and pushed,
you can use git log
and identify the commit by the commit message.
And next, use the commit hash to revert the transaction
using git checkout followed by the hash id,
followed by the file name.
And that will allow you to get this one single file back.
If you do that, it's really important that you
revert to the latest commit where you still had the file.
So you really need to pay attention.
Let me demonstrate.
So, here we have my new git repo and in my new git repo
I'm going to remove 10 dot txt.
So, git rm 10 dot txt.
Let's choose git status to figure out what is happening.
And there we can see it's deleted 10 dot txt
and it's telling us git restore minus minus staged,
which we can use to unstage, to get it back,
but I don't want to get it back yet.
So I'm using git commit minus m remove 10,
and then I'm using git push
and oh no, what did I do?
I need to have it back.
Well, what are we going to do to get it back?
Well, git log minus minus 10 dot txt.
This git log is giving an overview
of all the transactions that have happened on 10 dot txt.
And now we can see that there are two different commits.
This is the one where we removed it.
This is the one where we created it.
So I'm going to use a commit ID and git checkouts followed
by this commit ID
followed by 10 dot txt.
And what is the result?
Well, the result is that I have my 10 dot txt back.
Are we there yet?
Well, no, not exactly, because we have the file
still being ready to be committed.
So I need to git add 10 dot txt.
Did I really need to do that?
No, I don't even need to do that because it's already there.
I need to git commit minus m,
10 back,
and git push.
And now we have restored this file 10 dot txt back
and I have it permanently back.
So that's how you can undo certain transactions.
Now what else can you do?
Another convenient thing is dot gitignore.
In some cases, a directory may have files
that you don't want to be cloned to the GIT repository.
Think about files that contains sensitive values,
like passwords.
Hey, before we continue, can I say one thing about it?
I think if it contains sensitive values,
it shouldn't be in your git directory in the first place.
That reduces the risk of anything going wrong.
So better go for strict separation.
But hey, if you don't wanna do that,
it's useful to know that you can use dot gitignore
So, if you want to exclude files from being synchronized,
you can create a text file with name dot gitignore,
that lists all files that need to be ignored.
A dot gitignore file can be added
to the current git product directory
and you can use the following command to create it.
So git conflict minus minus global core excludesfile
dot gitignore underscore global.
And in this list,
you can list files that should be ignored
if they occur in any Git repository.
So you can do it globally.
You can also do it in a local Git repository.
And that is what I want to show you right now.
So let me use echo password.
Right, then password dot txt.
That's a typo.
And let me echo password dot txt greater than dot gitignore.
Now I'm using git add star,
and oh what do we see?
We see following paths are ignored.
It's going to display this message all the time.
If you don't wanna see that,
then you can use git config advice add ignored file false.
Then you won't see the message anymore,
but for now I'm good.
So git status is just telling us
that there is this gitignore file and this gitignore file
is an ignored file.
And git commit minus m ignoreme is making sure
that we are doing the commit and oh,
we already have to branch up to date.
What does that mean?
Well, that means that if I'm going to check
in the Git repository on the website,
I won't see the password dot txt file.
That's all that it's about.
So this hopefully helps you
in working in a more efficient way with Git.
There's one major topic we still need to talk about
and that's branches.
Let's do that in the next video.