Git is a version control system used to track changes in computer files and coordinate work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files.
As a distributed revision control system, it is aimed at speed, data integrity, and support for distributed, non-linear workflows. Linus Torvalds created Git in 2005 to develop the Linux kernel, with other kernel developers contributing to its initial development.
Before we get into more specifics about Git, let's go over what version control is and why it is useful?
A Version Control System (VCS) allows developers to work on the same codebase concurrently and maintain a complete history of every change that has been made to the code. This way, if a developer wants to roll back changes or revert to a previous version, they can do so easily.
There are two main types of version control systems: centralized and distributed.
In a centralized VCS, a server stores all the files, and developers can check out files from the server to work on them locally. They can check the files back into the server when they are done. The most popular centralized VCS is Subversion.
There is no central server in a distributed VCS, and every developer has a complete local copy of the entire codebase. Every time a developer makes a change, they can push those changes to other developers. The most popular distributed VCS is Git.
Fast - Git is very fast.
Scalable - Git can easily handle projects with large numbers of files and developers.
Secure - Since every developer has a complete local copy of the codebase, and changes are only pushed to other developers, there is minimal risk of data loss.
Flexible - Git allows developers to easily create and switch between different branches, making it easy to experiment with new features without affecting the main codebase.
To install Git on your computer, you can download it from the official website:
Or if you are using a Mac, you can install it using Homebrew:
After installing Git, you need to configure it with your name and email address. This is important because every Git commit uses this information to identify who made the change.
To set your username and email, use the following commands:
git config --global user.name "Your Name" git config --global user.email "email@example.com"
A repository is simply a place where all the files for a particular project are stored. A repository can be hosted on your computer or a server, and there are several different services, such as GitHub and Bitbucket, that allow you to host repositories online.
To create a new repository, use the Git init command:
This creates a new folder called .git that contains all of the necessary files for Git to work correctly.
Once you have created an empty repository, you can start adding files to it. To do this, use the Git add command:
git add <filename>
This adds the file to your local repository and stages it for commit. To unstage a file, use the Git reset command:
git reset <filename>
The next step is actually to commit these files into your repository. This is done with the git commit command:
git commit -m "commit message"
This commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use the Git reset command with the --soft flag:
git reset --soft HEAD~1
The HEAD~1 notation tells Git to return to the previous commit in your history. You can also specify a particular commit sha to reset to.
To push these changes to your remote repository, use the git push command:
git push origin master
Replace origin with the name of your remote repository, and master with the branch you want to push your changes to.
Here's a list of commands I frequently use on a daily basis in case it's helpful for you.
git add .: adds all the files
git commit: records the file permanently
git push -u origin master: for pushing local files to GitHub
git config: it controls set for the local save project/file
git help: displays all the necessary information about git commands
git status: gives all the information about the current branch
git log: get to know about the previous commits
git diff: runs a diff work on Git information sources. These information sources can be submitted, branches, records, and then some
git reset --hard: deletes all your uncommitted changes | dangerous command lol
git remote add <url or address>: to add a new remote address
git rm: to remove file from Git repository
git branch: the tip of a progression of submits—it is anything but a holder for submits.
git checkout: allows you to explore between the branches made by the git branch
git tag: tags are utilized to check a submit stage as important
git fetch: this command advises your local Git to recover the most recent meta-information data from the first
git rebase: you can take every one of the progressions that were submitted on one branch and replay them on an alternate branch.
git config -global color.ui true: see a different color on different outputs
git init: creates a new git repository
git commit -m "New instruction to do some specific task": saves your changes in the local repository
git show: that is utilized to see extended subtleties on Git items like masses, trees, labels, and submits.
git merge: allows you to take the autonomous lines of improvement made by the git branch and coordinate them into a solitary branch
git pull <repo link>: to download the folder from the remote repository
git stash save: stores modified tracked files
git stash drop: discards most recent stashed files
This is just a basic introduction to Git. Check out the Git Documentation or one of the many excellent resources available online for more information.