Pages

Wednesday, January 31, 2024

How Git Works?








There are 4 main location where our code lives in Git. 

The working directory: where we actively edit files locally.

The staging Area: It is the temporary holding spot for changes before committing. 

The local repository: This is where we store committed changes locally. 

The Remote repository: A server like Github for sharing and backing up code. 

Most Git commands move files between these four locations. 

The first step is git clone of existing repository so you have a local version of the project to work on, complete with all its history. With the repo clone locally, we are in the working directory. This is your local development environment where you make changes to your code. 

When you are ready to commit your changes, you'll use git add to stage a snapshot of those files in staging area. Think of this as a checkpoint, where your changes are gathered and ready to be finalized.

Next step is to use git commit, which takes the snapshot of the staging area and saves it to your local repository. This locks in those staged changes by creating a permanent record that you can refer back to, like a snapshot in time. 

Your code just doesn't stay on your local machine, When you are ready to share your progress with the team or backup your work, you use git push to send your commits to the remote repository. This is often a shared server where your team can collaborate, like github or bitbucket. 

Collaboration in git is a two way exchange, To integrate your team mates work, you use git pull, which fetches changes from the remote repository and merges them into your local repository. It combines two commands: git fetch, which grabs the latest updates, and git merge which integrates these updates with your work. 

There are times when you need to switch contexts, perhaps to fix a bug in another branch. Thats where the git checkout or git switch comes in. It allows you to switch between different branches to work on specific features. 

Git branching allows you to diverse from the main codebase to develop a new feature without impacting the main code. Some key concepts include creating a new branch with git branch and switching between branches using git switch, merging branches together with git merge and resolving merge conflicts when changes overlap. Branching enables isolate development and collaboration workflows. There are more nuance when merging or rebasing changes from others or managing branches. 

Many developers use graphical git tools like github desktop and sourceTree. These tools provide visual interfaces and shortcuts for common commands. 

###

Git is a distributed version control system (DVCS) widely used for tracking changes in source code during software development. Here's an overview of how Git works:

1. **Local Repository**:

   - Git operates primarily on a local repository, which resides on the user's computer. This repository contains the entire history of the project, including all files, commits, branches, and tags.

2. **Working Directory**:

   - The working directory is the directory on the user's computer where the project files are stored and edited. When changes are made to files in the working directory, Git tracks these changes and allows them to be staged for commit.

3. **Staging Area**:

   - The staging area, also known as the "index" or "cache," is a temporary storage area where changes to files are prepared (staged) for commit. Files in the staging area represent the next snapshot of the project that will be committed to the repository.

4. **Commits**:

   - A commit is a snapshot of the project's state at a specific point in time. Each commit records changes to files, along with metadata such as the author, timestamp, and commit message. Commits are immutable and uniquely identified by a cryptographic hash.

5. **Branches**:

   - A branch is a lightweight movable pointer to a specific commit in the repository's history. Branches allow developers to work on different features or bug fixes concurrently without affecting the main project. The default branch in Git is typically named "master" or "main."

6. **Merging and Rebasing**:

   - Merging combines changes from different branches into a single branch, typically the main branch. Git automatically resolves conflicts between conflicting changes during the merge process.

   - Rebasing is an alternative to merging that rewrites the commit history by moving, copying, or deleting commits. This can create a linear history and avoid merge commits.

7. **Remote Repository**:

   - Git supports remote repositories, which are copies of the repository hosted on remote servers, such as GitHub, GitLab, or Bitbucket. Remote repositories allow multiple developers to collaborate on the same project and synchronize changes across different machines.

8. **Pull and Push**:

   - Pull is the process of fetching changes from a remote repository and integrating them into the local repository. This updates the local repository with changes made by other developers.

   - Push is the process of sending local commits to a remote repository, making them available to other developers. This updates the remote repository with changes made locally.

9. **Distributed Nature**:

   - Git is a distributed version control system, meaning that each developer has a complete copy of the repository, including its entire history. This allows developers to work offline, commit changes locally, and synchronize with remote repositories later.

Overall, Git provides powerful version control capabilities, enabling efficient collaboration, code review, and software development workflows. Its flexibility, speed, and robustness make it one of the most widely used version control systems in the software development industry.



No comments:

Post a Comment