DEV Community

David
David

Posted on

A Beginner’s Guide to Git: Version Control, Tracking Changes, and Pushing Code to GitHub

Understanding Push, Pull, and Commit

If you're new to programming, terms like Git, GitHub, push, pull, and commit might seem confusing.

This guide breaks down these concepts step by step, using real commands from a beginner’s workflow.


What You’ll Learn

  • What Git and version control are
  • How to configure Git for the first time
  • How to track file changes
  • How to push and pull code using GitHub

Understanding Version Control

Version control is a system that helps you:

  • Track changes made to files over time
  • Revert to previous versions when something breaks
  • Collaborate safely with others on the same project

Git is the most popular version control system, while GitHub is an online platform where Git repositories are stored and shared.


Setting Up Git

Before using Git, configure your identity. This information appears in your commit history and helps others know who made changes.

git config --global user.name "dmungai"
git config --global user.email "dmungai@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Verify your configuration worked:

git config --global user.name
git config --global user.email
Enter fullscreen mode Exit fullscreen mode

If Git prints your username and email, you're all set


Git Repositories Explained

A Git repository is simply a folder that Git is tracking.

If you run Git commands outside a repository, you’ll encounter this error:

fatal: not a git repository (or any of the parent directories): .git
Enter fullscreen mode Exit fullscreen mode

This means you're not inside a Git-tracked project folder.


Creating Your Working Environment

Start by creating a folder for your project:

mkdir testfolderforcredentials
cd testfolderforcredentials
Enter fullscreen mode Exit fullscreen mode

This folder remains a normal directory until you introduce Git.


Cloning from GitHub

To work on an existing project, clone it from GitHub:

git clone https://github.com/dmungai97/dmungai.git
cd dmungai97
Enter fullscreen mode Exit fullscreen mode

Cloning does three things:

  1. Downloads the project
  2. Creates a .git folder inside it
  3. Automatically connects it to GitHub

Creating and Tracking Files

Create a new file and add some content:

vi testfile
cat testfile
Enter fullscreen mode Exit fullscreen mode

Git doesn't automatically track new files. Check what Git sees:

git status
Enter fullscreen mode Exit fullscreen mode

You'll see untracked files listed.

To start tracking them:

git add testfile
git add testfile.txt
Enter fullscreen mode Exit fullscreen mode

Run git status again to see the files are now staged and ready to be committed.


Committing Your Changes

A commit creates a snapshot of your project at a specific point in time:

git commit -m "this is test file for credentials - 1st method"
Enter fullscreen mode Exit fullscreen mode

Commits are saved locally

Good commit messages are clear and concise


Pushing to GitHub

Send your commits to GitHub so others can see them:

git push
Enter fullscreen mode Exit fullscreen mode

Your files are now saved locally and visible on GitHub.


Pulling Updates

If changes were made on GitHub or by another team member, download them:

git pull
Enter fullscreen mode Exit fullscreen mode

Always pull before starting new work to avoid conflicts.


Quick Reference

  • git clone – Download a repository from GitHub
  • git status – Check which files are tracked or modified
  • git add – Stage files for commit
  • git commit -m "message" – Save changes
  • git push – Upload commits to GitHub
  • git pull – Download latest changes

Top comments (0)