Trusted answers to developer questions

Learn Git and Github in 5 minutes

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

Getting started

Make sure you have already installed Git locally on your working desktop. If not, go here to download it.

Tips:

  • 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:

mkdir my-project

Then, we move to the project with cd command:

cd my-project

We can now tell git to track changes in this directory:

git init

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.

Note:
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).

Working locally

The working flow of git is very simple:

  1. Do some modifications
  2. Add them to git
  3. Create a commit (a snapshot)

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:

git status

If there’s any modification​ like there is in our case, you’ll get this message:

git status

Note:
If you try git status command 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 git init.

Add them to git

Let’s do some modifications to our project.

Create an 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 ls command.

To add content via CLI (Command Line interface):

echo "Hello World" > index.html

Use cat index.html to see the content added.

Now, we can see if git tracks our modification. On the terminal, type git status:

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.

Doing 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"

Note:
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.

Tip:
To edit the commit message of the current commit, use: git commit --amend command.

Homework:
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.

Other Git commands

History

To get the history of your commits (different versions of your source code):

git log

If you did the work above, you’ll see at least 2 commits:

Git log

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:

b7dffeaa7cc47c68ab0ba1547e1c1ab46bf3651c

To switch to any commit, use:

git checkout <sha>

Tips:

  • 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

Host your source code on GitHub

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:

Create a new repo

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 git@github.com: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:

  • To copy on your GitHub account, click the fork button
  • To copy on your local machine:
git clone <URL>

Conclusion

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.

RELATED TAGS

git
github
Did you find this helpful?