
is a flag you pass to tell git that this is going to be your global git account and you are going to use this account to create git projects all over the computer. You can also use a git account specific for a directory. You can find details more about the initial setup and the necessary commands here.
Basic Git Commands
Now that you’re all set up and ready let’s run a
You’ll get an error that looks something like this. And of course, it’s natural, as we have not initialized a git repository inside that folder which represents our working directory.
In order to initialize Git, we will run the command
Now, if we run
again we should see that we’re on branch master (we’ll talk more about branches in a minute) and that this is the initial commit, and there’s nothing there to commit, as the project is empty for now.
Let’s create a text file and start creating some changes to simulate a real-world project. I’m going to do so inside of visual studio code, you can pick any IDE/editor of your choice, they all have similar features, but VS code is my favorite. Let’s add some text to the file:
Now, when we run the git status command we’ll notice that we get an untracked file.
This means we have some content inside of the project in the modified state and that we haven’t added yet to the staging area.
To add the files created and edited in the previous step to the staging area we will run the command:
If you have more than one file that you want to add the staging area, you can also run
which adds all files in the modifying area. Run git status again and you’ll find that the text file is now labeled as changes to be committed.
Now for the last step, let’s run the commit command to commit these changes. One thing about the commit command though, it has to be accompanied with a message, the reason behind that is that Git tries to enforce best practices, and it wouldn’t be hectic inside of a single project if changes were being made without a word of description said. So this is how we will run the git commit command:
git commit -m “added hello.txt”
In the command line, it says that one file was changed with 3 insertions, which are the three lines we’ve written so far.
Since all the collaborators will be reading your commit messages, it’s important you learn how to write them succinctly: Git Commit Messages
So everything works fine, but now if we want to share the project with a colleague and bring them on board we need to place our project somewhere on the cloud where other teammates can access it. We’re not making it centralized, as everyone is going to have his working copy, but there needs to be a focal point to pipeline the project changes. So let’s go and create a repository on Github.
Give it a description and a name, I’ll call it Hello Git.
When the new repository’s page opens up, you’ll find an option to push an existing repository which is exactly what we want to do.
So we’ll follow the commands and write:
git remote add origin [repository’s link]
git push origin master
The first command links the remote repository we created to our local one under the name of origin, and in fact, you can name this anything you like as long as it’s clear enough. The second command pushes the files we have on the master branch to the origin repository. So by running these two commands, our repository would end up with the text file we created on the repository.
Now that your code is pushed to a remote repository, anyone with access/permissions to your repo can download the code and start editing it.
There are two ways to bring a remote repository to a directory on your local machine. You can either download a zip of the entire project or you can clone(duplicate) the repository to anywhere on your system.
To download the repository with the full history of the project and nearly every change that was ever made, we use the command
Just navigate to the place where you want to clone the repository and run
git clone "link of the remote repository"
Usually, you don’t need to write this entire command on your own. Just find the repository you want t