# Git for Beginners

### **What is Git**

* **Git** is a **Distributed Version Control System (DVCS)** used to **track changes in files and source code** during software development.
    
* Git is a distributed version control system that manages and tracks project changes efficiently.
    

### **Why is used Git**

* Git is used to **manage, track, and control changes in source code** during software development.
    
    * **Tracks Code History:** Saves every change, so you can return to any previous version.
        
    * **Enables Team Collaboration:** Multiple developers can work on the same project without overwriting each other’s work.
        
    * **Supports Branching:** Developers can create separate branches to work on new features safely.
        
    * **Data Integrity:** Git uses cryptographically secure hashing (SHA-1) to ensure the integrity of the code and its change history, protecting against accidental or malicious corruption.
        

### **Common Git Commands:**

| `git init` | Initialises a new git repository in the current directory |
| --- | --- |
| `git clone <url>` | Creates a local copy of a remote repository from specific URL, including all its files, history and branches |
| `git status` | Shows the status of changes as untracked, modified, or staged in the working directory |
| `git add <file_name>` | Stages a change, adding a file or changes in a file to the staging area, which is the next snapshot to be committed |
| `git add .` | Stages all new changes and modified files in the current directory and its subdirectories |
| `git commit -m <message>` | Saves the staged snapshot to the project history with a descriptive message |
| `git diff` | Show the difference between the working directory and the staging area |
| `git branch` | Lists all local branches in the repository |
| `git branch <branch_name>` | Creates a new local branch |
| `git checkout <branch_name>` | Switches to the specified branch or a previous commit |
| `git checkout -b <new_branch_name>` | Creates a new branch and switches to it in one command |
| `git merge <branch>` | Merges changes from the specified branch into the current active branch |
| `git remote add <alias> <url>` | Adds a remote repository to your local configuration |
| `git push <remote> <branch>` | Uploads local commits to the remote repository (eg: git push origin main) |
| `git pull` | Fetches changes from the remote repository and integrated them into the current local branch |
| `git fetch` | Downloads changes commits and refs from a remote repository into your local copy but does not automatically merge them |
| `git log` | Displays a log of all commits in the current branch history |
| `git stash` | Temporarily saves changes that are not ready to be committed, allowing you to switch branches or work on something else |
| `git config` | Used to set user specific configurations like make and email address before committing. |

### Git Essential Commands

* ### git init
    
    Initialises a new git repository in the current directory.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769323792841/3256919a-ad96-471f-ae97-584e05bafc69.png align="right")
    
    ### git status
    
    Shows the status of changes as untracked, modified, or staged in the working directory.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769324040976/b1487248-4d77-4376-b47c-edf9d66d3c92.png align="center")
    
    **git add**
    
    Stages a change, adding a file or changes in a file to the staging area, which is the next snapshot to be committed.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769324316226/3bd0f4d1-d044-4e26-ad9e-a5dc42c66d3b.png align="right")
    
    **git Commit**
    
    Saves the staged snapshot to the project history with a descriptive message
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769324655813/a2a70238-e659-462a-b99c-0e918f2510ba.png align="center")
    
    **git log**
    
    Displays a log of all commits in the current branch history.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769324796218/9e729c39-90a4-4a48-a995-9ad4262a88bb.png align="center")
    
    ### **Basic Developer Workflow Using Git**
    
    Creating a local repository and initialising it with git
    

```bash
cd my_project
git init
```

• Clone an existing remote repository (alternative)

```bash
git clone https://github.com
cd repository_name
```

* Create a new branch
    

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

* Make changes
    
    * Modify files, add now ones, or delete old ones with in your project directory using your preferred code editor
        
    * Stage your changes
        

```bash
git add . //To add all files to staging
git add specific_file.txt   // To add specific style
```

* Commit your changes
    

```bash
git commit -m "Initial commit"
```

* Push your changes
    

```bash
git push --set-upstream origin feature/my-new-feature
```

* Create a PR
    
    * Go to remote repository’s website (GitHub or gitlab) and open a pull request to merge your feature branch in to the main branch. This allows team members to review your code before it is integrated
        
* Merge the PR
    
    * After review and approval, merge the pull request via the web interface
        
* Update your local main branch
    

```bash
git checkout main
git pull origin main
```

* This process creates a clear, traceable history of changes and facilities collaboration with in a development team.
    

Git Working Directory → Staging area → repository

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769325738299/42e84a52-8827-4c48-a5dd-9b667d2afbe0.png align="center")
