Git- Your first step in the tech :)
Here's a detailed explanation and setup of the most popular version control system.
Why is it needed?
After working on anything, you want to save it before it is interrupted or deleted so that you may pick up where you left off, and maintain a history of the changes you made.
makes any sense?
Let's take an example, I want to create a personal website. I spent days working on the coding for it. But hold on, my system isn't working, my hard drive may have failed, or I might have made a mistaken modification to my code and I can't go back to how things were.
this has happened to me :(
This is where Git steps in to save you from all the hassles and disappointments by keeping track of your current project and all the changes you've made rather than saving every single version with a new name on your machine.
There are various programs available to help you maintain track of your work, but in this article, we will solely discuss Git. Git is a version control software that records changes to the code.
It's a talent that every company wants, and BOOM, you can add it to your résumé. :D
Ok! Perhaps you're not happy with it. Perhaps you feel safe storing every file locally.
Lemme answer that as well :)
Since numerous abilities would help the website and I need additional individuals, I am now intending to work with my friends on it. However, having it on your local makes it a little more challenging to use. Since you should always collaborate rather than operate remotely.
With a clear knowledge of where, what, and by whom changes have been made, Git may be used to merge a new version of an already existing version from another user.
Now make your friends do some work too :D
Git
Git is a distributed version control system that allows any user to modify the entirety of a repository on their computer. It makes use of the command line, and Git makes it simple to reverse changes repeatedly while providing a detailed account of what was changed. It can do the following:
Tracking the changes, or many iterations of the same file.
It also maintains a list of all the files that are included in a project.
Comparison and analysis of several codes with thorough justification.
Now you know why to use it, but how?
The first step in getting started with Git is to create a local repository and a remote repository.
Local repository: A local repository is a working path or directory that you have built on a local computer. Up until you push it to the remote repository, the repository where you write your code is private to you.
Remote repository: For hosting your websites, you may use a remote repository, such as GitHub or Bitbucket, which is a public directory or platform. Git makes it simple to push a section of code or a whole project to a remote directory.
Installation
You should now install it on your machine. To install it, kindly look at this link.
I hope you installed it :)
Key concepts
Let's attempt to comprehend some of the main terms used here:
Version Control System: This program keeps track of various versions of the codes. likewise known as source code management.
Commit: After making your changes, you should send them from the local repository to the staging area or staging index, then to the remote repository. Your code is now saved to Git. Please click the git commit link to learn more about committing.
Checkout: When the working directory has been duplicated with all of the repository's content.
Secure Hash Algorithm, or SHA: Each commit receives its ID. Branch: When you deviate from the primary route of development and carry on working without interfering with it.
Configure your Git
You may set up the user credentials for your local and remote repositories by using the command cd
to go to your working directory. The code is pushed into a remote repository using these credentials.
Add your username and user email-id
git config --global user.name "Enter your name here"
git config --global user.email "your_email_address@domain.com"
Use the following command to check the changes you made.
git config -l
Let's see how to work locally with git
A repository must first be present on a local system. The current project will be kept in this repository; it may then be uploaded to GitHub. To navigate the repositories, we utilize the command line. Go to the path using the cd command, then use the mkdir
command to create a directory.
mkdir git-test
git init
Use the git init command to initialize the directory after it has been created. The master branch is now the result of the git init command, which starts a fresh git repository. The source code and other development materials are kept in the repository, which is a version control system.
git status
It is simple to see the changes made in a repository using git status. Let's assume I used the git status command to add a new file to the branch that Git is showing. By executing this command, any modifications may be noticed. This does not imply that the modifications are made and saved.
git add
Before it is committed, the modifications made should be included. Using the git add command, the code or any file is added to the staging area.
To add multiple files at one go we use the command
git add .
All the modified files present in the local repository are moved to the staging area.git commit
It is used to store the file in a repository from the staging area.
Commands to review history in Git
When working on a piece of code, you'll probably want to undo any changes you've made or want to see how the code looked before you made them. Some commands may be used to view previous modifications.
git log
to see every commit that has been made to the repository. The git log command is used. By including the SHA key in the command, we can see a particular commit come first, then all the others.
git --stat
Using this command, you can see which files were changed and how many lines of code were added or deleted.
git log -p
Using this command, you may see the actual changes—the lines that have been added or removed—that have been made. The patch is indicated by the -p.
You may supply the SHA key to any of these commands, which may be beneficial for seeing a specific commit.
Branching in Git
Creating a new branch
One should make it a practice to make modifications in a distinct branch before making them straight to the master branch. Even though you can roll back to the prior commit if the new modifications don't work, doing so costs the organization money.
Instead, it is preferable to work on a fresh branch, test the changes, and then transfer them to the living environment.
Use the command git branch <branch-name>
to add a branch that references the master. Use git branch <branch-name>
SHA instead if the modifications need to be made to a commit that isn't a master right now. The new branch should be added after which commit, according to SHA.
We transfer the master branch to a new branch named <branch-name>
using the command git checkout <branch-name>
. By using the same procedures, we used with the previous master branch, you can now add and commit modifications to this branch. after that, your branch has been pushed to the distant repository:
<branch-name> git push -u origin
If successful, use git merge <branch-name>
to combine the code with the old master branch. However, you must use the git checkout
command to return to the previous master branch before executing the merge command.
Using git commands, it is also possible to rename, remove, and compare branches:
Rename:
git branch -m <branch-name> <new-branch-name>
Delete:
git branch -d <branch-name>
Compare branches:
git diff <branch-name> <master-branch-name>
TL; DR
Git is an open-source, free program. It delivers at a higher speed and is simple to use. It enables you to execute your code in the cloud and keeps track of every change that has ever been made to the project, or the whole history of changes. Each commit includes the name of the author, a brief description, a date, and the SHA1 hash of the previous commit.
Git is advantageous when a team is operating remotely. Since anybody may edit as long as they have access to the remote repository.
Your local repository may be connected to any remote repository, including GitHub, GitLab, Bitbucket, and others.