What is Git?

The most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments).

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer’s working copy of the code is also a repository that can contain the full history of all changes.

In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

Installing Git on Linux

If you want to install the basic Git tools on Linux via a binary installer, you can generally do so through the package management tool that comes with your distribution. If you’re on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

$ sudo dnf install git-all

If you’re on a Debian-based distribution, such as Ubuntu, try apt:

$ sudo apt install git-all

For more options, there are instructions for installing on several different Unix distributions on the Git website, at https://git-scm.com/download/linux.

Some basic commands for Git

Clone a Repository

Cloning is getting a copy of a repository onto your local machine is called cloning. Copy the ssh URL from https://github.com/<your github account>/myproject, then:

git clone git@github.com:<your github account>/myproject.git

Check Git Status

Change into the newly-created myproject directory, where you’ll find all the source code of the your project.

Now you’re in the working directory, the set of files that you currently have in front of you, available to edit. Use below command to know its git status:

$ git status
# On branch master
nothing to commit (working directory clean)

This command will guide to what to do next to update code on git.

Create a New Branch

To create new branch based based upon whatever branch you are on, use below command. You will automatically switch to newly created branch once created.

$ git checkout -b new-branch-name
Switched to a new branch 'new-branch-name'

git checkout is a command you’ll use a lot, to switch between branches. The -b flag tells it to create a new branch at the same time.

Use below command if want to create branch on based on another branch(Other than current branch).

git checkout -b new-branch-name existing-branch-name

This will create a new branch new-branch-name, based on existing-branch-name.

Stage Changes

run git add on the file again to stage the newer changes

Commit Your Changes

After adding all required changes on staged area, commit the code with below command.

git commit -m "Commit message…"

Every commit needs a commit message, The -m is for the commit message on that commit.

How to undo git commit locally if you accidentally commit some code?

Push Changes to Github

Your code will be still in local environment until to do not push it. If you were pushing changes from master locally to master on GitHub, you could just issue the command git push and let Git work out what needs to go where. It is when you want to push on same branch where you are residing.

To push on another branch you want, run below command

$ git push origin my-branch-name
Counting objects: 34, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (28/28), 6.87 KiB, done.
Total 28 (delta 13), reused 12 (delta 7)
To git@github.com:evildmp/myproject.git
 * [new branch]      my-branch-name -> my-branch-name

Git pull

Multiple users are updating and committing the same code on git. When you want to merge changes from git into your GitHub fork as well as your local clone to keep your code up to date, You will user git pull.

Note that here instead of git fetch followed by git merge, you could have run git pull. The pull operation does two things: it fetches updates from your GitHub fork (origin), and merges them.

However, be warned that occasionally git pull won’t always work in the way you expect, and doing things the explicit way helps make what you are doing clearer

git fetch followed by git merge is generally the safer option.

Git Pull Request

Pull request is created when you committed a change in the GitHub project, and you want it to be reviewed by other members. You can commit the changes into a new branch or an existing branch.

Once you’ve created a pull request, you can push commits from your branch to add them to your existing pull request.

Pull request allows you to announce a change made by you in the branch. Once a pull request is opened, you are allowed to converse and review the changes made by others. It allows reviewing commits before merging into the main branch.

To create a pull request, you need to create a file and commit it as a new branch. As we mentioned earlier in this topic, how to commit a file to use git pull. Select the option “create a new branch for this commit and start a pull request” from the bottom of the page. Give the name of the new branch. Select the option to propose a new file at the bottom of the page. Select the required options and give name to the file. Select the option to propose a new file. It will open a new page. Select the option create pull request.

Leave a Reply

Your email address will not be published. Required fields are marked *