Mastering Git Basic To Advance

Jitendra Patwa
7 min readMay 16, 2020

Originally created by Linus Torvalds in 2005, licensed GNU General Public License written in C, Perl, Tcl, Python, is open source version control system (VCS). Its distributed tool, every developer has the full access of code repository history. It subsequently operates in files commits, diff, merge, log, rewriting repository history, maintain excellent workflow and tool to collaborate others code.

To use git initially required a git command tool like git bash and to integrate with cloud require git hosting tools like, GitHub, BitBucket, GitLab, etc. In industrial agile process, git is most recommended tool to maintain application repository history for codebase.

How git is work,

  1. In simple project/application initialize a git using in git bash/command prompt or in terminal by command,

git init

In above command, it creates some git control files and folders in local application hierarchy like “.git” where all tracking of git commands is kept place, “.gitignore” maintain to use include or exclude files or folder to track in git remote origin. Default its create “master ” branch in local repository.

2. Create a repository in git hosting tool for example using bitbucket and connect application to the remote origin using following command,

git remote add origin https://<username>@bitbucket.org/<workspace>/<appname>.git

In above command, following elements need to be consider,

“origin” is the connector to make push and pull over local and remote changes with given git link.

“username” is the account name or username of bitbucket git hosting tool.

“workspace” is the bitbucket workspace name where other application with its origin placed.

“appname” identifies the application name for source codebase.

3. Make changes in application files and add to git with commit name, for first time when origin adds require to add all files by using,

git add .

after that specific file can commit even with all files,

git add myfile.txt

then commit with your message,

git commit -m “UPDATE: myfirstcommitmessage”

4. Push your code into remote using command,

git push -u origin master

5. If some files change in bitbucket hosting tool or some other developer has pushed some code changes to remote, use pull to keep code sync,

git pull origin master

And make changes again push the same to the origin with new revision history by commit message.

6. To see current branch log history

git log

7. To see the what changes have made over commit

git show <commitid>

where commit id is reference of last push commit to origin like, b484f744fa394fdc2fcd2afb255d3af6ceaac808

8. To see difference between codebase history the git diff used between two different commits

git diff <commiid1> <commitid2>

Since its some of the few commands, describing more to understand about git in agile process need to go through with below following aspects.

  1. Version Control
  2. Project Deployment Environment Setup
  3. Readme.md/Changelog.md
  4. Branching Strategy
  5. Tagging Strategy

Version Control,

Nowadays, every electronic document must maintain there version control whether its is in word document, presentation, technical applications, etc. Basically maintain version control is also know by semantic versioning to keep document roll-out with its descriptive versions. In General, Git version control is very much important for technical perspective. It records a changes to file or set of files to recall specific version later.

For example, 1.2.3 uses synonyms as application release of 1st release of 2nd minor and 3rd patch fixes. Version Control has few key concept to maintain application versioning.

1 Major: Where incompatible API get changes or breaking change in application

2 Minor: Where functionality added in backward compatible manner

3 Patch: When backward compatible bug fixes

Also its has segments like release, premajor, preminor, prepatch. But most commonly used for versioning is major.minor.patch(example 1.2.3).

An git version control system is also acronym by revision control management or source control management.

Project Deployment Environment Setup,

After knowing version control, need to understand the application should get setup for the deployment environment for CICD pipeline. Developers should aware all the environment to handle it in application hierarchy in own codebase. Before application is ready to serve there consumers, application must have compatible features maintaining codebase using branches and also to maintain environment specific codebase. For example codebase must handle to switch environment at any time or have distributed git branches to switch environment like, dev, stage, and prod.

Readme.md/Changelog.md,

While using git need to go through this two also.

Readme: Readme is actually not a project documentation but its identity to describe the application hierarchy in git hosting tool or locally. In general, its a identification to face your application to real world to understand what the application is about. Readme is simple text file which is graphically represented like HTML. Its has several syntax to write it. It generally uses extension like, “.md” known as Markdown Documentation File.

In git project readme is default give option to whether we want to add or later in application hierarchy at time when git remote is created. The good readme file contain basics details for each key points below,

Name

Description

Installation & Deployment Guidelines

Test Cases

Releases

Contributors

Tags

License

Project Status

Changelog: Changelog is another descriptive file to know other for tracking of release and update held in application. Its written when branches made build and deployed to environments with its version control and tag them with release. A good changelog contains in file,

Git tag release version

Update history

Features & Improvements

Branching Strategy,

Its a most advance strategy in git to maintain codebase distributed with its branch environments. Branching can be done in following aspects,

master: default created by origin

feature/branchname: when something related to new feature adding in application

release/branchname: when application moved to specific environment like prod environment

bugfix/branchname: when some defects raised from release branch or feature branches

hotfix/branchname: when production release branch arise some defects hotfix branch generally used to create and modify the codebase.

In general there also we add some dev, qa, stage, prod branch also to maintain codebase for CICD pipeline.

Some of the basic branch specific commands,

1 To create branch

git branch -b <branchname>

2 To delete local branch

git branch -d feature/branchname

3 To delete remote branch

git push origin — delete feature/branchname

4 To checkout to another branch

git checkout <anotherbranchname>

5 To copy the same branch codebase into new branch

git checkout -b <newbranchname>

6 To merge codebase from anotherbranch

git merge <anotherbranchname>

7 To merge specific commit code from another branch

git cherry-pick <anotherbranchcommitid>

8 List out branches with local and remote

git branch -a

Tagging Strategy,

Git tagging strategy is another advance feature in git to maintain codebase safe in git database forever. Basically, it creates a snapshot of your deployed or local branch specific codebase encapsulated by version control with some descriptive tool and specified by tag with tagname.

Tagging denotes basically the releases of your codebase. To create a git tag tagging uses two types:

1 Annotated Tags: Here metadata of the creator which contain tagger name, date, commit message of tag.

2 Lightweight Tags: Basically both tags are used to make tagging, but in lightweight tag is not annotated, it stores last commit checksum in git file, no other information is kept while tagging.

The purpose using this in such scenarios like when want to make version control history of application releases or when project get downgrade and there is no specific release branch in your branch list. In such scenario git tagging will play roles and roll-out to the branch.

Some of the basic tagging specific commands,

1 To create annotated tag

git tag -a v1.2.1 -m “My release version v1.2.1 of my app”

2 To create lightweight tag

git tag v1.2.1

3 To push tag into remote origin

git push origin v1.2.1

4 To push all tags

git push origin — tags

5 To delete local tag

git tag -d v1.2.1

6 To delete from remote

git push origin -d v1.2.1

7 To list out available tagging list in application hierarchy

git tag -l

8 To show specific tag detail

git show v1.2.1

Thanks Git.

--

--