Git and GitHub basics for beginners
Git and GitHub basics for beginners
This article explains how to use Git and GitHub for version control, aimed at developers who have never worked with version control.
Requirements
- Git 2.40 or higher (check with `git --version`) - A GitHub account (free at github.com) - Terminal or command prompt (macOS: Terminal, Windows: PowerShell, Linux: any shell)
Steps
1. Install Git
Download Git from git-scm.com or use your package manager:
Ubuntu/Debian
sudo apt update && sudo apt install gitmacOS (via Homebrew)
brew install git
Windows: download the installer from git-scm.com
Verify the installation:
git --version
2. Configure your identity
Git links each commit to a name and email address. You do this once:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"3. Create a repository
A repository is the folder where Git keeps track of your files and their history. Navigate to your project folder and initialize:
cd /path/to/your/project
git initThis creates a hidden `.git` folder in your project. Git now tracks all files in this folder.
4. Add files to the staging area
Git works in three stages: working directory, staging area, and repository. First, you add files to the staging area:
git add .
The dot adds all files in the current folder. To add one specific file, use:
git add filename.txt
5. Make your first commit
A commit is a snapshot of your files. It saves the state of the staging area into the repository history:
git commit -m "First commit"
The `-m` flag adds a message. Write a short, clear message that describes what you changed.
6. Create a repository on GitHub
GitHub is a cloud platform that hosts Git repositories. Log in to github.com, click on New repository, give it a name, and leave all options at their default settings. Copy the repository URL (for example `https://github.com/username/repositoryname.git`).
7. Connect your local repository to GitHub
Your local repository is not yet linked to GitHub. Add the remote:
git remote add origin https://github.com/username/repositoryname.git
`origin` is the default name for your remote repository. Now push your local commits to GitHub:
git push -u origin main
The `-u` flag links your local `main` branch to the remote `main` branch, so you only need to type `git push` from now on.
8. Continue working with branches
Branches let you work on new features without breaking the stable code. Create a new branch and switch to it:
git checkout -b feature/new-functionality
You are now on a separate branch. Changes you commit here do not affect the `main` branch. When you are done, merge the branch back:
git checkout main
git merge feature/new-functionalityVerification
Check that everything works:
git status
git log --oneline`git status` shows the current state of your working directory. `git log --oneline` shows a list of all your commits. If you see commits with your name and email address, Git is working correctly. On GitHub, you will see the same commits under the Commits tab of your repository.
Common problems
1. `git push` asks for a password
This happens when you use HTTPS and have not set up a credential helper. Solve it by using SSH:
git remote set-url origin git@github.com:username/repositoryname.git
First generate an SSH key with `ssh-keygen -t ed25519 -C "your@email.com"` and add the public key via GitHub → Settings → SSH and GPG keys.
2. `git merge` gives a conflict
Conflicts occur when two branches have changed the same line in the same file differently. Git marks conflicts with `<<<<<<<` and `>>>>>>>` in the file. Edit the file manually, remove the markers, and then run:
git add .
git commit -m "Merge conflict resolved"3. You accidentally committed a file that should not have been included
Remove the file from tracking but keep it locally:
git rm --cached filename.txt
git commit -m "Remove file from tracking"Then add the file to `.gitignore` so Git ignores it in the future.
Git and GitHub give you full control over every change in your codebase, and the ability to collaborate without overwriting each other's work — but it is only the first time you revert a commit or merge a branch that you realize how deep that control goes.