Git is the backbone of modern software development - a powerful version control system that tracks every change in your code and enables seamless collaboration across teams. Whether you’re managing a solo project or contributing to large-scale open-source repositories, understanding Git’s core commands is essential.
This guide walks you through the most important Git commands - from setup to branching, collaboration, and managing remote repositories - following the popular GitHub workflow.
Initial Setup and Identity — git config
Before making your first commit, configure your user identity. These details are attached to every commit you make.
Set Global Username:
git config --global user.name "Your Name"
Set Global Email:
git config --global user.email [email protected]
Review Settings:
git config --list
Create Shortcuts (Aliases):
git config --global alias.i init
Now 'git i' is shorthand for 'git init'.
![Git 1]()
The Core Workflow — Initialize, Stage, and Commit
Every Git project follows three key steps: Initialize a repository, Stage the changes, and Commit them to history.
Initialize a Repository
git init
![Git 2]()
Initializes a local Git repository, making the folder Git-trackable.
This command creates a hidden .git directory, which contains all the necessary metadata, objects, and configurations for the repository to function. After init, Git commands can be used in this folder.
Check Repository Status
git status
Shows the current state of the working directory and staging area.
Essential before and after making changes, staging, or committing.
Files that are not yet tracked by Git will appear in red, indicating they are untracked.
![Git 3]()
It clearly describes files into three categories: untracked (new files not yet added), staged (files ready for the next commit), and modified (files changed since the last commit but not yet staged).
![Git 4]()
![Git 5]()
Create, Stage, and Commit Files:
Create Files
touch MyRequest.http
![Git 6]()
Stage Files
git add .
It stages all files at once
Stages all modified and untracked files in the current directory and its subdirectories.
git add MyRequest.http
It stages a specific file.
Moves a specific file's current state from the working directory to the staging area.
![Git 7]()
Changes are only tracked by Git when explicitly added to the staging area. Any subsequent modifications to the file after running git add require running the command again to stage the latest changes.
Commit Changes
git commit -m "Initial commit"
![git 8.1]()
Stores all staged changes in Git’s history with a commit message.
The commit acts as a unique historical record. The -m flag is essential for providing a concise, descriptive commit message detailing the changes made.
Working with Remote Repositories
To collaborate on code, share work, and maintain backups, developers interact with a remote repository, often hosted on services like GitHub.
Clone a Repository
git clone <repository-URL>
![Git 9]()
Creates a local copy of a remote repository.
This operation copies the entire repository history, all branches, and sets up a connection to the original remote repository.
The repository URL can be retrieved from GitHub.he URL can get from GitHub,
![girhub url]()
Example.git clone https://github.com/Sreenath-Kappoor/ToDo_List_Application_Minimal_API.git
![Git 10]()
Add the remote repository as origin:
git remote add origin <remote-repository-url>
Push all project files to the remote repository,
git push -u origin main –force
![Git 10.1]()
![Git 10.2]()
Branch Management
Branches allow independent development for new features or fixes. They are critical for the GitHub Flow as they allow developers to work on features or fixes without affecting the stable main branch.
It keeps your work isolated, allows feature experimentation without disrupting the main branch.
Create and Switch to a Branch
git checkout -b <branch_name>
![Git 11]()
Creates a new branch and immediately switches the working directory to it.
The -b
flag is shorthand for "create and checkout." Work done here is isolated from other branches until merged.
List Branches
git branch
![Git 12]()
Lists all local branches in the repository.
The current active branch is typically highlighted (often with an asterisk).
Switch Branches
git switch branchname
![Git 13]()
Switches the working directory to an existing branch.
Synchronizing Local and Remote Repositories
Push Changes
Check the git status to see if there is anything to add to the staging area.
Use "git add ." to add all changes to the staging area.
Then use "git commit -m 'comments'" to commit all changes.
Finally, check the git status again to confirm that there is nothing pending to add to the staging area.
![Git 14]()
git push origin branchname
![Git 15]()
Origin refers to the remote repository.
branchname is the working branch.
This uploads local commits from a branch to the corresponding branch on the remote repository.
Click on “Compare & pull request” in the GitHub repository to see the changes.
![Git 16]()
After clicking "Compare & pull request," review the changes shown in the interface. Make sure these are the updates you want to merge and click the blue Create pull request button
![Git 17]()
Now, you will see the blue Merge pull request button—click that to merge your changes into the main branch.
![Git 18]()
![Git 19]()
Then check the changes in file, I added the comment '// To-Do List' on the first line of Program.cs.
![Git 20]()
Pull Updates
git pull
![Git 21]()
Fetches and merges all changes from the remote repository into the current local branch.
This is an essential operation for ensuring the local repository remains synchronized with the remote, preventing merge conflicts.
Cleaning Up Branches
Delete Local Branch
git branch -d <branch_name>
![Git 22]()
The -d
(safe delete) flag only works if the branch has been successfully merged. Use -D
(force delete) to delete unmerged branches.
Delete Remote Branch
![Git 22.1]()
git push --delete <remote> <branch_name>
![Git 22.2]()
This removes the branch from the server after it is no longer needed.
![Git 22.3]()
Reviewing Project History
View Commit Details
git show
![Git 23]()
Displays detailed information about a specific commit (or the latest commit if none is specified).
The output includes the full Commit ID (SHA), the author, the date, the commit message, and the specific line-by-line file changes (the diff) associated with that commit. This is crucial for auditing and code review.
SHA - Stands for Secure Hash Algorithm, A 40-character hexadecimal string.
The GitHub Workflow
The GitHub Flow is a streamlined, branch-based workflow that is widely adopted for continuous integration and delivery. The key steps are:
1. Clone the repository to your local machine.
2. Create a new branch of main
for any new feature or fix.
3. Commit changes to the new branch frequently and descriptively.
4. Push the feature branch to the remote repository.
5. Open a Pull Request (PR) on GitHub, which proposes to merge your changes into main
.
6. Address review comments, perform necessary updates, and pull the final approved PR into the main
branch.
7. Pull the updated main
branch into your local repository to synchronize.
8. Delete the feature branch, both locally and remotely.
Creating a Repository on GitHub — Step by Step
1. Log in to GitHub.
2. Click the 'New' repository button.
3. Fill in repository name and description.
4. Choose Public or Private.
5. Optionally add README, .gitignore, and License.
6. Click 'Create Repository'.
Final Thoughts
Git is more than just a tool, it’s a developer’s safety net. It allows experimentation, collaboration, and easy rollback. Master these essential commands to take full control of your codebase.