# Git

## Summary


Git is a popular free and open source distributed version control system for managing small to large-scale projects. It keeps track of the changes made to files in a project, making it easy to roll back when necessary. It is compatible with a wide range of operating systems and integrated development environments making it accessible to a large range of developers. 

Git is a software that tracks Files and their history. Online hosting providers of git such as Github helps developers to remotely collaborate and combine each other's work.

## Discussion

### Why do developers need to learn Git?

Git is useful for everyone from web developers to app developers who develop code or make changes to their files , if a set of people are working on one project, there is a risk of overwriting or conflicting with each others work to avoid these problems we can use Git which help teams to collaborate by implementing version control system(VCS). The most significant advantage is they can track and compare the modifications made to the files and revert back to their earlier versions if necessary. 

Version Control System (VCS) are specialized software which keeps the code in a central location and allows a team of people for sharing, collaborating and to track and make changes to their work which makes it easier to develop software in a continuous and straightforward manner. 


### What is a Git Repository?

A Repository in Git stores all the project related files as well as the revised history of all the files. It is virtual storage for your projects as it creates a sub-directory named .git (Usually hidden) inside your project directory to store these files. This directory is the heart of the repository where it also tracks the changes done to the files. So that deleting the.git/ subdirectory deletes the history of your project.  

There are different ways to create a repository git init command is used to create a new repository locally and the git clone creates a local copy of a full copy of an existing repository stored on a remote server .


### What is the difference between Bare and Non-Bare git repository?

A bare repository is a git repository that is just used for collaboration among multiple individuals for push and pull, with no active development or code written directly. It can serve as a remote repository for team members to share code. Bare Repository can be created using `git init --bare` command and if you list the contents inside the directory you can see the contents of what is normally found inside the .git directory. Individual users can't make changes or create new versions. .

In a non-bare repository, files under that reside under .git directory retain the snapshot of tracked files and allow you to edit the files under the working directory, whereas in a bare repository, you will not see the .git directory and instead all of the files that normally reside under .git will be present right over in the root directory 

`git init` command, by default creates a non-bare repository. \* Except for a few variations,both bare and non-bare repositories are almost identical  .


### What is the Difference between Git and SVN?

Git is a distributed version control system (DVCS) that saves a local copy of the repository on each member's machine. Any modification made to these files, referred to as a commit, is compared with the previous version and updates happen only to the alterations made in the files. Each user has their own repository as well as a working copy. Other members won't be able to see your modifications until you push changes to the central repository. 

SVN (Subversion) is a centralised version control system (CVCS) that requires individuals working on a repository to download the most recent version from a central location. Limitations of CVCS is that it need to be connected to the central repo to get the latest version of repository and also it can't perform other repo operations locally like DVCS. Despite the fact that each user has their own working copy, there is only one central repository where other members can see and update your changes as soon as you push them. 


### Tell me some interesting features of Git

    + **Troubleshooting**: git blame command shows line-by-line revision history done by an author. git bisect command is also used to assess good or bad commits by going throughout the project history. When we have a little idea of how a certain bug was introduced git bisect can be useful whereas git blame can be used when we couldn't find the exact cause for the problem.
    + **Branching and Merging**:Branches create separate copies of files in a repository for users and changes made to these files by users is not reflected on the main code and is independent of each other. This can help users to test and develop new features and commit to the main code only if required. A Sequence of commits from branches are called merging.
    + **Rebasing**: Changes made to the files can be replayed in the same sequence. Commits can be made on the main branch on top of other different commits made recently by another user. Each commit is made one by one so that if a conflict happens the rebasing is paused until it's fixed.

### What is a Git workflow?

It is a method of using Git in a productive and feasible manner. There are no specific rules these are some common practices and can be considered as guidelines for the development. Some commonly used workflows are described below :

 

    + **Centralized Workflow**: Similar to SVN a single collaboration model is conceptually centralized though it is decentralized technically. Though the project is contributed by multiple users there is only a single-point-of-entry for the changes made. The work done by each developer is merged before the changes are pushed no overwriting is allowed here.
    + **Trunk-based development**: Development happens on a single branch called Trunk the users divide work into small pieces and merged it into the trunk frequently within few hours. This also decreases the chances of merge conflicts.
    + **Git Flow Workflow**: There are many branches split for different purposes. Developments happen in the Develop branch with feature branches split for new features combining back to develop after getting ready. Release branch contains the code after test and is ready to be merged into the main branch. Final code ready for the release is only available in the main branch.

### Can you describe Git add and commit?

The Git add command includes the new or modified files in our working directory to the staging area.Files in the staging area are the ones ready to be added in the next commit. It asks Git to include these files for the upcoming commit. You can add directories, any files, even a specific part of a file. If a deleted file is added using this command then the deletion will be staged for commit. 

The term Committing is similar to saving files. Git commit would never happen without using git add for adding files to the staging area. Commits are like a recent snapshot of the repository and can show the history over a period of time. It also includes metadata like author name, timestamp etc. git revert command can be used to undo or redo a specific commit . git reset removes a specific commit and rollsback to the required commit by user . If you use git reset accidentaly and lose commits, you can use git reflog to get them back .


### Mention the three states of Git?

Git considers each file in a directory initially to be in one of two states: tracked or untracked. Files that have already been captured by git are known as tracked files. Everything else is untracked files, which are any files in your working directory not yet captured or not part of git's version control. 

These three file states are possible after the files are tracked by Git .

    + **Unmodified/Modified**: State of the file changes to modified whenever Files in a Repository is being edited until it is committed and stored to the local git database.
    + **Staged/Staging area**: When we've completed all the modifications to the files it will be moved to the staged state and is ready to be committed.
    + **Committed**: All modifications are saved in the local database, and the updated files are committed.

### Can you suggest some best practices in using Git?

Software development teams can adapt their own best practices according to their needs so there are no definite answers. Listed below are some recommended practices followed in the industry.  

    + **Keep the changes minimum**: Use the least amount of code possible to solve an issue so it is easy to revert back if that doesn't work as expected. Git is known to have atomic behaviour means if there is an issue git will commit files partially and avoid the rest. So only one fix or one upgrade at a time is recommended
    + **Writing Effective commit messages**: Describe Commit messages in a meaningful way by using a verb in the present tense. Clear messages benefit both teams and self when we look back later.
    + **Doing Code Reviews**: Code shall be evaluated by one or a team of developers to avoid mistakes, potential bugs.
    + **Make use of Branches**: Branches can be used to separate work among developers for efficient workflow. Code is verified and tested before merging into the main or master branch.

### Which should I choose Mono-Repo vs Multi-Repo?

The term "Mono-Repository" refers to the storage of all codebase related to a project such as its libraries, dependencies, or services, in a single repository. This approach helps new contributors to make their initial setup easy since everything related to the project is stored in a single location. Deploying a component independently becomes difficult since all the files are tightly coupled with each other also the size of the repository would be huge in large projects. 

In Multi-Repository all the project related services are stored in a separate repository. Libraries, dependencies can run altogether from an isolated environment. Teams can work autonomously with different responsibilities but need to better communicate and sync to avoid breaking the code while all the work happens independently.

There is a Multi-Mono approach known as submodules that keeps one git repository as a subdirectory of another repository.  

Though these approaches have the same goal to manage the codebase with their own benefits and drawbacks. Some challenges are managing the final release, collaborating and communication among the team. Finally, it is up to the team to decide which would suit their workflow.

## Milestones

Apr  
2005

Linus Torvalds designed the main principles of Git in a week of time to maintain the large Linux Kernel Project. Previously, a free DVCS named Bitkeeper was used until they revoked the free-of cost usage . The Word Git is a random three letter word not used by any UNIX command it doesn't have any meaning officially.


Jul  
2005

Linus Torvalds established Git as an open source for contributions, and Junio C Hamano is designated as Git's maintainer.  

Dec  
2005

Git 1.0 version was launched 

Jan  
2006

The .dircache directory is renamed to .git which stores all the information about the repository. . A command init-db to initalize repository is changed to git-init-db.  

Oct  
2007

Preston-Werner and Wanstrath wish to develop a platform where where programmers could host and collaborate in Git and started working on their idea of building Github.


May  
2012

Git logo was updated 

Nov  
2018

git range-diff command is introduced in version Version 2.19 it can be used to compare two sequences of commits and order of the changes made. 

Mar  
2021

git maintenance is introduced to optimize the repository data by speeding up commands and reducing the storage requirements.