Sitemap
Stanelyjones

Stanelyjones

Press enter or click to view image in full size

Git & GitHub for Beginners: From Basic Commands to Creating Repositories Through the Terminal

3 min read1 day ago

--

When I started learning Python, I also started using Git and GitHub to maintain my practice files and track my progress.

Initially, I knew only a few commands like git add, git commit, and git push. However, I wanted to understand what actually happens behind these commands and how to manage repositories and branches independently.

This article documents my learning journey with Git and GitHub.

1. Understanding Git vs GitHub

Git is a version control system that tracks changes in our code.

GitHub is an online platform where we can store Git repositories, collaborate with others, and maintain our project history.

2. Basic Git Commands

These are the commands I started practising:

CommandPurposegit initInitialize Git in a project foldergit statusCheck the current Git statusgit add .Stage changesgit commit -m "message"Save a snapshot locallygit pushUpload commits to GitHubgit pullDownload and integrate changes from GitHubgit logView commit history

3. Understanding Repository and Branch

A repository represents an entire project.

A branch represents a separate line of development within that repository.

For example:

python-project/
│
├── main
├── practice-dictionary
└── practice-loops

Here, main, practice-dictionary, and practice-loops are branches inside the same repository.

Creating a New Branch

git switch -c practice-dictionary

This command creates a new branch and switches to it.

To list branches:

git branch

To switch back to the main branch:

git switch main

To push a new branch to GitHub:

git push -u origin practice-dictionary

Note: Git does not technically enforce parent-child branches. The starting point of a branch is determined by the branch we are currently on when creating it.

4. Creating a New GitHub Repository from the Terminal

One interesting thing I learned is that we don’t necessarily need to open the GitHub website to create a repository.

We can use GitHub CLI (gh) directly from our terminal.

Step 1: Install GitHub CLI

On Windows, we can install it using:

winget install --id GitHub.cli --source winget

Verify the installation:

gh --version

Step 2: Authenticate with GitHub

gh auth login

Follow the browser authentication instructions.

Get Stanelyjones’s stories in your inbox

Join Medium for free to get updates from this writer.

Verify authentication:

gh auth status

Step 3: Create a Local Project

mkdir python-dictionary-practice
cd python-dictionary-practice
git init

Create a Python file:

New-Item main.py

Step 4: Create the GitHub Repository and Push

gh repo create python-dictionary-practice --public --source=. --remote=origin --push

This command can create the GitHub repository, configure the remote named origin, and push the local project.

5. My Git Learning Cheat Sheet

Task — — — — — — — — — — — — Command

Initialize repository — —- — — — — — — git init

Check status- — — — - — — — — — — — -- git status

Stage files — — — — — — — — — — - — — git add ./git add <filename>

Commit changes — — — — — — — — git commit -m "message"

Create branch — — — — — — — — - — — git switch -c branch-name

Switch branch — — — — — — — — — — — git switch branch-name

List branches — — — - — — — — — — — -- git branch

Check remote — — — — — — — — - — — — git remote -v

Push changes — — — — — — — — — — — - — git push

Pull changes — — — — — — — — — — — — — git pull

View commits — - — — — — — — — — — — — — git log

Login to GitHub CLI - — — — - — — — — — — gh auth loginCreate

GitHub repository — — — — — — — — — — -- gh repo create

Conclusion

Learning Git and GitHub alongside Python has helped me understand how developers manage code, track changes, and maintain projects.

My next learning goals are:

  • Understanding branch merging
  • Learning pull requests
  • Resolving merge conflicts
  • Managing multiple branches effectively
  • Building projects and maintaining them on GitHub

--

--