GPTT Developer Blog

← Back to Home

Mastering Git & GitHub for Developers: A Practical Guide

📖 1,531 Words

Git and GitHub are essential tools for modern software development. Git allows developers to track changes in their source code, while GitHub provides a platform for hosting repositories, collaborating with other developers, and managing software projects.

Whether you are building a PHP application, Flutter mobile app, Python automation project, or another software system, understanding Git and GitHub can make your development workflow more organized, safer, and easier to maintain.

What you will learn: How to install Git, configure your identity, create repositories, commit changes, create branches, connect projects to GitHub, push and pull code, and follow a practical Git workflow.

1. What is Git?

Git is a distributed version control system used to track changes in files and source code. Instead of keeping multiple copies of a project such as project-final, project-final-2, and project-final-new, Git allows developers to keep a history of changes inside one repository.

Each commit represents a point in the development history that can be inspected or restored when necessary.

2. What is GitHub?

GitHub is a cloud-based platform that hosts Git repositories. Developers can use GitHub to store projects remotely, collaborate with other developers, review code, manage issues, and maintain project documentation.

Git and GitHub are related but they are not the same thing. Git is the version control system, while GitHub is a platform that can host Git repositories and provide collaboration features.

3. Git vs GitHub

Git
  ↓
Version Control System
  ↓
Tracks Changes
  ↓
Creates Commits
  ↓
Manages Branches


GitHub
  ↓
Online Repository Platform
  ↓
Hosts Git Repositories
  ↓
Collaboration
  ↓
Pull Requests
  ↓
Issues and Project Management

4. Installing Git

First, install Git on your computer. After installation, open Command Prompt, PowerShell, Terminal, or another terminal application and verify that Git is available.

git --version

If Git is installed correctly, the terminal should display the installed Git version.

5. Configure Git

Before creating commits, configure the name and email address that Git will associate with your commits.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

You can check the current configuration using:

git config --global --list

6. Create a Git Repository

Navigate to your project directory using the terminal.

cd my-project

Initialize Git inside the project:

git init

Git will create a hidden .git directory that stores the repository history and configuration.

7. Check Repository Status

One of the most useful Git commands is git status. It shows which files have been modified, added, or are currently untracked.

git status

8. Add Files to the Staging Area

Before creating a commit, changes need to be added to the staging area.

To stage all changes:

git add .

You can also stage a specific file:

git add index.php

9. Create a Commit

A commit records a set of staged changes in the Git history.

git commit -m "Add login system"

Commit messages should describe what changed instead of using vague messages such as update or changes.

10. View Commit History

You can inspect previous commits using:

git log

A shorter version of the history can be displayed with:

git log --oneline

11. Create a GitHub Repository

After creating your local Git repository, create a new repository on GitHub.

The repository can be used as the remote location where your project will be stored online.

After creating the repository, Git can be connected to it using a remote URL.

12. Connect Local Git to GitHub

Add the GitHub repository as the remote named origin.

git remote add origin https://github.com/username/project.git

Check the configured remote:

git remote -v

13. Rename the Main Branch

Many modern repositories use main as the primary branch.

git branch -M main

14. Push the Project to GitHub

After creating your first commit and configuring the remote, push the project to GitHub.

git push -u origin main

The -u option associates the local branch with the remote branch so future pushes can be simpler.

15. The Basic Git Workflow

A typical development cycle can be represented as:

Edit Code
    ↓
git status
    ↓
git add .
    ↓
git commit
    ↓
git push
    ↓
GitHub

16. Pull Changes from GitHub

When changes exist on the remote repository, use git pull to retrieve and integrate them into the local branch.

git pull origin main

This is especially important when working on a project from multiple computers or collaborating with other developers.

17. Branching

Branches allow developers to work on features or fixes without directly modifying the main branch.

Create a new feature branch:

git checkout -b feature-login

Newer Git versions also support:

git switch -c feature-login

18. Working with a Feature Branch

After creating the branch, make your changes and commit them.

git add .
git commit -m "Add login feature"

The feature branch can then be pushed to GitHub.

git push -u origin feature-login

19. Switching Between Branches

To switch back to the main branch:

git switch main

You can also use the traditional command:

git checkout main

20. Merging a Branch

After completing and testing a feature, the branch can be merged into the main branch.

git switch main
git pull origin main
git merge feature-login

After a successful merge, push the updated main branch:

git push origin main

21. Using Pull Requests

GitHub Pull Requests provide a way to review proposed changes before they are merged into the main branch.

Feature Branch
      ↓
Push to GitHub
      ↓
Pull Request
      ↓
Code Review
      ↓
Testing
      ↓
Merge
      ↓
main

Pull Requests are especially useful for teams because another developer can review the changes before they become part of the main project.

22. Using .gitignore

Not every file in a project should be committed to Git. Temporary files, build directories, environment configuration, and sensitive credentials should normally be excluded when appropriate.

Git uses a .gitignore file to define files and directories that should not be tracked.

node_modules/
.env
build/
dist/
*.log

The exact entries depend on the technology and project structure.

23. Viewing Changes

Before committing changes, inspect what has changed.

git diff

To inspect staged changes:

git diff --staged

24. Undoing Changes

Git provides several commands for undoing changes, but they should be used carefully because some operations can permanently discard work.

To restore an unstaged file to its previous state:

git restore filename.php

Always inspect your repository status before performing destructive operations.

25. Useful Git Commands

  • git init - initialize a repository
  • git status - check repository status
  • git add . - stage changes
  • git commit - create a commit
  • git log - view commit history
  • git branch - manage branches
  • git switch - switch branches
  • git merge - merge branches
  • git pull - retrieve remote changes
  • git push - upload local commits
  • git clone - copy a remote repository
  • git remote - manage remote repositories

26. Clone an Existing Repository

If a project already exists on GitHub, you can download its Git repository using git clone.

git clone https://github.com/username/project.git

After cloning, enter the project directory:

cd project

27. Recommended Commit Strategy

Good commits make project history easier to understand. Instead of creating one huge commit containing unrelated changes, separate logical changes into smaller commits.

git add login.php
git commit -m "Add login validation"

git add dashboard.php
git commit -m "Add dashboard authentication"

28. Common Git Mistakes

  • Committing passwords or API keys
  • Using unclear commit messages
  • Working directly on main for every feature
  • Forgetting to pull remote changes
  • Ignoring merge conflicts
  • Adding unnecessary build files
  • Committing sensitive environment files
  • Using force push without understanding its effects

29. Handling Merge Conflicts

A merge conflict can occur when Git cannot automatically combine changes from different branches.

git pull origin main

When a conflict occurs, Git marks the affected files. The developer must inspect the conflicting sections, decide which changes should remain, and then stage the resolved files.

git add .
git commit -m "Resolve merge conflict"

30. A Practical GitHub Development Workflow

Clone Repository
      ↓
Create Feature Branch
      ↓
Develop Feature
      ↓
Test Application
      ↓
git status
      ↓
git add
      ↓
git commit
      ↓
git push
      ↓
Create Pull Request
      ↓
Review
      ↓
Merge
      ↓
Update Local main

31. Git for Solo Developers

Git is not only useful for teams. Solo developers can use Git to maintain a history of their projects, experiment with features, create backups through remote repositories, and safely test changes using branches.

32. Git for Development Teams

Teams can use branches, Pull Requests, code reviews, and issue tracking to organize development work.

Developer A
     ↓
Feature Branch
     ↓
GitHub
     ↓
Pull Request
     ↓
Code Review
     ↓
main

Developer B
     ↓
Bug Fix Branch
     ↓
GitHub
     ↓
Pull Request
     ↓
Code Review
     ↓
main

33. Best Practices

  • Commit logical changes frequently
  • Write meaningful commit messages
  • Use feature branches for development
  • Keep the main branch stable
  • Pull changes before starting new work
  • Review changes before committing
  • Use a proper .gitignore file
  • Never commit passwords or private credentials
  • Keep project documentation updated
  • Use Pull Requests when working with a team

34. Git and GitHub Workflow Summary

Project
   ↓
git init / git clone
   ↓
Create Branch
   ↓
Write Code
   ↓
git status
   ↓
git add
   ↓
git commit
   ↓
git push
   ↓
GitHub
   ↓
Pull Request
   ↓
Review
   ↓
Merge
   ↓
main

Conclusion

Git and GitHub provide a powerful foundation for managing modern software projects. Git gives developers version control, while GitHub adds remote repository hosting and collaboration features.

By understanding repositories, commits, branches, remotes, pull and push operations, Pull Requests, and merge conflicts, developers can build a more reliable and organized development workflow.

Key takeaway: Git is more than a backup tool. It gives you a history of your project, while GitHub provides a place to collaborate, review, share, and manage that code.

Article Statistics
📖 1,531 Words
Back to Top