GIT (Source Code Management)

This is a preview lesson
Register or sign in to take this lesson.

Introduction to GIT

Git is a version control software meant for tracking the changes in files. This helps to coordinate among the developers or programmers who are working together to develop source code for product development. Its purpose is to make distributed and non-linear workflow smoother, fast and reliable. Git is a free and open-source project used for version control as well as other applications including content management, configuration management, etc.

With Git, you can push and pull changes from other Git repositories. Repositories could be anywhere, your own file system, that of friends or team members, or even a distant server. So, you can make changes in your own repository, fetch changes committed by other team members, and consequently, merge the data as many times as is needed. This means that you can work in an isolated environment, but also synchronize the data easily with the rest of your team members. Push and Pull/Fetch features are exceptions to all the other operations on Git that are carried out locally. There are several platforms like GitHub, GitLab, Bitbucket, etc which use Git.

Installing GIT

Git can be installed on most operating systems. Mac and some Linux systems come with pre-installed Git. So it’s better to check before installing Git whether it is pre-installed or not. For that you can run the following in the shell.

Terminal (Linux/Mac) OR Git Bash or Command Prompt (Windows)::

$ git version

If Git is not installed, you can install Git through an installer, through a package, or by downloading source code and compiling it on your own. Installing GitHub Desktop also installs Git within your system with GUI.

Linux:

On the Linux system install git with the preferred package manager:

# Ubuntu/Debian
sudo apt install git

# Fedora
yum install git
dnf install git

# Arch Linux
pacman -S git

Mac:

# With Homebrew (install homebrew if not)
$ brew install git

# with MacPorts (install MacPorts if not)
$ sudo port install git

Git Set Up

After installing Git first thing you can do is set up your user name and password. And every commit you create will use this information. Git is enabled with a tool called “git config” which allows you to control configuration variables. You can set up with “—global” option to do it only once.

$ git config --global user.name "your name"
$ git config --global user.email "your email"

# Also you can configure other configurations as well
$ git config init.defaultBranch main

To check all the settings:

$ git config --list 

Repositories and Branches:

Repositories are collections of files that contain different versions of the same project. A common repository that all the members of a team make use of in order to exchange the changes they made to the project is called remote.

A branch is a copy of codeine, which is managed by the VCS. Branches allow for parallel work

along with stable, tried, and tested code. It represents an independent line of development.

GitHub/GitLab/Bitbucket:

GitHub or GitLab or Bitbucket is a Hosting provider for software development as well as version control using Git. It can be used as source code management as well. The source code repository of your product can be kept online by creating repositories on GitHub or similar platforms.

Create a new repository:

  • Visit github.com (sign up if you haven’t already) and log in.
  • Create a new repository by clicking “Create Repository”. You can initialize README.md file which contains the description of your project.

You have successfully created a new repository, now you can continue working with your repository by adding the repo URL to the existing local directory or by cloning the created repository to your local machine and start making changes.

Add remote URL (just created on GitHub) to your existing local project directory:

git remote add origin git@github.com:<username>/your_repo_name.git
git branch -M main
git push -u origin main

Add a new repository to GitHub from the command line:

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:<username>/<your_repo_name>.git
git push -u origin main

And you have successfully created a new repository on GitHub. A similar process applies to other platforms like GitLab, Bitbucket, etc.

Top Most Common Git Commands:

You can use git —help to get available commands in the terminal or command prompt.

  • git clone

This is used for downloading or copying remote projects to the local machine.

$ git clone <repository_url>
  • git init

Starts a new empty repository or reinitializes an existing one in the project root directory. Creates .git file.

$ git init <repository_name> # OR git init
  • git fetch

Gets all updates from the remote repository including branches too.

$ git fetch
  • git pull

It fetches all the changes from the remote repository and merges them into the current local branch.

$ git pull <remote_name> <branch_name>
  • git branch

Lists all branches of the repository or you can create a new branch without checking it out or deleting the existing branch.

$ git branch

# create new branch
$ git branch <new_branch_name>

# Delete branch
$ git branch -d <branch_name>
  • git checkout

This is used to switch the branch.

$ git checkout <branch_name>

# If you want to create a new branch and switch to it
$ git checkout -b <branch_name>
  • git add

This command is used to stage the changed files. It can be done individually or all files at once.

# Individually
$ git add <file1_path> <file2_path>

# At once
$ git add .
  • git commit

Used to save all changes you made. It’s like a snapshot of the current state of the branch.

$ git commit -m "your commit message"
  • git push

It is used to push the locally committed changes to the remote branch.

$ git push  # if already tracked branch

$ git push --set-upstream <remote_name> <branch_name>
  • git diff

It can be used to see the unstaged or staged changes in the current branch. And also you can compare two branches of local or remote.

# staged changes
$ git diff

# Unstaged changes# staged changes
$ git diff

# Unstaged changes
$ git diff --staged

# compare two branches
$ git diff <branch1> <branch2>

# compare local and remote branches
$ git diff <remote_name>/<branch_name> <branch_name>
$ git diff --staged

# compare two branches
$ git diff <branch1> <branch2>

# compare local and remote branches
$ git diff <remote_name>/<branch_name> <branch_name>