Hey Everyone!!! In this article, we are going to discuss about git and github, various commands and their uses. We will also discuss about how to create a git repository, push code, create branches and other various operations in github.
So. let's deep dive into it...
What is Git?
Git is a version control system. It helps us to keep track of the code changes. Different people can collaborate on the code at various intervals of time according to their convenience. It has many features like:-
- It can track the changes.
- We can even undo the changes made.
- We can create multiple copies of the project that can be handled separately.
- We can monitor other people's changes and merge them with a copy we want.
Installation of Git
We can download Git for free from the following Website .
After downloading the software, install it into the system. Once the installation is done, open command prompt
or terminal
in the system and check for the installed version of git by using the following command.
git --version
The output of the above command will be something like this:-
Git Configure
Once the git is installed, we need to tell the git who we are and set some information about us to git. For this we need to set username and email address. The following commands provided below helps us in setting username
and password
.
git config --global user.name "your_username"
git config --global user.email "your_email_address"
NOTE:-
If we want to set the username/e-mail for just the current repository, we can remove global.
We can even cross verify by checking whether the username and email address is set or not by using the following commands respectively.
git config --global user.name
git config --global user.email
Git Initialization
First, navigate to the folder where we would like to use for git. Once we have navigated to the current folder, we can initialize git on that folder.
git init
Git Commands to use
Git add command
It is used to add one or more files to staging area.
Syntax
To add one file
git add <filename>
To add all files
git add .
Git status command
It is used to tell the current state of the repository. If the files are in the staging area, but not committed, it will be shown by the git status. Also, if there are no changes, it will show the message no changes to commit, working directory clean.
Syntax
git status
Git commit command
It makes sure that the changes are saved to the local repository.
Syntax
git commit -m "<your message>"
It helps us to tell the exact information that what has been done.
Git branch command
It is used to determine what branch the local repository is on.
Syntax
To create a new branch
git branch <branch_name>
To list all the remote or local branches
git branch -a
To delete a particular branch
git branch -d <branch_name>
Git checkout command
It is used to switch branches, whenever the work is to be started on a different branch.
Syntax
To checkout an existing branch
git checkout <branch_name>
To checkout a new branch
git checkout -b <branch_name>
Git merge command
It is used to integrate the branches together. The command combines the changes from one branch to another branch.
Syntax
git merge <branch_name>
Git push command
It is used to transfer the commits or pushing the content from the local repository to the remote repository.
Syntax
git push -u origin master
What is Github?
GitHub is a code hosting platform for collaboration and version control. GitHub lets you work together on projects. To avoid any work loss or other issues, we use github collaboratively so that the changes are being tracked at any time and can't be lost.
To get started with github, first we need to create an account on the official website .
Once the account is created on github, we can create a new repository by providing a repository name and leaving all other options to default.
Our repository will be created and we will be able to work in it.
The GitHub repository we have created is empty, we have to upload our project into it. To do this we have to connect our remote repo(called origin) with our local repo.
git remote add origin git@github.com:khushichauhan425/FSJS-2.0.git
We can check if the remote is added successfully or not use the following command:-
git remote -v
Now, we can push our code which will send the local commits to the remote branch of the repository by using the following command:-
git push origin main
Now, once we go to our remote repository and refresh the page we will see that the code is uploaded on GitHub.
That's all we need to have for the basic knowledge about git and github. If you liked this article, please like, comment and give your valuable feedback for further improvement.
Happy Learning!!!