DEV Community

Brian O. Njagi
Brian O. Njagi

Posted on

Getting Started with Git: A Beginner's Guide to Version Control, Tracking Changes, Pushing, and Pulling Code

If you're new to coding or just dipping your toes into collaborative projects, you've probably heard the term "Git" thrown around. But what is it really? And why does everyone seem to be so devoted to it for managing code? In this article, we'll break it down step by step in a beginner-friendly way. No prior experience required
We'll cover the basics of version control, how to track changes in your code, and how to push and pull code from remote repositories.

What Is version Control and Why do we need it?

You ever changed something and wished you could back to how it was initially? Version control helps you see what you had before you made the changes. It is like the memory you thought you never needed. With version control you can always go back and forth making changes until you get the perfection you so desire.
One of the most popular version control systems is Git. And the good part is that it is free!!

Install Git and connect it to your github: Read this article I wrote

How to set up your First Git Repository

Open Git bash and make a new directory, say "my-first-project" and navigate into it by running the following commands
mkdir my-first-project
cd my-first-project

Initialize Git: This enables git to start tracking this folder.
git init
There is a hidden .git folder created. Here is where git does its thing!

Now the repo is ready and we can add a few things such as a html file or a python file

Tracking Changes: Staging, Committing, and Viewing History

Open VS code => file => open folder
Then select the folder you just created "my-first-project" and create a new file in this folder by selecting "new file" on VS code.
Name the file "hello.py" and add this line of code inside it print("Hello Git!")

Check repository status

git status - the output will show no commits and untracked files.

Staging changes

In order for git to track files, the files must first be staged.
git add hello.py - stages a specific file
If more than one file needs staging then you just name it after the first one as below
git add hello.py index.html (another file etc...)
To stage all changes
git add .
Check what has been staged
git status

Commit Changes

A commit records the staged changes with a message describing what you did.
git commit -m "name of file created"
To see your commit history
git log

Remote Repositories: Pushing and Pulling Code

All the above actions happen locally in your computer thus, we created a local repository. This means you can only access the files and track them only on your computer.
Now let us go a little further.
When we want to access our codes in a remote/different computer we have to create remote repositories which are going to be hosted on Github.
We create them locally and then push them to github. Pushing code, in a simpler term just means uploading while pulling is just downloading.

1. Create a Remote Repo: Sign up or log in to your github account and create a new repository and name it my-first-project, finally give it a description . Do not initialize with a README yet. Copy the repo URL.

2. Link Local to Remote: In your git bash terminal
git remote add origin https://github.com/yourusername/my-first-project.git
NB paste the URL (https/ssh) from your github in the place of "https://github.com/yourusername/my-first-project.git" in the above code.

Verify connection git remote -v

Push to Github

git push -u origin main for the first push
then git push for subsequent uploads.

NB: Git will prompt you to log in to your github account to authenticate it is you.

Pulling from github

Say you are collaborating with someone and they make changes, Pulling enables you to download the changes and continue from where they left off.
git pull origin main

Top comments (0)