What is Git?
Git is a Version Control System, which maintains different versions of your project. It is a platform on which, you can work on a single project in a team formation.
It is open source and free that is the reason, most companies use git. It is available for Windows, Mac and Linux too. Git is cross-platform.
What is GitHub?
GitHub is used to host repositories and version-controlling systems. GitHub is completely dependent on Git. While Git can survive without GitHub. There are other platforms too, to host your repositories. GitHub manages all operations, collaboration and forking of projects and repositories.
Git Architecture:
The image shows the basic structure of the file-transferring process. As shown in the image, from the local repository, when files move to the remote repository, they go through the staging area. While fetching files from the remote repository, it directly comes to your local repository.
Git Commands:
git --version:
You can check your current version of Git by running the git --version
command in a terminal (Linux, macOS) or command prompt (Windows).
git config --global user.name:
This command is used to set your username. it can run as git config --global user.name "XYZ".
Here, I configure the username as XYZ.
git config --global user.email:
This command is used to set your email-id. it can run as git config --global user.email "xyz@gmail.com".
Here, I configure a dummy email-id.
git init:
Initialize any project in GitHub, the git init command is used. After initializing it only, you can perform operations in the project.
git add:
The git add
command is used in two ways.
To add multiple things at a time, you can run git add .
.This command adds all local files in the remote repo.
To add a specific file also, you can use the git add command. It can be written as git add <filename>
This will add that specific file to the remote repository.
git status:
As the command suggests, it shows you the status of your working directory and staging area. It displays the changes, you have staged with the branch name. It also displays files, which are not tracked by Git.
git commit:
Commit simply means "To save".The git commit
command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to. Before the execution of git commit
, The git add
command is used to promote or 'stage' changes to the project that will be stored in a commit. These two commands git commit
and git add
are two of the most frequently used.
git commit -m "message here"
A shortcut command that immediately creates a commit with a passed commit message. By default, git commit
will open up the locally configured text editor, and prompt for a commit message to be entered. Passing the -m
option will forgo the text editor prompt in favor of an inline message.
git commit -am "message here"
A power user shortcut command that combines the -a
and -m
options. This combination immediately creates a commit of all the staged changes and takes an inline commit message.
git commit --amend
Passing this option will modify the last commit. Instead of creating a new commit, staged changes will be added to the previous commit. This command will open up the system's configured text editor and prompt to change the previously specified commit message.
git push:
The git push
command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It is generally done after the commit messages.
git diff:
Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on the disk.
git rm:
The git rm
command is used to remove files from a Git repository. It can be thought of as the inverse of the git add
command. The git rm
command can be used to remove individual files or a collection of files. The primary function of git rm
is to remove tracked files from the Git index. Additionally, git rm
can be used to remove files from both the staging index and the working directory. git rm <filename>
removes particular file. There are other commands are also available to remove files.
git fetch:
The git fetch
command download commit, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.
git merge:
The git merge
command lets you take the independent lines of development created by git branch
and integrate them into a single branch.
git pull:
The git pull
command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration workflows. The git pull
command is a combination of two other commands, git fetch
followed by git merge.
Git snapshot:
The most important priority of Git is data integrity. That is why, we are using Git. Git takes all your folders, files, authors' name and timestamps and applies them to an algorithm named SHA. Even if you, change '.', inside the file, the whole algorithm produces a different SHA. This thing is called Snapshot. Git snapshot has so much power that you can backtrack using SHA.
When you run git log
command, It displays the list of commit messages. With every commit, there is one SHA.
Git snapshot is generated using two things. The first part is from the previous commit and another part includes files, folders, author's name and timestamp.
Branch:
Your repository work as a tree. As a tree has branches, you can create branches in the repository also. Branching offers a way to work on a new feature without affecting the main codebase. To apply operations on a branch, there are different commands. Some commands are described below.
git branch <branch_name>
: This command creates a new branch in the repository.
git branch --list
: This command displays the list of branches of your repository.
git checkout <branch_name>
: To switch from one branch to another, this command is used.
git branch -b <branch_name>
: This command creates a branch and also shifts you to a new branch.
git branch -d <branch_name>
: This command delete the particular branch.
git merge <branch_name>
: This command merges the given branch with the existing branch.