- Welcome to another Git and GitHub tutorial.
Now, in this particular tutorial,
what I'm going to show you is the process for
you're working on a project,
that project exists on your laptop,
you've been working on it for an hour, a day,
a week, a month and suddenly you want to turn this project,
this folder of files into a Git repository
and then put that Git repository on GitHub,
this is different than what I showed you
in the previous video, right,
I created the Git repository on GitHub
and then I brought it down to my computer,
worked on it on my computer and pushed it back up.
You know, it doesn't really matter,
honestly, I often just work that way,
it's a little bit easier,
I just make something on GitHub,
pull it down and then put files into it,
but it's useful to know this other way of doing it
and the key command that you need to know,
if you come over here to my whiteboard
is, do I have it here anywhere?
No, I do not, it's a new one I'm adding,
it's getting to be a mess down here, but it's init,
so this command, get init
is crucial for this particular video.
Now, I'm making this video series
without source code whatsoever, but if you're a programmer,
most likely you have a directory of code files.
I, instead of having a directory of code files,
have a directory of instructions for dancing,
this is the Rainbow dance, which is.
♪ Step left ♪
♪ Step right ♪
♪ Jump up and down ♪
♪ To the Rainbow dance ♪
♪ Step left ♪
You don't want to see me do this, do you?
Probably not, okay.
So this is the Rainbow dance
and if I go to my desktop
and I go to this Rainbow-Dance directory,
you can see that file is in there,
again, this could be a directory of tons of files,
but just one file, so what I want to do
is turn this into a Git repository,
it is currently not a Git repository,
let's be sure about that.
So I'm going to Terminal
and I'm going to say cd,
hold on, let me adjust the size here a little bit
and I'm going to say cd and I need to go to that directory,
so the way I can go to that directory
is just take that directory and drag it here.
Now I'm in the Rainbow-Dance directory,
let's make sure by saying pwd, I definitely am.
Now I'm going to type git status, not a Git repository,
so by default, any directory on your computer
is not a Git repository,
but I can turn it into a Git repository by saying git init,
that's all I need, initialized empty Git repository,
so now I have an empty Git repository, fantastic.
Now, what if I type git status now?
So it's saying untracked files,
so this is something that we haven't actually seen before,
you might not have realized this,
but I secretly with the wave of my magic,
my hands aren't very magic, I guess,
but I have always just been working on pre-existing files,
that were made on GitHub, I didn't add a new file
and this brings up a very strange thing
about commits and git and saving things, oh,
this erasing is not working very well,
let's try this other eraser,
but you're just going to have to bear with me,
messy whiteboard makes me very uncomfortable,
but I think life will go on.
So let's talk about workflow
with git and commits
and we need to, before we can get
this new Git repository onto GitHub,
we've got to deal with something else, the add, right
and I'm going to put it up here, add.
So let's say you have a file,
dance.txt,
forgetting about Git for a second,
if you make a change to that file,
you're just got to hit Command+S and save it,
the file's saved, your changes are saved,
what we saw
was like a commit,
is when we were on GitHub,
a commit was basically the same as save,
these were like the same things,
if you're working locally however,
the first thing you do is save,
that's just saving the actual file on your file system,
but it's not actually committing the change
to the Git repository,
so the second thing that you do is actually commit,
so save and commit are seperate steps, I'm saving the file,
but now I'm also committing it to the repository.
Now the strange thing is
there's actually a third step called add
and add, this is required, if you have new files,
so Git has this concept of a staging area,
where you can think of like,
let me redraw this diagram up here,
right, you have a bunch of files
and these files are being manipulated,
there is a staging area
and then there is the action of committing.
So with one file, this almost seems ridiculous
to talk about, but if you have a lot of files
and a lot of things you're changing, this could be useful,
because let's just say you have dance.txt
and then one.txt, two.txt, three.txt, four.txt, five.txt
and you've made changes in all of your files
and you want to do a commit,
but you actually only want the commit
to be associated with changes that are in these files,
but there are changes in other files,
so the things that you actually want to commit,
you can put in the staging area,
you can also remove them from the staging area.
Add is a way of putting things in the staging area,
so that when you call, when you execute commit,
those are the changes that are actually committed.
So for us, for 99% of the cases,
you saw that I said git commit -a,
that does add and commit all in one fell swoop,
that's what I did in the previous video,
git commit -a just says add everything and commit it,
any changes I've made anywhere, commit them.
But in this case, I can't do that right now,
there might be a way, but I want to explicitly
add the new files and then commit them,
I need to put stuff in the staging area
and then commit them, so let's do that.
I'm going to come over here and what I'm going to do
is I'm going to say git add dance.txt,
now let's do something actually
just to be a bit more explicit about this,
I'm going to
make a second file,
dance-two.txt
and you can see git status,
so now you can see there are two files there,
so if I only want to add one,
I do that, I now added that to the staging area.
Actually let me clear and say git status again
and you can see, look, this file is going to be committed
and I can say git remove --cached
the filename to unstage it, if I made a mistake.
This file, when I say commit,
it's still just a new file, that Git recognizes there,
but it's not actually part of the history yet.
So now if I were to say git commit adding a new dance,
that's now been added
and oops, and I can say git,
oops, I hit Caps Lock by accident,
I can say git status again and you can see,
it's showing me the only thing is this new file.
Now if you've added a lot of files
or you just want to add everything,
you can always just say git add .,
so git add . is kind of like git add
anything that Git detects as a change or a new file
and then I can say git commit - adding a second dance,
oops, I forgot my -m,
git commit -m
and notice I'm not saying -a anymore,
because I'm doing the add and commit as seperate steps
and now I've added a second dance, I can say clear
and I can say git status and on branch master,
nothing to commit, working directory clean,
it means no files have any new changes in them
and the directory doesn't have any new files in it.
So now we're done, I made a new repository,
I had an existing directory of files,
I turned it into a repository, added those files,
now what I want to do is put that on GitHub.
So right, I should just say what I said before,
git push origin master, right,
that'll push it to GitHub.
Ooh, I hit the table by accident,
there was no, I got an error message,
but I'm not angry about it at all,
I deserve that error message,
origin does not appear to be a Git repository, because why?
Let's say git remote, nothing,
git remote -v, nothing, there are no remotes,
so if my repository started on GitHub
and I brought it down to my computer,
a remote is already going to be attached to it,
but if I'm starting my repository on my computer,
it doesn't have a remote associated with it,
so I need to add that remote.
So the way to add the remote is I have to go now to GitHub
and I have to make a new repository,
so I'm going to call this repository, Rainbow-Dance
and it's a public repository, now here's the thing,
I don't want to initialize this with a README right now,
I could, and there's a variety of ways
I can deal with that, if I do,
but this is actually going to make
a completely empty repository with no files in it,
that's what I want, I want it to be empty,
I'm going to make it remote and then push my repository to it.
So I'm going to say Create Repository
and actually if you look here,
it's going to tell you what to do on your command line,
but I already did this, git init, git add,
git commit first commit,
but this is the new thing that I need to add,
I actually need to make the URL
for this repository on GitHub
the origin remote for this particular repository.
So the commands for doing that are git remote add origin,
I want to add a remote named origin
and by the way, I don't have to call it origin,
I could call it GitHub, if I want
or I could call it Rainbow, if I want,
it doesn't matter what I call it,
but origin is kind of a convention for the sort of
origin web server version of this repository,
so I'm going to say git remote add origin
and then what do I need?
I need the URL for this Git repository,
which is conveniently right here,
I'm going to copy it and I'm going to paste that in here
and then I'm going to hit Enter
and now I should say git remote, you can see it's there,
git remote -v and you can see it's there.
Okay, now we're cooking
and I'm going to say git push origin master,
oh and whoa, it just worked.
So interestingly enough, remember how I had to type
my username and password in before?
Well, I didn't this time, the reason why I didn't
is that my machine just sort of like cached it somehow,
so I've a feeling if I like restart my computer,
the Terminal will come back and might ask me for it again,
but it's nice to see that you don't have to
continually enter that username and password,
if you're kind of in the same session,
but if it does, enter your username and password.
So you can see, I pushed all that stuff there
and now if I hit Refresh, you can see there it is,
dance and dance-two.
Now incidentally, I'm going to go to this file,
I'm going to hit Edit and we're going to change the Rainbow-Dance.
♪ Step left ♪
♪ Step right ♪
♪ Jump up and own ♪
♪ Twirl your hands ♪
Let's add that,
twirl your hands
and then I'm going to hit commit
making a change on GitHub,
I don't know if you guys can see this,
then we scroll down and hit Commit Changes,
I just want you to see that now, if I go,
so I made a change now on GitHub,
I made my repository on my computer,
I pushed it to GitHub, I made a change on GitHub,
now interestingly enough, if I say git status,
nothing to commit, working directory clean,
I feel like sometimes it tells me if I'm behind,
maybe I have to do git fetch, fetch is another command,
anyway, sometimes it tells me I'm behind,
but it doesn't know that I can say git pull origin master
and you can see that it actually brought those changes here
and now if I go back to my dance,
oh,
silly text editor doesn't like auto update the file,
but you can see it's there now,
Twirl your hands and do the Rainbow dance, yay.
Okay, so just to recap here in this particular video,
what I looked at is I have files on my computer,
I turned those files into a Git repository,
then I created an empty repository on GitHub,
linked it together, pushed the files up,
made a change on GitHub, pulled them down,
so now I have a way
that I can just work locally or work remotely.
So the thing that we're really missing here so far
is the collaborative features,
so what if you want to work on somebody else's repository,
but work on their repository locally on your computer?
What if you want to do that in a seperate branch?
So that's what I will get to in the next set of videos,
there's a lot more, I think there's a lot more concepts
to sort of cover of how to do all of those things locally
and a few other little tricks like a git ignore file
and a bunch of other little things,
that I'll get to in other videos as well, okay.
Thanks very much and see you hopefully in another video.