Git for Beginners
If you are a Beginner and you don't know what's Git. Here's an awesome article to help you out!!
If you guys quickly search on Google about Git it says that:
"Git is a distributed version-control system for tracking changes in source code during software development."
So, What is a Version Control System?
First thing first, try reading "version control" as "control version".
A Version Control System (VCS) or also called as Source Code Manager (SCM) is just software that helps you control (or manage) the different versions of something (typically your source code).
Git is an SCM and therefore a VCS! The URL for the git website is "https://git-scm.com" and you can see how it has "SCM" directly in its domain!
There are a lot of Version Control Systems:
But, there are two main types of Version Control Systems:
1. The Centralized Model - where, all users connect to a central, master repository.
2. The Distributed Model - where, each user has the entire repository on their computer.
Git Terminologies / Keywords :
1. Repository / Repo:
A Repository is a directory (folder) which contains your project work, which are used to communicate with git. Repositories can exist either locally on your computer or as a remote copy on another computer. A repository is made up of commits.
Every time you save (commit) the state of your project in git, it basically takes a picture of what all your files look like at that moment and stores a reference to that commit. You can think of it as a save point in a game - it saves your project's files and any information about them.
3. Working Directory:
The Working Directory (folder) are the files that you see in your computer's file system. When you open your project files on a code editor, you're working with files in the working directory. This is in contrast to the files that have been saved (in commits) in the repository.
Branch in git is similar to the branch of a tree. Going back to the example of save point in a game, you can think of a branch as where you make a save point in your game and then decide to try out a risky move in the game. If the risky move doesn't pan out, then you can just go back to the save point. The key thing that makes branches incredibly powerful is that you can make save points on one branch, and then switch to a different branch and make save points there, too.
The Primary (default) branch in git is the Master or the Main branch.
5. Staging Area / Staging Index / Index:
A file in the git directory that stores information about what will go into your next commit. You can think of the staging area as a prep table where git will take the next commit. Files on the staging index are poised to be added to the repository.
Some of the basic but important commands of git:
1. git init:
Running this command will create a new git repository from scratch. It is also used to initialize the existing project folder as a git repository.
Example: git init
2. git add <file>:
This command will add the file or files that u specify to the staging area or the index. You can run this command as many times as you need. But, Remember before commiting the files it will keep in the staging area. Instead of listing all file names one by one, you can use "." after "git add" to select all files under the current directory.
Example: git add filename or git add .
3. git status:
If you want to see what you have in the Staging Area and ready for commit you can run the command. It will display paths of your tracked and untracked files.So, basically it will display the difference between "git add" and "git commit" command.But, Remember it will not show any commit records or information.
Example: git status
4. git commit:
This command will take everything that is in the index for the Staging area and put it into the local repository.The "-m" option, which stands for "message". Writing good commit messages is a skill.
Example: git commit -m "Add files"
5. git push:
When you run this command it will take your local repository that you have created and then push it to the remote repository such as Github.
Example: git push <remote> <branch>
6. git pull:
If you want to pull the latest changes from the remote repository to the local repository you can do it using git pull command. This command is mostly useful when you are working in a Team or an Organisation.
Example: git pull <remote>
7. git clone:
The git clone command download the existing remote repository on your local computer by specifying the URL of the remote repository.Basically, If you find a program or project that you like on GitHub you can clone it and that will download it in your machine.
Example: git clone https://github.com/userName/repositoryName.git
If you want to write good commit messages you can use this to start your message:
- feat - a new feature
- fix - a bug fix
- docs - changes in documentation
- style - everything related to styling
- refactor - code changes that neither fixes a bug or adds a feature
- test - everything related to testing
- chore - updating build tasks, package manager configs, etc
Let me know what you think about this article, and Git in general. I hope this guide will help you with your learning, and hopefully save your time!