Popular GIT commands

Some of the popular GIT commands and their usage:

1. git init
2. git clone
3. git branch
4. git checkout
5. git add
6. git commit
7. git push
8. git pull
9. git merge
10. git status

Here we will explain only few, as others are explained in earlier pages:

git clone:
To make a local copy of an existing Git repository, use the git clone command. You can use it to retrieve all of the repository's files, branches, commit history, and other metadata. When you want to start working on a project or collaborate with others on an existing repository, this is really helpful.

The git clone command's fundamental syntax is as follows:
git clone <repository_url> [<destination_directory>]

git clone https://github.com/arjun2010-ece/sample_repo.git 

Repository URL: The represents the location of the remote repository you want to clone. This can be a URL starting with https://, git://, or ssh://. It can point to a remote server, a repository hosting service like GitHub, GitLab, or Bitbucket, or even a local directory if you're cloning from another location on your machine.

Destination Directory: The optional parameter specifies the directory where you want to place the cloned repository. If not provided, Git will create a new directory with the same name as the remote repository and place the cloned files inside it. If you specify a different directory name, Git will create that directory and clone the repository into it.

git branch:
This command displays all the local git branch (please observe only local branch in your system not of remote or any other developers local branches). It is written like this::
git branch


git checkout:
This command is of 2 types and has 2 different purposes. It is written like this::

// First way: create and move to a new branch
git checkout -b feature/creating_navbar

//Second way: Move to a new branch
git checkout main


The first way of writing command is like this git checkout -b, here -b means create a new branch from current branch (mostly develop/main) with the given name of branch i.e. here feature/creating_navbar and then change this as the current branch, where you could write your code.

The second way is simply switching to any branch from any other branch.


git pull:
The purpose of this command is to pull latest commit or changes from any remote branch to any local branch.
If we are on a branch feature/creating_navbar currently and we fire git pull origin main, then we are pulling latest commits from main branch and thus refreshes our branch thus trying to avoid any conflict. And if any comes then we fix that locally.

Just remember that before doing git push command then this command is mandatory for every developer.

Just remember that this command is equivalent to git fetch + git merge.

git merge:
If we are on branch A locally and then want to merge branch B into branch A then simply run this command like::

git merge branchB // it will merge commits of branchB into branchA



git fetch:
The purpose of git fetch is to bring all the remote branches into our local system. Then we can fire the command "git checkout feature/creating_navbar" to switch to that branch (which was not there locally before) and can run that locally or read the code too.

git fetch // bring all the branches from remote
git checkout feature/creating_navbar // switch to that remote branch


git status:
The purpose of this command is to show the status on the current branch. If after firing, the terminal shows files in red color means we have not fired "git add ." command yet. If it shows files in green color means we have not fired "git commit -m" command yet. It is extrememly useful command.

git status