Git A Beginner guide

Gumparthy Pavan Kumar
7 min readOct 29, 2023

--

GIT an open-source VCS

Disclaimer: This is a beginner guide for GIT inspired from many sources with intent to only bring all the gathered information at one place to help beginners comfortable with the commands that are used almost every day in a life of software development.

GIT evolution:

Prior to GIT, there was a distributed revision control tool termed BitKeeper originally developed by BitMover Inc.

BitMover is an organization which used to provide access to the system for certain open-source or free-software projects, one of which was the source code of Linux kernel. In the year 2002, a decision to use BitKeeper as a Linux kernel development was taken, sooner in 2005 the BitMover announced that it would stop providing a BitKeeper version on free of charge to community.

This led to the origin of GIT with the intent of becoming Linux Kernel source code management, that is completely distributed and open-source. Which was eventually adopted by Linux developers.

Linus Benedict Torvalds was the creator of GIT.

Sources: wiki

Table of Contents:

  1. What is VCS?
  2. What is GIT ?
  3. What is GIT Bash?
  4. Work-Flow Diagram.
  5. Terminology(Lingo)
  6. Should know Commands
  7. What is GitHub?
  8. References

VCS(Version Control System):

VCS is a system that records changes to a file or set of files over time so that you could recall the specific version later. VCS could be categorized into three types:

  1. Local VCS

In this version, authors maintain a copy of their working directory in their local system with some version hierarchy(every release in one sub-directory like v1, v2), the owners would manually track the differences between original and copy version and keep them in sync regularly/ periodically.

This approach being simple but comes with it’s own challenges like error prone, Collaboration and Single Point of failure

2. Centralized VCS

These systems have a single server that contains all the versioned files, and no. of clients that checkout files from central place.

It offers a couple of advantages over Local VCS including Access Management, Tip point of others contributors in team and administration.

The system still has one disadvantage which is Single Point Of Failure

Ex: SVN

3. Distributed VCS

Clients checkout the full mirror of the repository, including its full history. Thus if any server dies and these systems were collaborating via that server, any of the client repositories can be copied back up to the server to restore it. Every clone is a complete backup of data.

Distributed VCS

What is GIT?

GIT is a free open source distributed version control system and has emphasis on speed and performance.

What is Git Bash?

It is a CLI for maintaining and managing git project through command line.

Download here

Work-Flow Diagram:

GIT workflow

Terminology(LINGO):

  1. Repository: In simple terms, it is a container holding the history of work, git uses information stored here.
  2. Branches: They are pointer to snapshot of changes
  3. .gitignore: It is a file that instructs the repository to exclude directory, files or patterns in working directory. A come use-case could be to avoid env files.
  4. Untracked files(??): Newly created files are untracked files. These are files that have been never added to working directory which are not in .gitignore.
  5. Added Files(A): All files not in .gitignore and that have been added tracked.
  6. Modified Files: All files not in .gitignore and that have been added previously and have been modified since.
  7. Working Tree: This notes the untracked files in working directory
  8. Staging Area: It notes the added files
  9. SHA1: Secure Hashing Algorithm
  10. HEAD: It is a reference to the last checkpoint of current branch

It is a 40 digit hexadecimal number.
Every commit is associated with a unique SHA1
The first 7–8 characters referred to as short commit, which is well sufficient to identify a version

Enough of Theory 😬 let’s jump on to exercise.

Commands:

  • Config: A file that contains number of variables which affect GIT command behavior's. For Windows the file available in $HOME directory ~/.gitconfig
  • Syntax:
git config <options/flags>
  • The obvious first thing that have to done after installation is Identity Setup, this is important because every git commit uses this information
git config --global user.name "<name😀>"
git config --global user.email "<email 😀>"

git config --list # check for the list of configured properties

Note that any flag attached with — global option will enforce all the local repositories to use same configuration unless overridden. Check this for complete options.

  • clone: A command to copy entire remote changes into local system
git clone <repo_url> # NOTE: depending on whether you use https or ssh protocol this command authorizes
  • init: A command that initializes the .git hidden directory within the $PWD. Without this none of the below commands work.
git init <folder_name> # creates a new folder with .git directory inside it
  • add: A command that adds the modified/ untracked files to staging area
git add <flags>
git add <file_name.ext> # add a specific file
git add <folder_name> # add a specific directory
git add -A # add all files
git add . # add all the changed files
git add -p <file_name> # -p stands for patch allowing to choose specific portion of files instead of entire files.
git add -n . # Dispays files that will be added, but will not add them
  • status: A command which can help display the current status of working directory. This command shows the untracked/ modified files in working directory or files added in the staging area.
git status

# -s is for short that only logs file name with a simple LINGO documented above complete list: https://git-scm.com/docs/git-status
git status -s

Example:
M README.md # indicates a modified file in working directory
  • commit: A checkpoint indicating GIT to track changes upto the current point. A commit is only carried if there are files in staging area. Every commit has a unique SHA1 associated with it. Every commit must have a message.
git commit <flags>
git commit # Just using this command will open a editor for writing message
git commit -m "<message💬>" # An alternative to step-1 without using commit editor
git commit -m "<message💬>" -m "description🗨️" # Alternatively a long description can also be added

# A usefull shortcut to do add and commit in one step:
git commit -am "<message 🗨️>"

# Modify the recent commit message
git commit --ammend -m "<message 🗨️>"

# A cool trick for devops, It is sometimes needed for devops team to check the triggerring integration for CI/CD without making any changes to source code, in such cases following command can help
git commit --allow-empty -m "message 🗨️"
  • push: This command is useful to transfer local files to remote system. To setup remote use git remote
git push origin <branch_name> # If you would like to avoid giving this branch_name every time use --upstream option at the beginning, every following commit within same branch can then be issued with simply push command
# Like
git push -u origin <branch_name>
# Then
git push # Ensure there are commits in local repo
  • fetch: Brings the commits from remote repository that does not exist in local repository. It does not merge.
git fetch origin <name>
  • pull: Brings the commits that does not exist in local working directory directly from remote repository and merge the branch with bought in commits. Unlike fetch this merged the local changes with bought in commits in current branch.
# Make sure there are no changes in working area
git pull origin <name>
  • log: A command that records the running log of commits. It is used to list all the commits made in the current branch that are reachable from HEAD.
git log <flags>
git log # List all the commits in current branch
git log --oneline # Prettify the output of log to only a message and short SHA
git log -<n> # Replace n with a number, this limits the no of results to be dispalyed
git log --graph # Visualize the structure
git log --pretty="format: %C(magenta) %an %Creset %s" # Prettify the results with author name and message title

# For a complete list of pretty option check: https://git-scm.com/docs/pretty-formats
  • merge: A command to combine the changes between different branches and resolve conflicts if any.
git checkout <destination_branch> # make sure you are in the branch where you want to bring changes
# ensure the branch is up to date with remote
git merge <source_branch> # replace the source branch with name where you want to bring changes from

# NO CONFLICTS
# Above command will auto issue a commit that is called merge commit
git push

# CONFLICTS
# RESOLVE
git commit -am "<message 🗨️>"
git push
  • checkout: A command that operates upon three distinct entities: files, commits and branches. This lets you navigate between branches, commits.
git checkout <branch_name> # switch between branches
git checkout <commit> # switch to a specific commit in current branch
git checkout -b <branch_name> # create a new branch with all the changes from existing branch and switch
  • remote: A command to play with remote connection
git remote -v # check the currently configured remote for a repository
git remote add origin <origin_name> # configure a origin
  • diff: A command to identify differences between two commits, two files
git diff commit1..commit2
git diff absolute_path_file1 absolute_path_file1 # check difference between file in local working copy an dstaging area

What is GitHub?

GitHub is a hosting service for Git repositories. GIT AND GITHUB ARE NOT SAME.

Other services like GitHub are Bitbucket, GitLab etc.

Thanks for reading…

References:

--

--

Gumparthy Pavan Kumar

Hello ✋this is pavan a technology agnostic, with over 4+ years of experience in fintech space. I architect and build software solutions at scale.