# Journey with Git and GitHub

As a DevOps engineer, managing code effectively and ensuring smooth integration, testing, and deployment processes are essential responsibilities. Git and GitHub are the foundation for these workflows, providing the tools necessary for seamless collaboration, automation, and deployment. In this blog, we'll explore a DevOps engineer's journey with Git and GitHub, from creating repositories to deploying code in a production environment.

### Starting with Version Control: Understanding Git Basics

Q. **What is Git?**

Git is a distributed version control system that allows developers to track changes, collaborate on code, and manage multiple versions of a project efficiently.

Q. **What is a Version Control System (VCS)?**

A **Version Control System (VCS)** is a software tool that helps developers manage changes to source code or other files over time. It enables collaboration, tracks changes, and provides a history of modifications, allowing teams to work on projects efficiently while minimizing conflicts.

### **Installing and Setting Up Git: A Guide for Windows and Linux**

**Installing Git on Windows**

#### Step 1: Download Git

* Visit the official [Git webs](https://git-scm.com/)[ite](https://git-scm.com/) and download the latest version for Windows.
    
* Choose the appropriate version for your system (32-bit or 64-bit).
    

#### **Step 2: Install Git**

1. Open the downloaded installer and follow the setup wizard.
    
2. Choose your preferred editor for Git (default is Vim, but you can select Notepad++ or VS Code).
    

#### **Step 3: Verify Installation**

* Open Command Prompt or PowerShell.
    
* Type the following command to verify the installation:
    
    ```bash
    git --version
    ```
    
* You should see the installed Git version.
    

#### **Step 4: Configure Git**

* Set your name and email:
    
    ```bash
    git config --global user.name "Your Name"
    git config --global user.email "hello@anupkafle.com.np" // Enter your Email
    ```
    

**Installing Git on Linux**

#### **Step 1: Update Your System**

* Before installing Git, ensure your system is up-to-date.
    
    ```bash
    sudo apt update && sudo apt upgrade
    ```
    

#### **Step 2: Install Git**

* The installation commands vary based on your Linux distribution:
    
    **For Ubuntu/Debian:**
    
    ```bash
    sudo apt install git
    ```
    
    **For Fedora/RHEL:**
    
    ```bash
    sudo dnf install git
    ```
    
    **For Arch Linux:**
    
    ```bash
    sudo pacman -S git
    ```
    

#### **Step 3: Verify Installation**

* Check if Git is installed and its version:
    
    ```bash
    git --version
    ```
    

#### **Step 4: Configure Git**

* Similar to Windows, set your global username and email:
    
    ```bash
    git config --global user.name "Your Name"
    git config --global user.email "hello@anupkafle.com.np" // Enter your Email
    ```
    

### Check Your Git Configuration

After configuring your name and email in Git, it’s important to verify the settings to ensure everything is set up correctly. These details are crucial as they are attached to every commit you make and help identify who made the changes.

#### **Check Your Global Configuration**

Run the following command to view your global Git configuration, including your name and email:

```bash
git config --global --list
```

You should see output similar to this:

```bash
msi@anup:~$ git config --global --list
user.name=anupkafle
user.email=hello@anupkafle.com.np
```

#### **Check Configuration for a Specific Repository**

If you want to check the configuration for a specific repository, navigate to the repository folder and use:

```bash
git config --list
```

This will show both global and repository-specific configurations. If you’ve overridden the global configuration for the repository, the repository-specific settings will appear here.

### Git Commands with Detailed Explanations and Examples

## **1\. Initializing a Repository**

```bash
git init
```

#### What It Does:

Creates a new Git repository in the current directory by initializing a `.git` folder.

#### Example:

```bash
mkdir my-project
cd my-project
git init
```

This initializes an empty Git repository in the `my-project` directory.

---

### **2\. Cloning a Repository**

#### Command:

```bash
git clone <repository-url>
```

#### What It Does:

Copies a repository (and its history) from a remote location to your local machine.

#### Example:

```bash
git clone https://github.com/anupkafle/testrepo.git
```

This clones the repository at [https://github.com/anupkafle/testrepo.git](https://github.com/anupkafle/testrepo.git) into a local folder named anupkafle.

---

### **3\. Checking the Status of Your Repository**

#### Command:

```bash
git status
```

#### What It Does:

Shows the status of your working directory and staging area, including:

* Files modified but not staged.
    
* Files staged but not committed.
    
* Untracked files.
    

#### Example:

```bash
git status
```

Output might look like:

```bash
msi@msi:~/Desktop/my-project$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file1.txt

nothing added to commit but untracked files present (use "git add" to track)
```

---

### **4\. Adding Files to the Staging Area**

#### Command:

```bash
git add <file-name>    # Add a specific file
git add .              # Add all files
```

#### What It Does:

Moves changes from the working directory to the staging area, marking them for the next commit.

#### Example:

```bash
git add file1.txt
git add .
```

This stages the *file1.txt* file or all files in the directory, respectively.

---

### **5\. Committing Changes**

#### Command:

```bash
git commit -m "Your commit message"
```

#### What It Does:

Saves the changes in the staging area to the repository with a descriptive message.

#### Example:

```bash
git commit -m "Added a new feature to the project"
```

This commits all staged changes with the message "Added a new feature to the project."

Output might look like:

```bash
[master (root-commit) 2e0d35b] Added a new feature to the project
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
```

---

### **6\. Viewing Commit History**

#### Command:

```bash
git log
```

#### What It Does:

Shows the commit history for the repository, including commit hashes, author details, dates, and messages.

#### Example:

```bash
git log
```

Output:

```bash
commit 2e0d35bf87987f7bdcd8bd9cc6ac6051e8b12ab6 (HEAD -> master)
Author: anupkafle <hello@anupkafle.com.np>
Date:   Fri Nov 29 22:15:56 2024 +0100

    Added a new feature to the project
```

---

### **7\. Creating and Switching Branches**

#### Commands:

```bash
git branch <branch-name>       # Create a new branch
git checkout <branch-name>     # Switch to an existing branch
git checkout -b <branch-name>  # Create and switch to a new branch
```

#### What They Do:

* **Branch creation:** Creates a new branch to isolate work on a specific feature or bug.
    
* **Switching branches:** Moves you to another branch in the repository.
    

#### Example:

```bash
git checkout -b feature/new-feature
```

This creates and switches to a branch named `feature/new-feature`.

Output:

```bash
Switched to a new branch 'feature/new-feature'
```

---

### **8\. Merging Branches**

#### Command:

```bash
git merge <branch-name>
```

#### What It Does:

Combines changes from another branch into the current branch.

#### Example:

```bash
git checkout master
git merge feature/new-feature
```

This merges the `feature/new-feature` branch into the `main` branch.

---

### **9\. Pushing Changes to a Remote Repository**

#### Command:

```bash
git push origin <branch-name>
```

#### What It Does:

Uploads local commits from the specified branch to a remote repository.

#### Example:

```bash
git push origin master
```

Pushes the `main` branch to the remote repository. To proceed, you must first create a repository and link it to a remote URL.

To link your local repository to a remote repository, use the following command:

```bash
git remote add origin <remote-repository-url>
```

---

### **10\. Pulling Changes from a Remote Repository**

#### Command:

```bash
git pull
```

#### What It Does:

Fetches and merges changes from the remote repository into the current branch.

#### Example:

```bash
git pull
```

Updates the local branch with the latest changes from the remote branch.

---

### **11\. Viewing Differences**

#### Command:

```bash
git diff
```

#### What It Does:

Shows changes between the working directory and the repository (or between branches).

#### Example:

```bash
git diff
```

Displays line-by-line differences for modified files.

Output:

```bash
msi@msi:~/Desktop/my-project$ git diff
diff --git a/file1.txt b/file1.txt
index e69de29..537e61e 100644
--- a/file1.txt
+++ b/file1.txt
@@ -0,0 +1 @@
+hi this is test file
```

The `git diff` output shows the differences between the current working directory and the latest commit. In this case, the file `file1.txt` was modified, transitioning from an empty state (indicated by `e69de29`) to containing a single line, `hi this is test file`. The `+` symbol marks the addition of this line in the file.

### **12\. Discarding Changes**

#### Commands:

```bash
git checkout -- <file-name>  # Reverts changes to a file
git reset <file-name>        # Removes a file from staging
```

#### What They Do:

* **Reverting:** Discards changes in the working directory.
    
* **Resetting:** Removes changes from the staging area without deleting them.
    

#### Example:

```bash
msi@msi:~/Desktop/my-project$ cat > file1.txt 
Hello
msi@msi:~/Desktop/my-project$ cat file1.txt 
Hello
msi@msi:~/Desktop/my-project$ git checkout -- file1.txt
msi@msi:~/Desktop/my-project$ cat file1.txt 
msi@msi:~/Desktop/my-project$ 
```

---

### **13\. Deleting a Branch**

#### Command:

```bash
git branch -d <branch-name>
```

#### What It Does:

Deletes a branch that is no longer needed.

#### Example:

```bash
msi@msi:~/Desktop/my-project$ git branch
  feature/new-feature
* master
msi@msi:~/Desktop/my-project$ git branch -d feature/new-feature
Deleted branch feature/new-feature (was 2e0d35b).
msi@msi:~/Desktop/my-project$ git branch
* master
msi@msi:~/Desktop/my-project$ 
```

Deletes the `feature/new-feature` branch.

---

### **14\. Checking Remote Repositories**

#### Command:

```bash
git remote -v
```

#### What It Does:

Lists remote repositories linked to your local repository.

#### Example:

```bash
git remote -v
```

Output:

```bash
msi@msi:~/Desktop/my-project$ git remote -v
origin	https://github.com/anupkafle/testrepo.git (fetch)
origin	https://github.com/anupkafle/testrepo.git (push)
```

---

### **15\. Tagging Releases**

#### Command:

```bash
git tag -a <tag-name> -m "Tag message"
```

#### What It Does:

Marks a specific commit with a version number or release label.

#### Example:

```bash
msi@msi:~/Desktop/my-project$ git tag -a v1.0.0 -m "Version 1.0.0"
msi@msi:~/Desktop/my-project$ git log
commit 2e0d35bf87987f7bdcd8bd9cc6ac6051e8b12ab6 (HEAD -> master, tag: v1.0.0)
Author: anupkafle <anupkafle24@gmail.com>
Date:   Fri Nov 29 22:15:56 2024 +0100

    Added a new feature to the project
```

Creates and pushes a `v1.0.0` tag to the remote repository.

---

### **16\. Stashing Changes**

Git stash is a feature that allows you to temporarily save changes in your working directory that you don’t want to commit yet. This is useful when you need to switch branches or perform other tasks without losing your uncommitted changes. The changes are saved in a stack-like structure, and you can reapply them later.

#### Command:

```bash
git stash
```

#### What It Does:

Temporarily saves changes without committing.

#### Example:

```bash
git stash
```

To apply stashed changes later:

```bash
git stash apply
```

---

### **17\. Undoing Changes**

#### Command:

```bash
git reset --hard <commit-hash>
```

#### What It Does:

Resets the repository to a specific commit, discarding all changes after it.

#### Example:

```bash
git reset --hard 1a2b3c4d5e6f7g8h9i0j
```

Rolls back to the specified commit.

---

### **18\. Help and Documentation**

#### Command:

```bash
git help <command>
```

#### What It Does:

Provides detailed documentation for any Git command.

#### Example:

```bash
git help commit
```

Displays help information for the `commit` command.

### **19.Git Rebase**

#### Command:

```bash
git rebase <branch-name>
```

#### What It Does:

* Integrates changes from one branch into another by **moving** the base of your branch to the latest commit on the target branch.
    
* Keeps a linear commit history by replaying your changes on top of the target branch.
    

#### Example:

```bash
git rebase main
```

Rebases your current branch on top of the `main` branch.

#### Use Case:

When you want to synchronize your feature branch with the latest updates from the `main` branch while avoiding merge commits.

---

### **20\. Git Cherry-Pick**

#### Command:

```bash
git cherry-pick <commit-hash>
```

#### What It Does:

* Applies a specific commit from one branch onto the current branch.
    

#### Example:

```bash
git cherry-pick abc1234
```

Applies the commit with hash `abc1234` onto your current branch.

#### Use Case:

When you need a specific change from another branch without merging the entire branch.

### **Conclusion**

Git is an indispensable tool for modern software development, enabling teams to collaborate efficiently, maintain code integrity, and streamline project management. Its powerful commands, such as `git commit`, `git branch`, `git rebase`, and `git stash`, provide flexibility and control over your codebase, making it easier to manage complex projects. By mastering Git’s workflows—whether it’s basic version control, branch management, or advanced rebasing techniques—you can significantly enhance your productivity and contribute to a cleaner, more maintainable project history.

Incorporating Git into your development process not only helps you work better individually but also ensures seamless teamwork, especially when paired with platforms like GitHub or GitLab. Understanding Git’s capabilities and best practices is a crucial step in your journey toward becoming a proficient developer or DevOps engineer. With a strong foundation in Git, you are equipped to tackle challenges in version control, collaborate effectively, and deliver high-quality software.

**FAQs**

1. What is the difference between git pull, git fetch, and git clone?
    
    `git pull`, `git fetch`, and `git clone` serve different purposes in Git workflows. `git pull` combines `git fetch` and `git merge`, fetching changes from the remote repository and immediately merging them into your current branch, making it ideal for quick synchronization. `git fetch`, on the other hand, only downloads changes from the remote repository without merging, allowing you to review updates before integrating them. Finally, `git clone` is used to create a local copy of an entire remote repository, typically when setting up a repository for the first time. Each command has its unique use case: pull for immediate updates, fetch for careful review, and clone for starting fresh.
