All About GIT:

As promised I am here with my next blog! So, let us begin our learning without wasting even a single second. πŸ™‚

Working in the IT Industry, we might be very familiar with the term ‘GIT’ but what exactly is a GIT Repository??

So, here is the answer! GIT Repositories contains a collection of files of various different versions of the project.

  • A VCS or the Version Control System is used to create these versions and store them in a specific place termed as REPOSITORY.
  • These files are imported from the repository into the local server of the user for further updations and modifications in the content of the file.
  • The process of copying the content from an existing GIT Repository with the help of Various GIT tools is termed as CLONING.
  • Once the cloning process is done, the user gets the complete repository on his/her local machine.

GitLab

  • Coming to the GitLab, it’s a service that provides remote access to GIT Repositories.
  • In addition to hosting the code, the services provide additional features designed to manage the SDLC(Software Development Lifecycle).
  • These additional features include:
  1. Managing the sharing of code between different people.
  2. Bug Tracking
  3. Wiki Space
  4. Other tools for ‘Social Coding’

PULL AND PUSH

  • PULL: When the remote repository changes, the local copy will be behind it. We can update it with the new changes in the remote repository. This is referred to as Pulling from GitLab.
  • PUSH: After saving a local copy of a repository and modifying its files on the local machine, the changes can be uploaded to the GitLab. This is referred to as Pushing to GitLab.

WHY BRANCHING??

  • Sometimes it happens with us that we want to add our code to a project but we are not sure enough whether it will work properly or not πŸ™
  • Or other cases can be, we are collaborating on the project with others and don’t want our code to get mixed up. In this type of situations, it’s a good idea to work on a different branch.
  • We are free to do whatever we want with the code in our branch without impacting the main branch or the other branches!
  • And when we would be ready to bring our changes to the main codebase, we can merge our branch into the main one used in the project(such as master).

Basic GIT Commands

To set the Author Name and Email Address:

 git config –global user.name β€œ<name>”

git config –global user.email β€œ<email address>”

To create a new repository:

 git init <repository name>

To obtain a repository from an existing URL:

git clone <url>

To download the latest changes( Pull):

git pull <REMOTE> <name-of-branch>

NOTE: When we clone a repository, <REMOTE> is usually ‘origin’ <name-of-branch> is usually master, but it may be any existing branch also.

It is important to every time take a pull before you start working on a project in order to work on an up-to-date copy of the project.

To View remote repositories:

git remote -v

-v shows URLs of remote repositories when listing the current remote repositories.

To Switch to a branch:

git checkout <name-of-branch>

To create a new branch and switching to it:

git checkout -b<name-of-branch>

To Switch to the master branch:

git checkout <branch name>

To check the changes you made to your files:

git status

Changes will be displayed in red colour.

To View the Differences:

To view the differences between your local & repository versions that you cloned or pulled,

git diff

To view the differences between the two branches:

git diff <First branch><Second branch>

To revert changes in all files:

git reset –hard

For discarding all history and going back to the specified commit:

git reset -hard<commit>

To Add & Commit Local Changes:

  • Any local changes will be displayed in Red Colour when ‘git status’ command would be used.
  • These changes can be new, modified or deleted files/folders.
  • Then we need to first add the files then commit it.

git add <file-name OR folder-name>

git commit -m “Message to describe the intention of the commit”

To add & Commit all the local changes quickly:

git add .

git commit -m “Message to describe”

Push

git push <remote><name-of-branch>

For Example: To push our local commits to the ‘master’ branch of the ‘origin’ remote, command would be: git push origin master

To Delete all changes in the branch:

git checkout .

To Undo the most recent commit:

git reset HEAD~1

To Merge a branch with Master Branch:

git checkout<name-of-branch>

git merge master

To add a file to the staging area:

git add<name-of-file>

To list the version history for the current branch:

git log

To list version history for a file including the renaming of files:

git log -follow<file>

To give tags to the specified commit:

git tag<commitID>

To list all the local branches in the current repository:

git branch

To create a new branch:

git branch <branch name>

To delete a branch:

git branch -d <branch name>

I hope this article seems helpful to you! Stay tuned for more interesting blogs πŸ™‚