Introduction
Git is a distributed version control system used to track changes in source code and collaborate with other developers. Although Git provides many commands, beginners do not need to learn all of them at once.
A better way to learn Git is to understand the commands through a real development workflow.
In this article, we will create a small project, initialize a Git repository, track and commit changes, create a feature branch, merge the changes, connect the repository to a remote repository, and push the project.
Along the way, we will use commonly required Git commands such as git init, git status, git add, git commit, git branch, git switch, git merge, git remote, git fetch, git pull, and git push.
What We Will Build
Suppose we are working on a simple application called ShoppingApp.
Our workflow will look like this:
Create Project
↓
git init
↓
Create / Modify Files
↓
git status
↓
git add
↓
git commit
↓
Create Feature Branch
↓
Make Changes
↓
git commit
↓
Merge Feature
↓
git push
This workflow represents a simplified version of what developers commonly do when working with Git.
Step 1: Create a Project
Create a directory for the project:
mkdir ShoppingApp
cd ShoppingApp
Create a simple file:
README.md
Add some content to the file:
# ShoppingApp
A simple application for managing products.
At this point, the directory is just a normal folder. Git is not tracking it yet.
Step 2: Initialize the Git Repository
Run:
git init
Output may look similar to:
Initialized empty Git repository in .../ShoppingApp/.git/
The git init command creates a new Git repository in the current directory.
Git creates a hidden .git directory containing repository metadata such as:
Commit information
Branch information
Configuration
Object database
References
You normally should not manually modify the contents of the .git directory.
The project now has Git version control enabled.
Step 3: Check the Repository Status
Run:
git status
You may see:
Untracked files:
README.md
This means Git knows that the file exists, but the file has not yet been added to the staging area.
The basic Git workflow has three important states:
Working Directory
↓
Staging Area
↓
Repository
Step 4: Stage the File with git add
To stage the README file:
git add README.md
You can check the status again:
git status
The output should now indicate that README.md is staged for commit.
To stage all changes in the current directory, you can use:
git add .
The staging area allows you to choose exactly which changes should become part of the next commit.
Step 5: Create Your First Commit
Create a commit:
git commit -m "Add project README"
The commit records the staged changes in Git's repository history.
A commit is essentially a snapshot of the project at a particular point in time.
You can think of the workflow as:
Edit Files
↓
git add
↓
Staging Area
↓
git commit
↓
Repository History
Step 6: Configure Your Git Identity
Git records information about who created each commit.
You can configure your name and email globally:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
You can verify the configuration:
git config --global --list
If you want a configuration to apply only to the current repository, omit --global:
git config user.name "Your Name"
git config user.email "[email protected]"
Repository-specific configuration overrides the corresponding global configuration.
Step 7: View Commit History
After creating a commit, use:
git log
You may see output similar to:
commit 7a91c2d...
Author: Your Name <[email protected]>
Date: ...
Add project README
For a shorter view:
git log --oneline
Example:
7a91c2d Add project README
The commit hash uniquely identifies the commit.
Step 8: Create a Feature Branch
Suppose the team now wants to add product functionality.
Instead of making the changes directly on the main branch, create a feature branch.
Modern Git provides:
git switch -c add-products
This creates the add-products branch and switches to it.
You can verify the current branch with:
git branch
Example:
* add-products
main
The * indicates the current branch.
What Is a Branch?
A branch provides an independent line of development.
Conceptually:
add-products
/
main ----------●
Developers can work on a feature without immediately changing the main branch.
Step 9: Add a Product Feature
Create a file called:
Product.cs
For example:
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
Check the changes:
git status
Git will report Product.cs as an untracked file.
Stage it:
git add Product.cs
Create a commit:
git commit -m "Add Product model"
The feature branch now contains the new commit.
Step 10: Compare Changes with git diff
Before committing changes, you may want to inspect what changed.
Modify Product.cs:
public int StockQuantity { get; set; }
Then run:
git diff
Git displays the differences between the working directory and the staged version.
For example:
public decimal Price { get; set; }
+public int StockQuantity { get; set; }
This is useful for reviewing changes before staging them.
After staging the file:
git add Product.cs
you can inspect the staged changes with:
git diff --staged
This distinction is important:
git diff
↓
Working Directory vs Staging Area
git diff --staged
↓
Staging Area vs Last Commit
Step 11: Rename or Move Files with git mv
Suppose we rename:
Product.cs
to:
ProductModel.cs
Instead of manually moving the file and then staging the changes separately, you can use:
git mv Product.cs ProductModel.cs
Git stages the rename.
Commit it:
git commit -m "Rename product model"
Step 12: Remove a File with git rm
If a file is no longer required:
git rm ProductModel.cs
This removes the file and stages the deletion.
Commit the change:
git commit -m "Remove obsolete product model"
Step 13: Merge the Feature Branch
After completing and testing the feature, switch back to the main branch:
git switch main
Then merge the feature:
git merge add-products
Git attempts to integrate the commits from add-products into main.
The history can now look like:
main
|
● Add project README
|
● Add Product model
|
● Rename product model
For more complex projects, Git may encounter a merge conflict.
Step 14: Understand Merge Conflicts
A conflict can occur when two branches modify the same part of a file differently.
For example:
<<<<<<< HEAD
decimal Price;
=======
decimal ProductPrice;
>>>>>>> add-products
Git cannot determine which version should be retained automatically.
Join the conversation! Your thoughts help the community grow.