Git for GitHub: How to use simple Git commands to manage a GitHub repository
Source: Dev.to
The Story
Recently, I was working on creating a website on a cloud‑based IDE (CodeHS). One night I finished editing, turned off my monitor, and disabled my mouse and keyboard. The next day at school I continued working and made significant changes. When I got home and made more changes, I realized something very important: the cloud‑based IDE hadn’t refreshed to the new code, so when I saved my new work it overwrote the work I did at school.
What’s the point of telling you this? It isn’t “always close your editor” or some other trick. After the initial panic I discovered the IDE’s History section, which logs every change to every file. I copied the lost changes from the history and restored them.
Now imagine you aren’t using a cloud‑based IDE—just editing a file locally in an IDE or the terminal. When something breaks, the previous states of your files can be lost forever.
Using a version control system (VCS) like Git prevents that: you can always go back to previous commits (snapshots of your code).
The work I almost lost wasn’t critical, but imagine losing important code for a job or a serious project.
What Is Git?
Git is the most popular VCS in the world. It’s free, open‑source, and actively maintained.
“By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.”
— Atlassian
In this guide you will learn how to:
- Install Git.
- Configure your identity.
- Initialize a local repository.
- Commit and push changes to a GitHub repository.
Step 1 – Install Git
Open a terminal
| OS | How to open |
|---|---|
| Windows | Press Windows Key, type PowerShell, and press Enter. |
| macOS | Press ⌘ Space, type Terminal, and press Enter. |
| Linux | Press Ctrl + Alt + T. |
Verify the installation
git --version
If a version number appears, you’re ready for Step 2.
If the command is not recognized, follow the OS‑specific instructions below.
Windows
-
Via Winget
winget install --id Git.Git -e --source winget -
Via installer – download the standalone installer from the official site:
macOS
-
Via Homebrew (install Homebrew first if you don’t have it)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install git
Linux
Use your distribution’s package manager:
| Distribution | Command |
|---|---|
| Debian/Ubuntu | sudo apt-get install git |
| Fedora | sudo dnf install git |
| Arch | sudo pacman -S git |
Step 2 – Configure Your Identity
Git attaches a name and email to every commit. Set them globally:
# Your display name (real name, GitHub username, etc.)
git config --global user.name "Your Name"
# Your email (must match the email on your GitHub account)
git config --global user.email "your-email@example.com"
You can verify the settings with:
git config --global --list
Step 3 – Initialize a Local Repository
-
Navigate to the folder where you want the project:
cd path/to/your/project -
Create the repository and set the default branch to
main:git init -b main -
Stage all files (tell Git to start tracking them):
git add . -
Commit the staged files with a clear, imperative‑mood message:
git commit -m "Initial commit"Example of good commit style:
Add CSS rule to fix whitespace(not “Added CSS rule …”).
Step 4 – Create a GitHub Repository & Link It
- Create a GitHub account if you don’t already have one.
- Sign in at .
- Click the green “New” button (top‑right of the Repositories list) or go directly to .
- Fill in a repository name.
- Leave all “Add …” boxes unchecked (no README, .gitignore, or license for now).
- Click Create repository.
Add the remote and push your local commit
# Replace USER and REPO with your GitHub username and repository name
git remote add origin https://github.com/USER/REPO.git
# Push the local main branch to GitHub
git push -u origin main
You should now see your code on GitHub.
Recap
- Never rely on a single copy of your work.
- Git gives you a complete history of every change, letting you recover lost work instantly.
- Follow the steps above to install Git, configure it, initialize a repository, and push to GitHub.
Happy coding! 🚀
Create Repository
- Click Create Repository.
- Remember the URL for your repository – you’ll need it in the next step.
Link Your Local Project to the Repository
Navigate to your local project folder and run the command below, replacing “ with the URL you saved in step 2.
git remote add origin
Pushing to GitHub
Now push your local commits to the remote repository:
git push -u origin main
- The
-uflag sets upstream tracking, linking your localmainbranch to the remoteorigin/main. Future pushes can be done with justgit push.
You have now:
- Initialized a local Git repository
- Tracked your changes
- Pushed the code securely to GitHub
Your workflow is much simpler now that the local project is linked to a remote repository.
Typical Git Workflow (Four Core Commands)
| Command | What It Shows / Does |
|---|---|
git status | • Current branch |
| • How many commits you’re ahead/behind the remote | |
| • Unstaged changes | |
| • Untracked files | |
git add (or git add .) | Stage files (tell Git to track them) |
git commit -m "COMMIT MESSAGE" | Save a snapshot locally (still only on your machine) |
git push | Send committed changes to the remote repository |
Tip: Run
git statusbefore you start working to see the current state of your repository.
If you ever need to be explicit about the remote and branch, you can use:
git push origin main
Adding a .gitignore File
A .gitignore file tells Git which files not to track. Place it in the root of your project and add one rule per line.
Common Syntax
#– comment linesecret.txt– ignore this exact fileprivate/– ignore the directory and everything inside it*.txt– ignore all files ending with.txtlog*– ignore any file that starts withlog**/logs– ignore any directory namedlogsat any depth!important.txt– re‑include a file that was previously ignored/config.yaml– pattern anchored to the location of the.gitignorefile (not the filesystem root)
Why Version Control Matters
Imagine you’re developing an Automated Market‑Making (AMM) system that places buy and sell orders across many stocks. A bug in the code could cause costly errors for the company.
- Without a VCS: Downtime and financial loss could be severe because you can’t easily revert to a known‑good state.
- With Git: You can roll back to a previous commit, fix the issue, and redeploy quickly, saving time and money.
Version control also enables:
- Collaboration: Multiple developers can work on the same codebase simultaneously.
- Code Review: Changes can be reviewed and approved before merging.
Even for solo projects, not using a VCS can lead to lost progress and frustration.
Writing Good Commit Messages
A clear commit history is essential. While you’re still improving your commit messages, consider adopting the Conventional Commits standard. It provides a structured format that makes logs easier to read and automate.
I won’t detail the standard here, but you can read a concise summary here.