What is Git?
Git is a specific open-source version control system created by Linus Torvalds in 2005.
Version control is an essential aspect of modern software development, allowing teams to efficiently manage code, collaborate seamlessly, and track changes over time.
Git is used to store the source code for a project and track the complete history of all changes to that code. It lets developers collaborate on a project more effectively by providing tools for managing possibly conflicting changes from multiple developers.
What is GitHub?
GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.
Additionally, anyone can sign up and host a public code repository for free, which makes GitHub especially popular with open-source projects.
What is Version Control?
Version control helps developers track and manage changes to a software project’s code. As a software project grows, version control becomes essential.
Types of version control systems
There are two primary types of version control
Centralized version control system (CVCS)
Centralized version control systems, such as Subversion and Team Foundation Server, rely on a central server to store the repository, with individual developers checking out files for editing.
Drawback of CVCS
It is not locally available, We always need to connected to a network to perform any task.
If the Central Server gets down then all the work get down.
Distributed version control system (DVCS)
In DVCS every contributor has a local or clone copy of the main repository. i.e. everyone maintains a local repository of their own. Which contain all files and metadata present in main repository.
**
Differences between CVCS & DVCS**
Stages of Git / Workflow
To understand Git's workflow, let's explore its three main elements:
Working Directory: The working directory represents the files we are actively working on in our local computer.
Staging Area: The staging area serves as an intermediary step between your working directory and the local Git repository. It holds either the snapshot of the last commit saved in the repository or the new changes you want to include in the next snapshot (commit).
Local Git Repository: The local Git repository is created using the git init command. It stores the complete history of commits and serves as the central database for our project.
Informing Git about changes
To incorporate changes into the next commit, we need to inform Git using the git add command. This command allows us to specify which changes to move from the working directory to the staging area.
Creating Commits
Once we have added the desired changes to the staging area, it's time to create a new snapshot, known as a commit. Use the git commit -m "{some-commit-message}" command to commit our changes. A commit in Git consists of an identifier (ID), a message describing the changes, the date/time of the commit, the author, and a complete snapshot of our repository at the time of the commit.
Branching in Git
Branching is a fundamental aspect of Git, enabling parallel development and experimentation. The branching models differ between centralized and distributed version control systems.
Centralized: Branching in centralized systems like Subversion is cumbersome and inefficient. Branches are complete copies of the whole repository, making branch management complex.
-
- Distributed: Git excels in branching, providing lightweight and efficient branch management. Branches in Git are essentially pointers to specific commits, stored in a simple file. The HEAD pointer indicates the branch you are currently working on.
Git Basic Terms
Git Repository - A Git repository is a directory that contains a Git working directory. The working directory contains all the files that are tracked by Git.
Git Remote Repository - A Git remote repository is a repository that is stored on a remote server. We can think of a Git remote repository as a remote file system.
Branch - The branch can be thought of as an alternate repository that diverges from the Git Working Directory or main project. A Branch can be a different version of the same repository.
Checkout - Checkout is a command that is used to switch between branches.
Merge - Merge is a command that is used to merge the changes from one branch to another branch.
Pull - Pull is a command that is used to pull the changes from the remote repository to the local repository. Git Pull will fetch the changes from the remote repository and then download that content locally from the remote repository.
Push - Push is a command that is used to push the changes from the local repository to the remote repository.
Clone - Clone is a command that is used to clone the remote repository to the local repository.
Status - Status is a command that is used to show the status of the local repository.
Fork - A git fork is a copy of a repository that is stored on a remote server.
Rebase - Rebase or we can say rewriting history, Its a powerful feature of Git, using this we can move commits to a new base commit. Suppose we found a bug that was previously not there, and after so many changes now a particular feature is broken in that case we can rebase the project to the last stable version.
Stash - Stash command can be helpful when we don’t want to commit the changes in the working branch. But we want to switch the branch and after finishing with the branch we want to come back and commit those leaved changes. In this kind of scenario the git stash commands help, it saved our uncommitted changes for later use.
Git revert - Git Revert is a command that is used to revert the commits. We can revert the commits using the command git revert. Think of it as an undo command in Git.
Git reset - Git Reset command will reset the changes in the working tree. It will reset the changes in the index and the working tree. It will also reset the HEAD to the previous commit. Depending upon which forms we used for the git reset command, it will reset the git repository. following are the forms of the git reset command.
Sources: