When I’m coding, I sometimes accidentally delete important parts of my script. Sometimes I even spend hours trying to "undo" my mistakes from the last hour or so. I also have a ton of saved files (script_v1_final_REAL_final.py, for example). If you’ve been coding for more than a week, then chances are you have accidentally deleted important parts of your script, spent hours undoing your work, or even saved tons of “final” versions of your script.
Modern software development involves more than just writing code. With the complexity of development increasing, manual backups and shared folders are not enough. This is where Git Version Control and GitHub come in. Together, they form the backbone of modern software engineering. Software developers around the world use these tools to track changes, collaborate on projects, and even roll back errors that have been made.
Whether you are a software engineer in training, a data analyst, a graphic designer in technology, or anyone else who creates digital products, these are essential tools to master. Below, we will go through step by step the process of creating your first local repository and ultimately uploading to global open-source projects via smooth pull requests in GitHub in this ultimate Git tutorial and GitHub tutorial for the absolute beginner.
Git vs. GitHub: What Is the Difference?
Before diving into commands, let’s clear up one of the biggest points of confusion for newcomers: Git is not GitHub. While they are often mentioned in the same breath, they serve two distinct yet complementary roles in your development workflow.
What is Git?
Git is a distributed Git Version Control system that runs locally on your computer. Created by Linus Torvalds in 2005, Git tracks changes in your files over time. It acts like a digital time machine, saving snapshots of your codebase so you can jump back to any previous point whenever something breaks. Because it is distributed, every developer has a complete copy of the project history right on their hard drive, allowing them to work offline effortlessly.
What is GitHub?
GitHub is a cloud-based hosting service for Git repositories. If Git is the tool that tracks your code locally, GitHub is the online hub where you upload that code to share, backup, and collaborate with team members across the globe. GitHub adds social and managerial tools on top of Git, such as issue tracking, pull requests, automated CI/CD Pipelines, and project boards.
Feature
Git
GitHub
Type
Command-line tool / software
Cloud web service / platform
Location
Runs locally on your machine
Hosted on the cloud
Primary Role
Tracks local file history & commits
Hosts remote repositories & collaboration
Offline Capability
Works completely offline
Requires internet connection
Key Advantage
Fast, flexible version tracking
Team reviews, forks, & open-source hosting
Getting Started: Installation & First-Time Setup
This Git tutorial starts with the installation of Git and the configuration of your identity.
Step 1: Install Git
Windows: Download Git installer from git-scm.com. Then, run Git Bash.
macOS: Type git --version in your terminal. If Git isn’t already installed, the installation of Git will prompt you to also install the Xcode Command Line Tools.
Linux: You can install git using your package manager. For example on Ubuntu/Debian systems: sudo apt install git-all.
Step 2: Configure Your Global Credentials
Every time you save a snapshot in Git, it attaches your name and email address. This ensures everyone on a team knows who wrote what code. Open your terminal or Git Bash and run:
Bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
You can always check back on the changes you made to your global Git configuration using git config --list.
Understanding the Core Architecture: The 3 Stages of Git
There are three primary states or zones in which your project files exist when using Git. These are described in more detail below.
- Working Directory: The sandbox where you actively create, edit, and delete files.
- Staging Area (Index): A ‘draft board’ where you choose the changed files you want to be part of the next commit.
- Repository (.git directory): The permanent database where Git securely stores committed snapshots of your project.
+------------------+ git add +------------------+ git commit +------------------+
| Working Directory| -----------------> | Staging Area | ----------------> | Local Repository |
| (Your active edits)| | (Drafting space) | | (Saved history) |
+------------------+ +------------------+ +------------------+
Core Git Commands: Step-by-Step Hands-On Guide
A Programmer’s Daily Routine of Using Git Version Control Software includes conceptual knowledge of what Git does and the following routines of using Git’s essential commands.
1. Initializing a Repository (git init)
Start tracking changes to an existing project or a brand new project by running the following commands:
Bash
cd path/to/my-project
git init
This command creates a hidden .git folder in your directory, initializing your brand-new Git Version Control repository.
2. Checking Status (git status)
The most used Git command is probably the status check command: git status. It shows which files have been modified, which files have been already added to the repository (i.e. “staged”), and which new files have been found in the repository but not added yet (i.e. “untracked”).
Bash
git status
3. Staging Files (git add)
When you modify files in a Git repository, those changes are not included in a subsequent snapshot until you have staged them to be part of that snapshot.
Bash
# Stage a specific file
git add index.html
# Stage all modified and new files in the current folder
git add .
4. Committing Changes (git commit)
A commit is a permanent snapshot of your staged changes. Using the command above creates a commit. Always use the most important information in the commit message and write your commit messages in the imperative tense.
Bash
git commit -m "Add responsive navigation header"
5. Viewing Commit History (git log)
To view your project’s historical timeline, run:
Bash
git log --oneline
Mastering Branching & Merging
Branching is where Git really beats traditional software development tools. Git’s notion of a branch means that you can develop a feature or fix a number of bugs entirely in isolation from the ‘stable’ code in the main branch.
Creating and Switching Branches
To create a new feature branch and switch to it immediately:
Bash
# Modern command to create and switch branch
git switch -c feature/login-page
# Traditional command
git checkout -b feature/login-page
All commits to the newly created branch will then be added to the feature/login-page branch.
Merging Branches
After you finish your development, you should merge it into the main branch (for example, the main branch for this tutorial is named “main”).
Bash
# 1. Switch back to main
git switch main
# 2. Merge feature branch into main
git merge feature/login-page
Handling Merge Conflicts
A merge conflict occurs when Git encounters changes to the same lines of code within two different branches, as shown in the plaintext snippet below.
Plaintext
<<<<<<< HEAD
<h1>Welcome to Our Store</h1>
=======
<h1>Welcome to Our Premium Shop</h1>
>>>>>>> feature/login-page
To resolve it:
Open the affected file and choose which code to keep.
Remove the marker lines (<<<<<<<, =======, >>>>>>>).
Now save the file, go back to the Git terminal, and use git add . followed by a git commit.
GitHub for Beginners: Cloud Collaboration Made Easy
In this GitHub tutorial section we will show you how to connect your code to GitHub, the web-based platform for developers, and share your code with millions of other developers from around the world.
Step: Create a GitHub Account & New Remote Repository
Sign up for a free account at github.com.
After logging in to GitHub, you can create a new remote repository by clicking on the “+” icon in the upper right corner of the GitHub main page and selecting “New repository."
Create a new repository on GitHub by clicking the "+" icon in the upper right corner and selecting New repository. Fill out the repository information, such as repository name (e.g., awesome-web-app), and select whether to make it a public or private repository. Click Create repository when finished.
Step 2: Push Your Local Repository to GitHub
GitHub will display commands to link your existing local directory to the newly created remote repository. Run these commands in your local terminal:
Bash
# Add the remote origin link
git remote add origin https://github.com/your-username/awesome-web-app.git
# Set default branch to main and push
git branch -M main
git push -u origin main
Your code and the whole history of all changes in your code have been published on GitHub.
Step 3: Cloning an Existing Repository
To clone an existing repository to a local computer, type git clone in the terminal. If you downloaded the local repository as a zip file, type "git clone" followed by the path to the downloaded zip file in the terminal.
Bash
git clone https://github.com/username/repository-name.git
Daily Collaborative Workflow: Pulling and Pushing
Your daily work in a team environment is synchronizing your local changes with the server on GitHub.
+-----------------------+ +-----------------------+
| Local Repository | --- git push ---> | Remote (GitHub) |
| (Your Computer) | <--- git pull --- | (Cloud Storage) |
+-----------------------+ +-----------------------+
- git pull origin main: Downloads and merges the latest updates from GitHub into your active local branch. Always pull before starting new work to prevent conflicts!
- git push origin feature-branch: Uploads your local commits to GitHub so your teammates can review your work
Working with Pull Requests & Code Reviews
GitHub Pull Requests (PR) let us tell our colleagues that we have added some new code to a branch in our GitHub repository for review and testing before it gets merged into our production code.
How to Create a Pull Request on GitHub:
If your feature has progressed to the point where you want to share your code with others, you can push your feature branch to GitHub using the following command: git push origin feature/new-api
You go to the GitHub website, select your repository and see a banner on top saying "Compare & pull request review."
Provide a helpful title and summary for the pull request.
Tag team members for code review and submit.
The developer with the pull request simply clicks Merge pull request to allow the changes to be incorporated into the production code in the main branch.
Git Best Practices for Professional Developers
Below is a step-by-step guide on best practices when using Git in your workflow as a professional developer. Learn Git and GitHub in no time, and apply these to your workflow as you progress in your developer career.
Commit Early, Commit Often: Commits should be atomic, i.e., they should be used to complete a single task or fix, and you should commit early and often.
Writing commit messages is a simple task but requires care to avoid writing messages of poor quality like ‘fixed bug’ or 'stuff,' and always start a commit message with a verb (e.g. Add, Fix, Refactor, Update, etc.).
Use a .gitignore file: never commit passwords, API keys, compiled binaries and more. Create a .gitignore file in your root folder and add the paths to files and folders that you don’t want to add to your repository.
Never commit directly to the main branch of a Git repository—always create feature branches and use pull requests to merge in code.
Upgrade Your Developer Career with Modern Developer Tools
Mastering GitHub for Beginners is the first step to becoming a job-ready developer. Memorizing terminal commands to use GitHub is only the first step of software engineering mastery. Hands-on practice, real-world projects and teamwork with other developers are the next steps to becoming a real software engineer.
In addition to mastering GitHub for beginners, if you are serious about growing your tech career, we have the tools to help you learn how to become a software engineer and then grow as a software engineer with interactive paths, labs with real-world scenarios, and code reviews with experienced mentors.
Join thousands of developers who transformed from absolute novices to senior engineers by mastering Git, modern frameworks, and DevOps pipelines!
Got Questions? Here Are Some FAQs
1. What is the difference between git pull and git fetch?
This can help to track what other developers have been doing in your remote repository. You can download their commits, files and files, and also their local ref names by using the git fetch command. Note that fetch will not modify your local files, as described above in the merge section.
2. How do I discard uncommitted local changes in Git?
To restore the file as it was the last time it was committed (i.e. discard all changes you made), use `git restore `. To unstage a file again (i.e. un-stage it but retain changes to file), use `git restore --staged `.
3. What is a .gitignore file and why is it important?
A .gitignore file is used to tell Git to ignore certain files and/or directories, which will not be tracked by Git tracked. These types of files that should not be added to public repositories for security reasons, such as secret key files, operating system temporary files (such as .DS_Store files created by Macs), and large dependencies (such as the node_modules/ directory for projects that use Node.js).
4. How can I undo the last commit without losing my code?
To undo a previous commit while keeping all of your code (i.e. not discarding any changes) you can do a git reset --soft HEAD~1. This will “undo” your last commit but leave the changes to be edited or committed again in your Staging Area.
5. Do I need to know the command line to use Git and GitHub?
While knowing the command line (CLI) would grant you the greatest amount of speed, flexibility, and control, it is not required. The variety of Graphical User Interfaces (GUIs) for version control, including GitHub Desktop, VS Code (with a Source Control tab), and SourceTree, can be used to perform the various tasks for version control.
SevenMentor
Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.