Saturday, 3 June 2023

Useful Git Commands

Comprehensive Guide to Essential Git Commands

Introduction

Welcome to this comprehensive guide on essential Git commands. This blog covers everything from basic configuration to advanced branching strategies. Whether you're new to Git or looking to sharpen your version control skills, you'll find practical examples and best practices throughout.

---

1. Initial Setup & Configuration

**Purpose:** Configure your Git identity for commits and version tracking.

**Commands:**

```
git config --global user.email "phani@soft.com"
git config --global user.name "phani"
```

**Description:** These commands configure your user information globally across all repositories on your system. This information will appear in all your commits.

---

2. Creating & Initializing a Project

**Purpose:** Initialize a new Git repository for version control.

**Command:**

```
```

**Description:** Creates a `.git` folder in your project directory, initializing it as a Git repository. This is the first step when starting a new project.

---

## 3. Checking Project Status

**Purpose:** View the current state of your working directory and staging area.

**Command:**

```

git status

```

**Description:** Displays modified files, staged files, and untracked files in your repository. Use this frequently to understand what changes exist.

---

## 4. Staging Files

**Purpose:** Prepare files for commit to the repository.

**Commands:**

```
git add FILE_NAME       # Stage a specific file
git add .              # Stage all changes
```

**Description:** Staging marks files as ready to be committed. Think of it as the intermediate step between making changes and recording them.

---

**Purpose:** Save staged changes to your repository with a descriptive message.

3. Checking Project Status

4. Staging Filesgit commit -m "Your commit message"

```

**Example:**

```
git commit -m "Add user authentication feature"
```

**Description:** Commits record snapshots of your project. Use clear, descriptive messages to document what changed and why.

---

## 6. Viewing Commit History

**Purpose:** Review all commits and changes in your repository.

**Command:**

```
```

**Description:** Displays a chronological list of all commits with author, date, timestamp, and message. Essential for tracking project evolution.

---

## 7. Working with Branches

**Purpose:** Create isolated workspaces for different features without affecting the main codebase.

**Commands:**

```
git branch                  # List local branches
git branch --all           # List all branches (local + remote)
```

**Description:** Branches allow you to work on different features simultaneously. Each branch is an independent line of development.

---

## 8. Switching Branches

**Purpose:** Move your working directory to a different branch.

**Modern Approach (Git 2.23+):**

```
```

**Legacy Approach:**

```
```

**Description:** Switching branches updates your working files to match the selected branch. Use the modern `git switch` for clarity.

---

## 9. Merging Branches

**Purpose:** Integrate changes from one branch into another.

**Command:**

```
```

**Description:** Merging creates a merge commit that combines changes from both branches. This preserves complete history but can create complex commit graphs.

---

## 10. Handling Detached HEAD State

**Purpose:** Recover from accidental checkout of specific commits.

**Problem Scenario:**

```
git checkout HASH-CODE-COMMIT    # Enters detached HEAD state
```

**Solution - Create a Branch:**

```
```

**Description:** When checking out a specific commit instead of a branch, you enter a detached HEAD state. Creating a branch preserves your changes and prevents data loss.

---

## 11. Git Rebase

**Purpose:** Rewrite commit history by applying commits on a new base.

**Command:**

```
```

**Key Differences from Merge:**

**git merge:**
- Creates a new merge commit
- Preserves complete history of both branches
- Results in a non-linear commit graph

**git rebase:**
- Rewrites commit history
- Moves commits onto a new base
- Creates a linear, cleaner history

**Example Workflow:**

```
git checkout feature
git rebase master
```

**Description:** Git replays commits from the feature branch on top of the latest commit in master, creating a linear history.

---


**Purpose:** Create named labels for important commits (releases, milestones).

**Commands:**

```
git tag TAG-NAME                # Create a tag
git checkout TAG-NAME           # Checkout a specific tag
```

**Description:** Tags mark specific points in repository history. Commonly used for release versions (v1.0.0, v2.1.3, etc.).

---


**Purpose:** Synchronize local repository with remote repositories.

**Commands:**

```
git pull        # Fetch and merge remote changes
git push        # Upload local commits to remote
git fetch       # Download remote changes without merging
git merge       # Integrate fetched changes
```

**Description:** 
- **pull** = fetch + merge (combines downloading and integrating)
- **fetch** = download only (safe, doesn't modify working directory)
- **push** = upload your commits to the remote repository

---

## 14. Best Practices Summary

### Branching Strategy
- Use feature branches for new development
- Keep main/master branch stable
- Delete merged branches regularly

### Commit Practices
- Write clear, descriptive commit messages
- Make commits logically grouped
- Avoid "I fixed stuff" commits

### Merging vs. Rebasing
- Use **merge** for shared/main branches (preserves history)
- Use **rebase** for feature branches (cleaner history)
- Never rebase public/shared branches

### Tagging & Releases
- Tag every release with semantic versioning
- Use annotated tags for releases
- Document what changed in each tag

---

## Conclusion

Mastering these Git commands will transform you from a basic user into a proficient developer capable of managing complex projects efficiently. Start with the basics, practice regularly, and gradually incorporate advanced techniques like rebasing and strategic branching. Your future self will thank you for maintaining clear, organized version control!

**Key Takeaway:** Git is not just for backup—it's a powerful collaboration and project management tool. Use it wisely.

No comments:

Post a Comment