DEV Community

Cover image for GIT: Explained Like I'm Five
Joel Olawanle
Joel Olawanle

Posted on

GIT: Explained Like I'm Five

Like we said earlier Git is the tool that tracks the changes in your code over time while Github is the website where you host all your Git repositories.

Getting Started

The first thing you have to do is install Git. To install git follow the instructions here to install git (if it's not already installed).

Git Commands

Making use of git commands can be so confusing if you don't know what each command stand for and what they do specifically. I have put together basic git commands, what they mean, and how to use them so you can see how easy it is making use of git.

This is actually in two phases.

Working with local repositories

By local repositories, I mean that folder in your Computer which you want to move to GitHub. To properly explain the whole process, I will attach pictures that explain the whole process.

I would be making use of the terminal in VScode, you can make use of any command-line interface.

Alt Text

The image above is a folder(GIT-DEMO) with just two files. I would be making use of git to push this folder to Github now.
To do that we would be making use of the VScode terminal (ctrl +).

Note: You can make use of any CLI as long as you are in the folder directory.

The first command we would be using is

  • git init This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible. In other words, you are initializing a git repository in the root of the folder.

Alt Text

If you check your folder you would see a .git folder

Alt Text

  • git add This command is used to add files to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). You can use git add to stage a whole folder, all unstaged files, or a specific file.
 To add all files not staged:
$ git add .

 To stage a specific file:
$ git add index.html

 To stage an entire directory:
$ git add directoryName
Enter fullscreen mode Exit fullscreen mode

Alt Text

  • git status This is a command you will find yourself using all this time. As its name implies, it helps you check/know the status of your files if it has been staged, committed, e.t.c.

Alt Text

The image above tells us that we are on branch master. Not commits made yet and also lists files that should be committed.

  • git commit This records the changes made to the files to a local repository. For easy reference, each commit has a unique ID. It’s best practice to include a message with each commit properly describing the changes made in a commit.
$ git commit -m "Commit message in quotes"
Enter fullscreen mode Exit fullscreen mode

Alt Text

You can still make use of git status to check the current state of the repository.

Alt Text

At this point, the next thing might be to push all changes made and files to Github.

The first thing you have to do is go to Github and create a new repository if you haven't created any before. This would give us rights to a particular repo.

  • git remote This is used to connect a local repository with a remote repository.
 Add remote repository
$ git remote <command> <remote_name> <remote_URL>
Enter fullscreen mode Exit fullscreen mode

Alt Text

You can make use of git remote -v to check/list remote repository

The final thing is to push to your repository.

  • git push This is used to sends local commits to the remote repository. It requires two parameters: the remote repository and the branch that the push is for.
 $ git push <remote_URL/remote_name> <branch>
Enter fullscreen mode Exit fullscreen mode

Alt Text

That's all about working with local repositories, although there are some basic commands we haven't explained which I will explain in the next phase on working with remote repositories.

Working with remote repositories

To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer. Git will create a directory locally with all files.

$ git clone <remote_URL>
Enter fullscreen mode Exit fullscreen mode

To get the remote URL of whatever repo you want to clone, just go to Github and copy the URL.

Alt Text

Once that is done, you can now open the cloned folder, make your changes, and update the repository. Once you are done making local changes on your computer, use the following commands to push changes to your Github repository.

You can read about git branching here if you want to learn how to push to a new branch so as to avoid conflicts when pushing your codes back to the repository.

  • git add Make use of git add as I explained earlier to stage your files. Then you can proceed to check the status of your repository by using git status.

Alt Text

  • git commit The next stage would be to commit all your changes as I explained earlier. Add a message to your commit as it's a best practice.

Alt Text

Once that is done the next thing would be to push your changes/files to Github.

  • git push To know which branch exists on your repo, make use of the git branch command so you know which branch to push your changes to. You can also use git remote -v to see the exact repo you are about to push to.

Alt Text

Then Finally, push your code.

Alt Text

I strongly recommend you check the article on Git branching so you see the importance of pushing your codes to a new branch to avoid conflicts.

As always, any questions or suggestions, please feel free to leave a response or tweet me 🤭! Be sure to connect with me on socials! 😎

Top comments (0)