GIT

Git Commands Cheat Sheet: 15 Essential Git Commands Every Developer Must Know (Beginner to Advanced Guide)

Git Commands Cheat Sheet: 15 Essential Git Commands Every Developer Must Know

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.

πŸ”Ή 1. git clone (Copy a Repository)

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.

πŸ”Ή 2. git status (Check Repository Status)

This command shows the current state of your working directory.

git status

πŸ‘‰ It helps you identify:

  • Modified files
  • Staged files
  • Untracked files

πŸ”Ή 3. git add (Stage Changes)

Before committing, you need to stage your changes.

git add file.txt

To add all files:

git add .

πŸ‘‰ Moves changes to the staging area.


πŸ”Ή 4. git commit (Save Changes)

Commits save your staged changes with a message.

git commit -m "Added login feature"

πŸ‘‰ Think of it as creating a checkpoint in your project.


πŸ”Ή 5. git push (Upload Code to Remote)

Push your local commits to a remote repository.

git push origin main

πŸ‘‰

  • origin = remote repository
  • main = branch name

πŸ”Ή 6. git pull (Get Latest Code)

Fetches and merges the latest changes from the remote repository.

git pull origin main

πŸ‘‰ Best practice: Always pull before starting work to avoid conflicts.


πŸ”Ή 7. git fetch (Download Changes Only)

Downloads changes without merging.

git fetch

πŸ‘‰ Useful for safely reviewing updates before applying them.


πŸ”Ή 8. git branch (Manage Branches)

List all branches:

git branch

Create a new branch:

git branch feature-login

πŸ‘‰ Helps in working on features independently.


πŸ”Ή 9. git checkout (Switch Branches)

Switch to another branch:

git checkout feature-login

Create and switch:

git checkout -b feature-login

πŸ‘‰ Used for navigation and restoring files.


πŸ”Ή 10. git merge (Combine Branches)

Merge one branch into another.

git checkout main
git merge feature-login

πŸ‘‰ Integrates feature code into the main branch.


πŸ”Ή 11. git log (View Commit History)

Shows commit history.

git log

Short version:

git log --oneline

πŸ‘‰ Helps track changes and history.


πŸ”Ή 12. git diff (See Code Changes)

Displays differences between files.

git diff

πŸ‘‰ Useful for reviewing changes before committing.


πŸ”Ή 13. git reset (Undo Staging)

Remove a file from staging area:

git reset file.txt

πŸ‘‰ Does not delete changes, only unstages them.


πŸ”Ή 14. git stash (Save Work Temporarily)

Save unfinished work:

git stash

Restore it later:

git stash pop

πŸ‘‰ Perfect when switching branches without committing.


πŸ”Ή 15. git remote (Manage Remote Repositories)

View remote repositories:

git remote -v

Add a new remote:

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

⚠️ Important Git Workflow Tip (Real-World Practice)

πŸ‘‰ Before pushing code:

  • Always pull from pencil_dev_env
  • Only push to basant_dev
  • Add proper tags when required

This ensures:

  • No merge conflicts
  • Clean collaboration
  • Proper version tracking

πŸ“Œ Best Practices for Using Git

  • βœ… Commit frequently with meaningful messages
  • βœ… Pull before starting work
  • βœ… Use branches for new features
  • βœ… Avoid pushing directly to main
  • βœ… Use git fetch for safe updates
  • βœ… Keep your repository clean

🎯 Conclusion

Mastering 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 πŸš€

Basant

Recent Posts

How to make dependent dropdown of state and district using cakephp2 form?

Creating dependent dropdowns in CakePHP 2 involves using AJAX to dynamically load data into the…

1 year ago

How to make id as auto increment in postgrey SQL?

just add below code in column properties id Default input Method: 1nextval('form_submissions_id_seq'::regclass) Method: 2 Via…

1 year ago

How to Add Auto Increment to a Column in PostgreSQL ?

Learn how to efficiently set up auto-increment columns in PostgreSQL with this step-by-step guide. From…

1 year ago