Git is one of the most powerful version control systems used by developers worldwide. Whether youβre working solo or collaborating in a team, mastering Git commands is essential for efficient code management.
In this guide, youβll learn 15 essential Git commands with examples, helping you streamline your development workflow and avoid common mistakes.
The git clone command is used to download a remote repository to your local machine.
git clone https://github.com/user/project.git
π This creates a complete local copy of the project, including all files and commit history.
This command shows the current state of your working directory.
git status
π It helps you identify:
Before committing, you need to stage your changes.
git add file.txt
To add all files:
git add .
π Moves changes to the staging area.
Commits save your staged changes with a message.
git commit -m "Added login feature"
π Think of it as creating a checkpoint in your project.
Push your local commits to a remote repository.
git push origin main
π
origin = remote repositorymain = branch nameFetches and merges the latest changes from the remote repository.
git pull origin main
π Best practice: Always pull before starting work to avoid conflicts.
Downloads changes without merging.
git fetch
π Useful for safely reviewing updates before applying them.
List all branches:
git branch
Create a new branch:
git branch feature-login
π Helps in working on features independently.
Switch to another branch:
git checkout feature-login
Create and switch:
git checkout -b feature-login
π Used for navigation and restoring files.
Merge one branch into another.
git checkout main
git merge feature-login
π Integrates feature code into the main branch.
Shows commit history.
git log
Short version:
git log --oneline
π Helps track changes and history.
Displays differences between files.
git diff
π Useful for reviewing changes before committing.
Remove a file from staging area:
git reset file.txt
π Does not delete changes, only unstages them.
Save unfinished work:
git stash
Restore it later:
git stash pop
π Perfect when switching branches without committing.
View remote repositories:
git remote -v
Add a new remote:
git remote add origin https://github.com/user/project.git
π Before pushing code:
pencil_dev_envbasant_devThis ensures:
git fetch for safe updatesMastering these Git commands will significantly improve your development workflow. Whether youβre a beginner or an experienced developer, these commands form the foundation of efficient version control.
Start practicing today and make Git your best development companion π
Creating dependent dropdowns in CakePHP 2 involves using AJAX to dynamically load data into the…
just add below code in column properties id Default input Method: 1nextval('form_submissions_id_seq'::regclass) Method: 2 Via…
Learn how to efficiently set up auto-increment columns in PostgreSQL with this step-by-step guide. From…