DEV Community

Cover image for Git Made Simple
Fariq
Fariq

Posted on

Git Made Simple

It is normal for developers to save different versions of their work, track changes, make changes, and collaborate; this is the work of Git.

What is Git

Git is a version control system that lets one:

  • Track changes
  • Go to earlier code versions
  • Collaborate
  • Save code online on GitHub

Steps

Step 1: Installing Git Bash

Go to Git Bash

Select your Operating System, e.g., Windows, macOS, or Linux

Git Bash Website

Install Bash

Open Git Bash

You will see a terminal that looks like this:

username@computer MINGW64 ~

Step 2: Create (or Log Into) a GitHub Account

  1. Go to Github

  2. Create an account or log in if you already have one

Step 3: Connect Git Bash to GitHub

Set up your name

git config --global user.name "Your Name"

Set up your email

git config --global user.email "youremail@example.com"

Note: Make sure the email matches your GitHub email.

Check Your Configuration:

git config --list

Step 4: Create a New Project or Repository(repo)

Create a Folder

mkdir my-first-git-project

Navigate to the now-created folder

cd my-first-git-project

Git Bash Terminal

Initialize Git

git init

Now Git is watching this created project

Git Bash Terminal

Step 5: Track Changes with Git

Create a File

touch index.html

Open the Folder Directly from Git Bash

Input this in Git Bash and press enter:

code .

This opens VS Code: The dot (.) means “this current folder."

Step 6: Create the First code in VS Code

Open index.html and add this code:

<!DOCTYPE html>
<html>
<head>
  <title>My First Git Project</title>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Code iamge

Step 6: Check Git Status in Git Bash

Return to Git Bash and run:

git status

Git will show index.html as untracked.

This means Git sees the file, but it has not started tracking it yet.

Git Bash Terminal

Step 7: Add the File to Git Tracking

Track the index.html by running:

git add index.html

Or add everything using:

git add .

Step 8: Commit Your Changes

git commit -m "Initial HTML file commit"

The m means a message to be added to the commit

Step 9: Create a Repository on GitHub (Online)

Now that your project exists locally on your computer, the next step is to create an online repository on GitHub where your code will be stored and shared.

Make sure you are logged in to Github

On the right top corner of the github website, click the + icon.

Select New repository

Enter a Repository name; use the same name as your folder i.e:

my-first-git-project

Choose Public

Scroll to the bottom and click Create repository

Github will now show some commands

Github website

Step 10: Connect Your Local Project to GitHub

Return to Git Bash and run:

git remote add origin https://github.com/YOUR-USERNAME/my-first-git-project.git

Replace YOUR-USERNAME with your actual GitHub username

Step 11: Rename the Default Branch**

Run:

git branch -M main

Step 12: Push Your Code to GitHub

Run:

git push -u origin main

Note:

  • git push - send your code online

  • origin - the GitHub repository

  • main - the branch name

  • -u - remembers this destination for future pushes

Github Website

Step 13: Go back to your GitHub repository page and Refresh the page

You will now see the index.html file and the commit message

Step 14: Pull Code from GitHub

This will let the latest code be pulled or downloaded back to your local machine, where you can again edit the code. Run:

git pull origin main

GitHub

Summarized Git Workflow

Git Workflow

Top comments (0)