🔹What is Git?
Git is a distributed version control system (VCS) widely used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project by managing revisions, branches, and merges effectively.
Here are some key concepts and features of Git:
Repository: Git operates based on repositories, which store the entire history of a project, including all changes made to files. A repository can be local (on a developer's machine) or remote (on a server or hosting service).
Commit: A commit is a snapshot of the project's state at a particular point in time. It represents a set of changes made to one or more files. Each commit is uniquely identified by a hash code, and commits are organized in a linear sequence.
Branch: A branch is an independent line of development within a Git repository. It allows developers to work on different features or bug fixes simultaneously without affecting the main codebase (often referred to as the "master" or "main" branch). Branches can be created, switched, merged, and deleted.
Merge: Merging combines changes from different branches into a single branch. It integrates changes made in one branch with another, bringing them together to create a unified codebase.
Remote: Remotes are copies of a Git repository hosted on a server or a shared location. Developers can push their local commits to a remote repository to share their changes with others. Git supports various remote hosting services like GitHub, GitLab, and Bitbucket.
Pull: Pulling is the process of fetching changes from a remote repository and integrating them into the local repository. It combines a fetch operation (retrieving changes from the remote) with a merge operation (integrating the changes into the local branch).
Push: Pushing involves sending local commits to a remote repository, updating it with the latest changes made by a developer. It allows developers to share their work with others and collaborate on a project.
Conflict Resolution: Git helps in managing conflicts that arise when multiple developers make conflicting changes to the same file or code section. Conflicts need to be resolved manually by the developers, who can compare, modify, and merge conflicting sections.
Imagine you are working on a project, and you make changes to files as you progress. But sometimes, you might make mistakes, and you wish you could go back to a previous version of the files. Git is like a time machine for your project! It keeps track of all the changes you make to your files, allowing you to go back to any version whenever you want. So, if something goes wrong, you can easily fix it by reverting to an earlier version.
Moreover, if you are working with a team, Git helps you collaborate seamlessly. It allows multiple people to work on the same files simultaneously without interfering with each other's work. When everyone is done, Git helps merge all the changes into a single, cohesive version of the project.
🔹What is Github?
GitHub: Now, think of GitHub as a place to store your project's time machine. It's like a safe cloud storage for your Git-powered projects. With GitHub, you can save your project's history (all the versions and changes) online, making it easy to share your work with others. It's like having a backup of your project in a secure place.
Additionally, GitHub makes collaboration even more enjoyable. You can invite other people to your project, and they can make changes and suggest improvements. GitHub keeps track of who did what, so you always know who contributed what part to the project.
In a nutshell, Git is the version control system that helps you manage and track changes in your project, and GitHub is the platform where you can safely store and collaborate on your Git-powered projects.
🔹What is Version Control? How many types of version controls we have?
Version control is a system that keeps track of changes made to files over time. It acts like a "time machine" for your project, allowing you to go back and see how your files looked at different points in the past. With version control, you can also work with a team more efficiently, avoiding conflicts when multiple people make changes to the same files.
Types of Version Control: There are two main types of version control systems:
Centralized Version Control System (CVCS): Think of a CVCS like a library where everyone borrows and returns books. In this system, there is a central server that stores all versions of the project's files. When you want to work on a file, you "check out" a copy from the central server. After making changes, you "check in" the updated file back to the server.
Examples of CVCS are Subversion (SVN) and Perforce.
Distributed Version Control System (DVCS): Imagine everyone having their own copy of the library with all the books. In a DVCS, each developer has a complete copy of the entire project, including its entire history. This means you have a full backup on your computer.
You can work independently and make changes to your copy without needing to connect to a central server. Later, you can "merge" your changes with others to combine everyone's work.
Git is the most popular example of a DVCS.
In simple terms, version control helps you keep track of changes and collaborate better, while CVCS and DVCS are like different ways of managing and sharing the changes with your team.
🔹Why we use distributed version control over centralized version control?
We use distributed version control (DVCS) over centralized version control (CVCS) for several reasons:
Better Collaboration: In a DVCS, every team member has a full copy of the project, including its entire history. This means each person can work independently and doesn't have to constantly connect to a central server to make changes. It's like having your own personal copy of a book in a library. This flexibility allows team members to collaborate more efficiently and without interruptions.
Improved Speed: With DVCS, since each team member has a local copy of the project, they can work faster and commit changes without waiting for a central server's response. It's like having a personal workstation for your work that doesn't depend on others.
Greater Flexibility: DVCS allows developers to work offline. Imagine you're on a long flight without internet access, but you still want to make changes to your project. With DVCS, you can do that on your local copy and later synchronize the changes when you're back online. It's like having a portable version of your work that you can carry anywhere.
Enhanced Security: In a DVCS, each developer's local copy is a complete backup of the entire project. This redundancy means that if the central server or repository is lost, each team member's copy serves as a backup. It's like having multiple backups of important documents in different locations for added safety.
Easier Branching and Merging: In DVCS, creating branches to work on new features is simple and doesn't interfere with the main project. Developers can work on their tasks independently in their branches and later merge their changes back to the main project effortlessly. It's like having separate spaces for different tasks that can be easily integrated later.
Overall, distributed version control provides more freedom, flexibility, and security for teams working on projects. It's like having personal workspaces for each team member that can be seamlessly combined into the final product. These advantages have made DVCS, like Git, the preferred choice for many software development teams worldwide.
🔹Install Git:
For the latest stable version for your release of Debian/Ubuntu
apt-get install git
#For Ubuntu, this PPA provides the latest stable upstream Git version
add-apt-repository ppa:git-core/ppa
apt update
apt install git
Create a GitHub Account: If you don't have one already, create a free account on GitHub. Visit github.com and click on "Sign up" to create your account. You'll need to provide a username, email address, and password.
Creating the repository after login:
then click the create repository.
then clone the project first you have to click the code icon then copy the HTTPS url.
Learn the Basics of Git:
git init: Initializes a new Git repository in your current directory. It sets up the necessary data structures and configures the repository.
git init
git add: Adds the specified file(s) to the staging area. Files in the staging area are ready to be committed in the next step.
git add <file(s)> or git add .
git commit: Records the changes in the staging area to the repository. It creates a new commit with a descriptive message that explains the changes made.
git commit -m "Commit message"
git status: Shows the status of your working directory and staging area. It lists all the modified, added, and deleted files.
git status
git log: Displays a chronological list of commits in the repository, showing the commit ID, author, date, and commit message.
git log
git branch: Lists all the branches in the repository. It indicates the current branch with an asterisk.
git branch
git branch <branch_name>: Creates a new branch with the given name. The new branch will be identical to the current branch.
git branch <branch_name>
git checkout <branch_name>: Switches to the specified branch. It updates the working directory to reflect the content of the selected branch.
git checkout <branch_name>
git push <remote> <branch>: Uploads the local commits to the remote repository. It updates the specified branch on the remote with the local changes.
git push origin <branch>
git pull <remote> <branch>: Fetches changes from the remote repository and merges them into the current branch. It updates the local repository with the latest changes from the remote.
git pull origin <branch>