DEV Community

Cover image for Git in just a few minutes
EstherWanjiru
EstherWanjiru

Posted on • Updated on

Git in just a few minutes

Hey, here's a deep dive into Git.

Incase you missed the Introduction to git and GitHub, start here

Whether you are a long time programmer or are just getting started, you know that Git is quite pivotal in your coding journey.

Git is a distributed version control system that makes tracking changes to your code very easy. It's easy to swap across versions and well who doesn't like the idea that one can undo the changes in your code through Git.

Lastly, Git is so ideal when incorporated in Teams.

Guide to install Git:

After installation, Configure it with your GitHub account.

Step 1: _(Option1)_Initialize Git in your existing project
To do this simply run the command in the terminal from the folder that contains all your project files. This will add a .git folder to your project that Git will use to track all the changes you make to your project.

Git init
Enter fullscreen mode Exit fullscreen mode

_(Option2)_Cloning your repository(entire project being managed by Git) by creating a new repo or getting an existing repository and copying the link to clone the project. Ensure you pick the right url according to how you set up your Git, either ssh or http. Thereafter type the command below

Git clone ssh_url or Git clone http
Enter fullscreen mode Exit fullscreen mode

in your terminal with the copied url at the end of the command. This copies all the code from the repo on to your computer and sets up Git to track all the changes. It adds a .git folder so that when you are ready to push your changes to GitHub, git will already know where to push them too.

Git structure

  1. Working Directory - All the files in the work repository in their current state. At this point none of the working repositories are being tracked by Git.
  2. Index/ staging - To track these changes in the work directory we need to add them to the Index. This can be done by using the command below.
git add
Enter fullscreen mode Exit fullscreen mode

This tells Git we want to track all the changes that have been made in the working directory. This still doesn't make the changes in the official version in your code that you can change in between them. Which is why we commit these changes from the index to the final stage, the Head. Using the command below

git commit -m "message saved with the commit."
Enter fullscreen mode Exit fullscreen mode

3.Head - This tells git that the changes are final and requires a message to be saved with the commit. After committing changes to the head, they are saved as a version on our computer but are not in our remote repository on GitHub.

In order to get these changes to our remote repository on GitHub, we need to push them to GitHub using the command below:

git push
Enter fullscreen mode Exit fullscreen mode

If you set up your project using git clone, then Git will already know where to push all the files to. But if you used git init to initialize a new project, then it will need to be told where to push these changes to. This can be done by:

  1. Creating a repository on GitHub
  2. Copy the ssh_url for cloning.
  3. Thereafter, you can use the origin command below. This tells Git that we have a remote repository called origin at the copied url.
git remote add origin ssh_url
Enter fullscreen mode Exit fullscreen mode

With the remote repo added, we can use the git push command to push the changes to the remote repo on GitHub.

So far... we can 🥷🏻🦸🏻‍♀️

  1. Create a repository.
  2. Make changes to it.
  3. Save the changes.
  4. Push the changes to GitHub.

What if 🤔

One of your team members wants to pull down the changes that we have pushed to GitHub on to their local computer, this is where we use the command below in their terminal and all the changes from GitHub will be pulled onto their local computer and merges with all the changes that they have:

git pull
Enter fullscreen mode Exit fullscreen mode

That's basically all you need to know about Git to get started!!!

Please make sure you leave a like 👍/loveheart ❤️ on this one. It encourages one to write more and more 🎉 🎊

Latest comments (3)

Collapse
 
roneo profile image
Roneo.org

The first commands contain 'Git' instead of 'git' (lowercase)

Collapse
 
jovialcore profile image
Chidiebere Chukwudi

So apt and useful piece

Collapse
 
estherwanjiru profile image
EstherWanjiru

Thank you so much.