What is Git?

Git is a free and open-source distributed version control system designed to manage software development projects with speed and efficiency. It was created by Linus Torvalds in 2005 for the development of the Linux kernel. Today, Git is widely used by developers all over the world to manage their code and collaborate on projects with ease.


Basic Concepts in Git:

Here are some of the basic concepts in Git that you need to understand:

1) Repository

A repository is a place where Git stores all the files, directories, and metadata for a project. A repository can be local which is located on your computer or remote which is located on a server that can be accessed over the internet (ex : GitHub, Gitlab)

2) Commit

A commit is a snapshot of all the changes made to the repository at a given time. Commits are stored in a timeline, forming a history of changes to the repository. Each commit is identified by a unique hash code and can be used to revert back to a previous version of the project.

Git Commit Workflow:

  • Create or edit files in your local repository.
  • Stage the changes using the git add command. This adds the changes to a temporary area, ready to be committed.
  • Commit the changes using the git commit command. This creates a new commit in your local repository with a message that describes the changes.

3)Branch

A branch is a separate timeline of changes to a repository. Branches allow you to work on multiple features or bug fixes simultaneously without affecting the main timeline of the project. When a branch is ready, its changes can be merged back into the main branch (usually called the master branch) to form a new commit.

To use branches one should aware about branching policy.

Git branching policy refers to the rules and guidelines that teams follow when using branches in Git. A Git branching policy helps to ensure that everyone on the team is using Git in a consistent and organized manner, and that the branches are used effectively to manage different stages of development.

Here are some of the most common elements of a Git branching policy:

  • Naming conventions: Teams often establish naming conventions for branches, such as using specific prefixes for feature branches or using a consistent format for branch names.
  • Branch creation: Teams may have specific rules for creating branches, such as requiring approval from a certain team member or having specific branching points for different types of branches.
  • Branch life cycle: Teams may establish rules for when branches should be created, when they should be merged, and when they should be deleted.
  • Merge policies: Teams may have specific merge policies, such as requiring a certain number of approvals before merging a branch, or requiring a certain level of code review before merging.
  • Code standards: Teams may have code standards that they enforce through their Git branching policy, such as requiring code to be reviewed before it is committed to a branch.

A Git branching policy can help to keep everyone on the same page and ensure that changes are managed effectively. However, it's important to remember that a Git branching policy should be flexible and adaptable to meet the needs of the team.

In summary, a Git branching policy is a set of rules that a team follows when working with branches in Git, with the goal of improving collaboration and organization. 


4) Merge

A merge is the process of combining the changes from one branch into another branch. Merges are used to bring the changes from multiple branches back into the main branch. Git provides several methods for merging including fast-forward merge, recursive merge  and octopus merge.

Git merge types:

  1. Fast-forward merge: A fast-forward merge is the simplest type of merge. It occurs when the current branch is directly ahead of the branch being merged, and Git simply moves the branch pointer to the merge target.
  2. Three-way merge: A three-way merge is the most common type of merge. It combines the changes from two branches and creates a new merge commit. This type of merge can result in conflicts if there are conflicting changes in the two branches.
  3. Octopus merge: An octopus merge is used to merge multiple branches at once. This type of merge is useful for combining changes from multiple feature branches into a main branch.
  4. Recursive merge: A recursive merge is a type of three-way merge that Git uses by default when merging branches. It performs a three-way merge using a modified version of the merge-recursive strategy.
  5. Squash merge: A squash merge is used to combine multiple commits into a single commit. This type of merge is useful for cleaning up the commit history and making it easier to follow the changes that have been made.
note:  Fast-forward merge and  Squash merge mostly used

5) Stash

A stash is a temporary place to store changes that are not ready to be committed. Stashes allow you to switch between branches or perform other actions without losing your current changes. Stashes can be reapplied later to continue where you left off.

6) Revert

Revert is the process of undoing changes to a repository. Reverts can be used to undo a commit, roll back to a previous version, or remove changes that have been stashed.

7) Push and Pull

Push and pull are the operations used to transfer changes between a local repository and a remote repository. Push sends changes from the local repository to the remote repository, while pull retrieves changes from the remote repository to the local repository.

Here's a brief explanation of the Git push and pull workflow:

  • Git push: The Git push command is used to upload local changes to a remote repository. When you run git push, Git will take the changes you have made in your local repository and transfer them to the remote repository. This is useful when you want to share your changes with others or when you want to backup your work.
  • Git pull: The Git pull command is used to download changes from a remote repository to a local repository. When you run git pull, Git will retrieve the latest changes from the remote repository and merge them into your local repository. This is useful when you want to incorporate changes made by others into your local repository.


Conclusion

Git is a powerful version control system that is essential for modern software development. By understanding the basic concepts of Git, you can effectively manage your projects, collaborate with others, and keep track of changes to your code. Whether you are a beginner or an experienced developer, Git is a tool that can help you to improve your workflow and increase your productivity.