Guide to Git Commands

Git is a powerful πŸ› οΈ version control system that helps developers πŸ‘©β€πŸ’» manage and track πŸ” changes to their codebase. Whether you are a πŸ†• beginner or an experienced πŸ§‘β€πŸ’» developer, understanding essential Git commands is crucial for collaboration 🀝 and efficient development.

In this blog, we’ll explore key Git commands with interactive explanations 🧩 and practical examples. Let’s dive πŸŠβ€β™‚οΈ in!

1. Setting Up Git

Before you start 🏁 using Git, you need to configure your user πŸ‘€ information:

# Set your name
git config --global user.name "Your Name"

# Set your email
git config --global user.email "youremail@example.com"

Why it matters:

These settings identify πŸ‘€ you as the author of changes πŸ”„ in your commits.

step1

2. Initializing a Git Repository

To start tracking πŸ›€οΈ a project with Git, initialize it as a repository πŸ“:

# Initialize a Git repository in your project folder
git init

Example:

cd my-project
git init

This creates a hidden πŸ•΅οΈ .git folder in your project, signaling that it’s now a Git repository.

3. Checking the Status of Your Repository

To view πŸ‘€ the current state of your repository:

git status

πŸ€” Interactive Tip:

Run this command frequently to see which files πŸ“‚ are staged, modified ✏️, or untracked.

4. Adding Files to the Staging Area

Before committing, you need to stage changes πŸ”„:

# Add specific files
git add filename

# Add all files
git add .

Example:

git add index.html

This stages the index.html file for the next πŸ›€οΈ commit.

5. Committing Changes

Commit your staged changes πŸ”’ with a message πŸ’¬:

git commit -m "Your commit message"

βœ… Best Practice:

Write ✍️ clear and concise commit messages to describe what the changes πŸ”„ do.

6. Viewing Commit History

To see πŸ‘€ a log πŸ“œ of all commits:

git log

Example:

git log --oneline

This shows a compact summary πŸ“ of your commit history.

7. Cloning a Repository

To copy πŸ“‹ an existing repository to your local machine πŸ–₯️:

git clone repository_url

Example:

git clone https://github.com/user/repository.git

This creates a local copy πŸ“‚ of the remote 🌐 repository.

8. Pushing Changes to a Remote Repository

To upload πŸ“€ your commits to a remote 🌐 repository:

git push origin branch_name

Example:

git push origin main

This pushes changes to the main branch of the remote repository.

9. Pulling Changes from a Remote Repository

To update πŸ”„ your local repository with changes from the remote:

git pull origin branch_name

Example:

git pull origin main

This merges remote changes into your local branch 🌿.

10. Creating and Switching Branches

Branches 🌿 allow you to work on new features independently:

# Create a new branch
git branch branch_name

# Switch to a branch
git checkout branch_name

# Create and switch in one step
git checkout -b branch_name

Example:

git checkout -b feature-login

This creates and switches to the feature-login branch 🌿.

11. Merging Branches

To merge changes from one branch 🌿 into another:

git merge branch_name

Example:

git merge feature-login

This merges the feature-login branch 🌿 into the current branch.

12. Resolving Merge Conflicts

Conflicts βš”οΈ occur when changes overlap. Git marks the conflict areas in your files πŸ“„:

# After resolving conflicts, add the resolved files
git add filename

# Commit the resolution
git commit -m "Resolved merge conflict"

πŸ’‘ Tip:

Carefully review πŸ‘€ conflicting changes to ensure nothing important is lost.

13. Stashing Changes

To temporarily save πŸ’Ύ changes without committing:

git stash

To apply stashed changes later πŸ•’:

git stash apply

14. Viewing Differences Between Commits

To see πŸ‘€ what has changed between commits:

git diff

Example:

git diff HEAD~1 HEAD

This compares πŸ†š the latest commit with the one before it.

πŸŽ‰ Conclusion

Mastering πŸ’ͺ Git commands is essential for developers πŸ‘©β€πŸ’» to manage their code efficiently. Start 🏁 with the basics and gradually explore advanced πŸ” commands as you grow 🌱. Remember, practice makes perfect!
Have questions ❓ or suggestions πŸ’‘? Drop a comment below! πŸ’¬

Devops Multi cloud Training

Choose the training style that fits your schedule β€” Self-Paced or Live Interactive Sessions. Both include hands-on projects, expert support, and lifetime access.

FeatureSelf-Paced TrainingLive Training
🎯 ModeπŸŽ₯Pre-Recorded SessionπŸ§‘β€πŸ«Live Class + Recordings
πŸ’Ό ProjectsπŸ•’ Weekend Real-Time ProjectsπŸ“… Weekdays + Weekend Real-Time Projects
❓ Doubt ClearingπŸ“ž Weekend Live Support Session🧠 Anytime Doubt Clearing Session
πŸ‘₯ Career Support & Mentorship❌ Noβœ… Yes
πŸŽ“ Global Certification Training❌ Noβœ… Yes
πŸ”‘ Access♾️ Lifetime Access♾️ Lifetime Access
πŸ’° Feesβ‚Ή4,999 (2 x β‚Ή2,500)β‚Ή7,999 (2 x β‚Ή4,000)
ℹ️ For More InfoExplore Self-Paced Training Explore Live Training
Open chat
Hello, Good day!!
How can we help you?