How to Use Git and GitHub for Version Control and Collaboration
How to Use Git and GitHub for Version Control and Collaboration

Whether you’re building a website, developing a game, creating a mobile application or working on a large software project, keeping track of changes to your code is essential.
That’s where Git and GitHub come in.
Git allows you to track changes to files and create a history of your project, while GitHub provides an online platform where Git repositories can be stored, shared and collaborated on with other developers.
In this guide, we’ll look at how Git and GitHub work together and walk through a typical workflow, from creating a repository to making changes and collaborating with other developers.
What Is Git?
Git is a distributed version control system. In simple terms, Git keeps a history of changes made to your project.
Instead of having folders such as:
MyProject_Final
MyProject_Final2
MyProject_Final_ReallyFinal
MyProject_Final_ReallyFinal_v2
you can use Git to maintain a structured history of your project.
You can see what changed, when it changed and who made the change. More importantly, you can return to an earlier version if something goes wrong.
What Is GitHub?
GitHub is an online platform built around Git repositories. A Git repository, or “repo”, contains your project files together with the Git history.
Git can be used entirely locally on your computer, but GitHub makes it easier to:
- Back up repositories online
- Share projects
- Collaborate with other developers
- Review code
- Track issues
- Manage development tasks
- Host open-source projects
It’s important to understand that Git and GitHub aren’t the same thing. Git is the version control system. GitHub is an online service that works with Git.
1. Install Git
First, you’ll need Git installed on your computer. You can download it from git-scm.com.
After installing it, open a terminal or command prompt and run:
git --version
If Git is installed correctly, you’ll see the installed version. For example:
git version 2.x.x
You can now use Git from your terminal.
2. Configure Git
Before creating your first repository, configure your name and email address. Run:
git config --global user.name "Your Name"
Then:
git config --global user.email "[email protected]"
These details are associated with the commits you create.
You can check your configuration with:
git config --list
3. Create a Project
Let’s create a simple project. Create a folder called my-project, then open a terminal inside the folder. For example:
cd my-project
Create a simple file called index.html and put some basic HTML inside it:
<!DOCTYPE html>
<html>
<head>
<title>My Project</title>
</head>
<body>
<h1>Hello Git!</h1>
</body>
</html>
We now have a project that we can put under version control.
4. Initialise a Git Repository
Run:
git init
Git will create a hidden .git directory inside your project. This directory contains the information Git needs to track the project.
You can check the repository’s status with:
git status
You should see your new index.html file listed as an untracked file.
5. Stage Your Changes
Git doesn’t automatically include every file in a commit. First, you need to stage the files you want to include. Run:
git add index.html
You can check the status again:
git status
The file should now appear as a staged change.
If you want to stage all changed files, you can use:
git add .
Be careful with this command in larger projects, as it stages everything that isn’t excluded by your Git configuration.
6. Create Your First Commit
A commit is a snapshot of your project at a particular point in time. Create your first commit:
git commit -m "Initial commit"
The message describes what the commit contains. Good commit messages should be short but meaningful. For example:
Add login form
Fix navigation bug
Update homepage layout
Add player inventory system
Improve database error handling
Avoid messages such as:
stuff
changes
updated
final
A useful commit history makes it much easier to understand how a project has developed.
7. View Your Commit History
You can see previous commits with:
git log
You’ll see information including:
- Commit ID
- Author
- Date
- Commit message
For a shorter version, use:
git log --oneline
For example:
8f32a12 Add navigation
4c91b23 Add homepage
2ab7310 Initial commit
This provides a useful overview of the project’s history.
8. Create a GitHub Repository
Now we’ll connect the local project to GitHub. Sign in to GitHub and create a new repository, giving it a name such as my-project.
For an existing local project, avoid creating another README or other files initially if you want to keep the first push straightforward.
GitHub will provide instructions for connecting your local repository.
9. Connect Your Local Repository to GitHub
GitHub repositories have a remote address. You can connect your local repository using:
git remote add origin https://github.com/YOUR-USERNAME/my-project.git
You can check the remote with:
git remote -v
You should see your GitHub repository listed. The word origin is simply the conventional name given to the primary remote repository.
10. Push Your Code to GitHub
Your commits currently exist on your local computer. To upload them to GitHub, use:
git push -u origin main
The -u option establishes the relationship between your local main branch and the remote branch.
If your local branch has a different name, such as master, you can rename it first with git branch -M main.
After the first push, you can usually use:
git push
Your project should now be visible in your GitHub repository.
11. The Basic Git Workflow
A common Git workflow looks like this:
Make changes
↓
git status
↓
git add
↓
git commit
↓
git push
↓
GitHub
For example:
git status
git add .
git commit -m "Add contact form"
git push
This simple cycle will become second nature as you use Git regularly.
12. Create a Branch
One of Git’s most useful features is branching. Branches allow you to work on a feature without directly changing the main version of the project.
Create a new branch:
git branch feature-contact-form
Switch to it:
git switch feature-contact-form
Or combine both operations:
git switch -c feature-contact-form
You can see your branches with:
git branch
The current branch will be marked with an asterisk.
13. Work on Your Feature
Make your changes as normal. For example, you might add a new file called contact.html. Then stage your changes:
git add .
Create a commit:
git commit -m "Add contact form"
You can continue making commits while working on the feature.
14. Push Your Branch
When you’re ready to share your branch with GitHub:
git push -u origin feature-contact-form
The branch will now be available on GitHub.
This is particularly useful when working with a team. Developers can work on different features without constantly changing the main branch.
15. Pull Requests
Once your feature is ready, you can create a Pull Request, commonly abbreviated to PR.
A pull request asks the team to review your changes before they are merged into the main branch. A typical workflow looks like:
main
│
├── feature-login
│
├── feature-dashboard
│
└── feature-search
Each developer can work independently. Once a feature is complete, the developer opens a pull request.
Other developers can then:
- Review the changes
- Comment on specific lines
- Request modifications
- Approve the changes
- Merge the branch
This creates a controlled process for introducing changes into the main project.
16. Pull Changes from GitHub
When working with other developers, changes may have been pushed to GitHub since you last updated your local repository.
You can download and integrate those changes with:
git pull
It’s good practice to run this command before starting work. This reduces the likelihood of working against an outdated version of the project.
17. Understanding Merge Conflicts
Sometimes two developers modify the same part of a file. For example, Developer A changes a line to:
<h1>Welcome to our website</h1>
while Developer B changes the same line to:
<h1>Welcome to our new website</h1>
Git may not know which version should be kept. This creates a merge conflict.
Git will mark the conflicting section in the file, like this:
<<<<<<< HEAD
<h1>Welcome to our website</h1>
=======
<h1>Welcome to our new website</h1>
>>>>>>> feature-branch
You’ll need to decide which version should remain, or combine the two changes, and remove the conflict markers.
After resolving the conflict:
git add .
Then create a commit:
git commit -m "Resolve merge conflict"
Merge conflicts can look intimidating at first, but they’re a normal part of collaborative development.
18. Use a .gitignore File
Not everything in your project should be committed to Git. For example, you may have:
- Build files
- Temporary files
- IDE settings
- Environment files
- Dependencies
- Secret credentials
A .gitignore file tells Git which files and folders it should ignore. For a Node.js project, you might have:
node_modules/
dist/
.env
This prevents unnecessary or sensitive files from being added to your repository.
Never commit passwords, API keys, private certificates or other credentials to a public repository.
19. Reverting Changes
One of Git’s major advantages is that your previous work is preserved.
If you make a change and commit it, you can create a new commit that reverses it using:
git revert COMMIT_ID
For example:
git revert 8f32a12
This is generally safer for shared branches than rewriting history.
Git also provides other commands for undoing changes, such as git restore and git reset, but they behave differently and should be used carefully.
20. A Practical Team Workflow
A simple team workflow might look like this.
Step 1: Update your local main branch
git switch main
git pull
Step 2: Create a feature branch
git switch -c feature-user-profile
Step 3: Develop the feature
Make your changes and test them.
Step 4: Commit your work
git add .
git commit -m "Add user profile"
Step 5: Push the branch
git push -u origin feature-user-profile
Step 6: Open a Pull Request
Open a pull request on GitHub so your team can review the changes.
Step 7: Merge the Pull Request
Once approved, merge the feature into main.
Step 8: Update your local repository
git switch main
git pull
This workflow keeps the main branch stable, gives every change a review, and lets several developers work on the same project without getting in each other’s way.
Conclusion
Git and GitHub are essential tools for modern development. Git gives you a complete history of your project and the freedom to experiment safely, while GitHub makes it easy to back up your work, review code and collaborate with others.
Start with the basic cycle of git add, git commit and git push, then add branches and pull requests as you begin working with a team. With a little regular practice, these commands will soon become second nature.