Git basic commands and functioning

Installing git in your machine

yum install git ( for rpm based system)

or

apt-get install git ( for deb based system)

local repository

This is a git repository you are creating on your local machine

git init command will turn a normal directory into an empty git repository

git init

version check


[root@ip-172-31-43-215 ~]# git --version

git version 2.14.4


How to make a local repository

------> create your repo directory

mkdir git

cd git

----> make the folder into an empty local repo

git init

you can see the result

Initialized empty Git repository in /home/git/.git/

===

---> Add or create your files in this local repo, here I am creating some text files in this directory

git add

This command will add all files into the staging area, whenever we commit, git will look for the files which are present in the staging area.

git add file_name ====> it will add the file into the staging area

To add whole files in this directory to the staging area

git add .

git commit

It will make effect all changes happend in the staging area to the local repo

Always leave a commit message while commiting, it will help us to detect what was changed.

git commit -m "my first commit"

git status

This command will shows the current status of git repository

git config

By this command, we can configure settings like username and email address

git config --global user.name "nithin"

git config user.email "xyz@gmail.com"

Branching

By default, git has a master branch, and if your developer wants to work on a new configuration and he can work on new git branch and once all completed, he can merge the current branch to the master branch


To list branches : git branch

To integrate 2 git branches together : git merge branch_name

Once we merge 2 branches, all contents from one branch to be merged to the other

To switching branches : git checkout branch_name ( To enter into a new branch)

To create branch : git branch branch_name

To delete a branch : git branch -d branch_name

To create a branch and checkout into the same in one command : git checkout -b branch_name


Remote repository commands

Anyone can create a git account in github and can create repos freely or private repos by paying a certain amount

git remote add origin git_URL

Git url can be obtained from clone or ssh options in git hub account

So by adding origin, we have connected our local repo to the remote repository

HOW TO CLONE A REMOTE REPOSITORY

git clone ssh_link_from_github

This will clone all the files and history of the remote repo in github to our local repo

pull command

If you want to reflect all changes that you made in your github remote repo to your local machine(local repo)

Use git pull command

git pull origin master

push command

git push origin master

It will clone all changes that we have done in our local repository to the remote repo.

git log

This will show the history of commit and changes of the repository

git revert

If you want to revert to any previous commit, use the git revert command.

"git log" command will show the commit id and

git revert commit-id

will revert to that commit.

=================================================================================

Top News