Automation Crunch 😎
January 14, 2022

Getting started with Git

Posted on January 14, 2022  •  16 minutes  • 3298 words

This article contains my own notes about Git, including Git introduction, Git commands, Git basic internals, and Git FAQs, all of which can be quite valuable in your daily work.

1. Git

1.1 Introduction

Git is an Open Source Distributed Version Control System designed to manage the teamwork done on a project. Git helps the contributors to track the changes in files or projects and speed up the overall process.

It is designed for :

- Speed
- Simplicity
- Fully Distributed
- Excellent support for parallel development
- Integrity

1.2 Version Control System

Version Control System (VCS) is a software that helps software developers to work together and maintain a complete history of their work.

Functions of a VCS :

Types of VCS

1.2.1 Centralizted version conrol system (CVCS)

Centralized version control system (CVCS) uses a central server to store all files and enables team collaboration. But the major drawback of CVCS is its single point of failure, i.e., failure of the central server. image courtsey abc.com

1.2.2 Distributed version control system (DVCS)

Distributed VCS introduced significant improvement over the risks posed by Central VCS.These systems do not necessarily rely on a central server to store all the versions of a project’s files. Instead, every developer “clones” a copy of a repository and has the full history of the project on their own hard drive. This copy (or “clone”) has all of the metadata of the original. Test Image 1

1.3 Github Vs Git

Test Image 1

Github : It is web portal and cloud hosting service for your Git repositories.

Git : It is a Distributed Version Control System client for tracking versions of files.

1.4 Install

1.5 Git States

Test Image 1

Working area : Consider a project residing in your local system. This project may or may not be tracked by Git. In either case, this project directory is called your Working directory. Working directory is the directory containing .git folder. In this state, Git is just aware of the files in the project. It doesn’t track the files yet. To track the files, we’ve to commit these files by first adding the files to the staging area.

Note : git init – Command to initialize a Git repository

Staging area : Staging area is the playground where you group, add and organize the files to be committed to Git for tracking their versions. Committing process is done in the staging area on the files which are added to the Index after git add command is executed. This committing process is done by the use of git commit command. This command commits the staged changes to the local repository.

Note : git add – Command to add files to staging area.

Git repository : Git repository is the database where metadata about project file’s history will be tracked.Now that the files to be committed are grouped and ready in the staging area, we can commit these files. So, we commit this group of files along with a commit message explaining what is the commit about. Apart from commit message, this step also records the author and time of the commit. Now, a snapshot of the files in the commit is recorded by Git. The information related to this commit (names of files committed, date and time of commit, author of commit, commit message) is stored in the Git directory.

Note : git commit -m “your message” – Command to commit files to Git repository with message

Remote repository : It means mirror or clone of the local Git repository in Github. This will allow other collaborators to view the code. Thus, whatever changes you make in the local Git repository will be visible to other collaborators when you push your code to the remote repository. Command to push the code to remote repository in Github is git push.

Test Image 1

git push - Command to push commits from local Git repository to remote Git repository hosted in Github.

git pull - Pull command is used to fetch the commits from a remote repository and stores them in the remote branches.

1.6 Git Commands

1.6.1 Configuring git

1.6.1.1 git config

: git config –-global user.name "FIRSTNAME LASTNAME"
: git config –-global user.email "abc@example.com"
: git config --global --list

1.6.1.2 color highlighting

git config --global color.ui auto

1.6.1.3 Setting default editor

git config --global core.editor <editor>
# for example:
git config --global core.editor vim
git config --global core.editor emacs

One can use :

  • “–global” to set configs for global level
  • “–user” for user level
  • without any option (git config core.editor vim) for project level.

1.6.2 Create repositories

When starting out with a new repository, you only need to do it once either locally and adding remote url, then push to GitHub, or by cloning an existing repository.

git init : This command is used to start a new repository. git init [repository name] git remote add origin https://github.com/example/project.git

1.6.3 git clone

This command is used to obtain a repository from an existing URL.

git clone [url]

1.6.4 Make changes

1.6.4.1 git add

git add [file]  
git add *  

1.6.4.2 git commit

git commit -m "[Type the commit message]"
git commit -a  

1.6.4.3 git tag

git tag [commitID]

1.6.4.4 git log

This command is used to list the version history for the current branch.

git log  

[add alias: git config --global alias.lga "log --graph --oneline --all --decorate"]

git log --graph --oneline --all --decorate
git lga (if alias added)

1.6.4.5 git show

This command shows the metadata and content changes of the specified commit.

git show [commit]  

1.6.4.6 git diff

git diff  
git diff --staged 
git diff [first branch] [second branch]  

1.6.5 Redo commits

git reset [file]  
git reset [commit]  
git reset --hard [commit] 

1.6.6 Check status

git status  
git rm [file]  

1.6.7 Git Branches

Branching means diverging from the mainline and continue to work separately without messing with the mainline.

Test Image 1

git branch

git checkout :

1.6.8. Add Remote

A common repository on GitHub that all team member use to exchange their changes

1.6.9 Synchronise changes

1.6.9.1 git push

This command sends the committed changes of master branch to your remote repository.

git push [variable name] master  
git push [variable name] [branch]  
git push --all [variable name]  
git push [variable name] :[branch name]  

1.6.9.2 git pull

This command fetches and merges changes on the remote server to your working directory.

git pull [Repository Link]  

1.6.9.3 git fetch

The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.

git fetch <remote>
git fetch <remote> <branch>
git fetch --all
git fetch --dry-run

1.6.9.4 git merge

This command merges the specified branch’s history into the current branch.

git merge [branch name]  

1.6.9.5 git rebase

Rebase is another way to integrate changes from one branch to another. Rebase compresses all the changes into a single “patch.” Then it integrates the patch onto the target branch.Unlike merging, rebasing flattens the history because it transfers the completed work from one branch to another. In the process, unwanted history is eliminated.

git checkout -b feature_branch master
git commit -a -m "Adds new feature"
git rebase <base>
git rebase --interactive <base>
pick 2231360 some old commit
pick ee2adc2 Adds new feature
# Rebase 2cf755d..ee2adc2 onto 2cf755d (9 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

1.6.10 Git stash

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

1.7 Git ignore

The .gitignore file is a text file that tells Git which files or folders to ignore in a project. To create a local .gitignore file, create a text file and name it .gitignore (remember to include the DOT “.” at the beginning).

1.8 Git Fork

When you fork a repository, you create a copy of the original repository (upstream repository) but the repository remains on your GitHub account. A fork is a copy of a repository. Forking a repository allows to freely experiment with changes without affecting the original project.

1.9 Git Alias

Aliases are used to create shorter commands that map to longer commands. Aliases enable more efficient workflows by requiring fewer keystrokes to execute a command.

1.10 Git Hooks

Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally.

2. Frequently Asked Questions

2.1. What is a repository in Git?

2.2. Difference between clone and fork

2.3. Difference between rebase and merge

2.4. Difference between pull and fetch

2.5. What is a ‘conflict’ in git?

2.6. How to resolve a conflict in Git?

2.7. What is the difference between the ‘git diff’ and ‘git status’?

2.8. How will you know in Git if a branch has already been merged into master?

2.9. Explain the difference between reverting and resetting.

2.10. What is git cherry-pick?

Follow me

You can find me on