Git is a fantastic tool that every modern developer should know, no matter the language they use. Git tracks changes to your source code so that you don’t lose any history of your project.
In this tutorial, I want you to learn the fundamental notions of Git and how you can host your code online on GitHub and share it with the world.
Make sure you have already installed Git locally on your working desktop. If not, go here to download it.
- To check if git is already installed, open the Terminal and hit:
git --version # output git version 2.20.1
- To get the latest version of git:
git clone https://github.com/git/git
To start working with Git, we need to create (if it hasn’t been already) a directory (folder) for our project. Type the command below on your Terminal window:
Then, we move to the project with
We can now tell git to track changes in this directory:
Now, let’s try all of the above commands:
mkdir my-project cd my-project git init
The command above is important as it helps create a git repository in your project.
You can use Git in a GUI mode but, in this tutorial, we are using Shell (Terminal) to type commands. You can use the built-in Terminal in your OS or the one that is integrated into your editor (VSCode for instance).
The working flow of git is very simple:
Let’s detail them one by one.
Do some modifications
Adding or deleting content or files is what we call modifications. To check for (a) modification(s), hit:
If there’s any modification like there is in our case, you’ll get this message:
If you try
git statuscommand in a project that is not recognized by git, you’ll get this error:
fatal: not a git repository (or any parent up to mount point /) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
To fix it, just do
Add them to git
Let’s do some modifications to our project.
index.html and add some content. Use your favorite editor for that. If you want to use the command line:
touch index.html # create a file
You can see if it was created with
To add content via CLI (Command Line interface):
echo "Hello World" > index.html
cat index.html to see the content added.
Now, we can see if git tracks our modification. On the terminal, type
touch index.html # create a file echo "Hello World" > index.html # adding hello world in index.html cat index.html #to see the content in a file git status
Git finds a file that is modified but not yet added for tracking. To do that, use
git add <file>. For simplicity, I advise you to use:
git add . # dot at the end
… to add any file that has been modified in our project.
git status once again will tell you that your changes are ready to commit:
touch index.html # create a file echo "Hello World" > index.html # adding hello world in index.html cat index.html #to see the content in a file git add . # dot at the end git status
Now is time to create our first commit.
Create a commit
Simply speaking, a commit is a version of your code at a given time, like a snapshot of your code. You can create it like this:
git commit -m "Initial commit"
It is common to name your first commit as Initial commit or First commit. For the next commits, make sure you give a good description so that you can easily remember what change you did in that specific commit.
To edit the commit message of the current commit, use:
git commit --amendcommand.
Add more content in your project and create the second commit.
That’s all for git workflow. You can repeat it as much as you want. Let’s see some other useful commands.
To get the history of your commits (different versions of your source code):
If you did the work above, you’ll see at least 2 commits:
Move back in the history
The power of Git is that it allows you to easily go back to the history of your code. This is especially helpful if the current code is broken and you want to have its previous version. You need to have the commit id (aka SHA) to do this operation.
The commit id looks like this:
To switch to any commit, use:
git checkout <sha>
- The first 7 charactors of your commit are enough to switch to it:
git checkout b7dffea
- To move to the lastest commit use:
git checkout master
Github is an online platform where developers host their source code (and can share it the world). Go there and create an account if you don’t have one.
You need to create a folder (called repository) for your project:
Now, you can connect your remote repository with your local project. First, copy the address that’s shown out there.
Add a remote repository
git remote add origin firstname.lastname@example.org:Bam92/my-project.git
Our remote is named origin. Make sure you use your link (that might be in https).
Send your code to a remote repository
Make sure you have already saved all your commits:
git push origin master
In case you’ve done modifications on GitHub and you want to have them locally:
git pull origin
Copy a remote repository
Everything on GitHub is public, and you can copy as well. To do that you have 2 options:
git clone <URL>
We’ve learned the fundamental notions of Git and Github. The most important commands to remember are
git add <file> and
git commit -m 'message' if you’re working locally, and
git push and
git clone if you’re working with remotes.
Thank you for reading.
View all Courses