Basics of using Git

In this article, we’ll cover the basics of using git, how version control systems work, and how to push and pull code.

What is version control?

Version control is a system that keeps track of changes made to your codebase over time. It is like keeping a digital copy of your files somewhere on the internet where other people can make changes to those files. It allows developers to collaborate effectively, with the freedom of knowing every change to the repository can be easily reverted.

Why use version control?

Version control comes with a lot of benefits that make it easier for projects to exist long term:-

  1. A history of all changes made to a codebase are maintained making it easy to manage different versions of the same software.
  2. It can easily identify incompatible changes made to a codebase and help you rectify before causing any permanent damage.
  3. It helps distributed teams to coordinate their work in fast paced software delivery workflows.

Types of Version Control Systems

  1. Centralized Version Control
    There is one centralized repository which hosts different versions of the code. Every User commits directly to the main branch. A developer can lock a file that contains a piece of code they are working on to avoid conflict with other developers. This system is more suitable for small teams working with huge binary files. Instead of downloading the entire codebase, they can just download a subset of what they need. The downside to this system is it creates a single point of failure such that, when the repository server is down no one can access their codebase. Examples of centralized version control tools include perforce and subversion.

  2. Localized Version Control
    This system operates entirely on one server and does not require any remote connection. This is more ideal for personal projects or single user contributions. A structured revision of history is still maintained to enable rollbacks after accidental file modifications that are causing errors. The most widely used local version control tool is known as revision version control. It stores the differences between file versions known as patch sets and allows you to recreate any file versions by applying patches.

  3. Distributed version control
    Every contributor download a local copy of the repository which fully mirrors everything in the central repository. This allows developers to work offline and later synchronize changes. This system helps development teams to set up strong workflows. developers can easily sync their code and maintainers can set up code review processes to maintain code quality. Examples of tools include Mercurial, Bazaar and Git.

The Journey From Git Init

This section covers the basics of using git to work with a repository. It covers:

  • How to create a repository
  • How to push code to a repository
  • How to pull a repository version
  • How to track changes made to a repository
    We will work solely with the command line to achieve this.

Prerequisites

  1. Have git bash installed
  2. Setup ssh key for GitHub

Follow these next steps to successfully track your project with git

  1. Initialize your repository
    The first step is to navigate to the project directory you are working on as
cd path/to/project/myproject 

In the root directory of this project is where you want to initialize git.

git init

This command creates a new subdirectory .git which contains all repository files.

To start tracking your files with git use the git add command. This command tells git which specific files you want to track

git add . // track all files
git add filename //track only specified filename
git add *.py // track all files with the .py extension e.g file1.py, file2.py

Run git status to see which specific files are being tracked.

# Crafting a commit message

The next step is to run git commit which takes a snapshot of the current file version that you are tracking. The default git commit message will contain the latest output of git status.
To override the message with a custom one send

git commit -m "This is my first commit"

. A good commit message is useful for future reference and for other developers using the project. There are a few commit message conventions to follow for uniformity and readability. These include capitalizing the first letter of the message and using flags such as fix, refactor, style to categorize a commit.
The message also needs to be descriptive of the changes you made. Read more about this on freecodecamp article

git commit -m "docs: Added instructions on how to..."

Run git log to view commit history

Working with remote repositories

You need remote repositories to push and pull data from them when sharing your work. Its possible to add remote repositories, remove invalid ones. To add a remote repository run the command
git remote add url. For example on github we can add a remote repo as git remote add firstproject https://github.com/username/firstproject. You can add multiple remote repositories to your project just by changing the shortname and url.

Git Branching

Branches in git emulate a movable pointer that points to a particular commit. Every commit in a particular branch pushes that branch closer to the HEAD. HEAD in git is a symbolic reference that points to the most recent commit in the currently checked-out. branch. This way developers can view which branch has the latest changes and which ones are still behind.
Branching helps developers to test their code in staging branches to verify that the code is Ok before pushing to production.
The default branch is known as master which can be overwritten with the command:

git branch -M main

Its possible to create a new branch with

git branch <branchname> //create new branch
git checkout <branchname> //switch to branch

Pushing your changes

This is the step where we push our code to the remote repository. There are different servers where you can push your code such as GitHub, GitLab and BitBucket.

To push your codebase to a remote repository:

git push <remote_name> <branch>
# An example could be 
git push origin main
# or
git push backup master

This makes it possible to push to different origins and different branches. You can also merge branches using git merge.

You can also create a new branch and push to its remote using one command:
git push -u

You can push a single specific tag which will create a snapshot of a particular release point version that you may want to deploy

Before pushing we need to create the tag as:

git tag -a v1.0 -m "First release version v1.0"

Then push with

# push only v1.0 tag
git push <remote_name> v1.0 

# push all existing tags
git push <remote_name> --tags

Pulling The Latest Version

git pull is the command used to fetch from remote branches and integrate with your local environment

Summary Of Commands Used

# initialize repository
git init 
# track all files in repo
git add . 
# check tracked files status
git status
# take snapshot of files with message
git commit -m "My message"
#view commit history
git log
# add remote repo
git remote add remote origin https://<URL-to-remote
# Overwrite default branch
git branch -M <branch_name>
# Push to remote
git push -u origin main
Total
0
Shares
Leave a Reply

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

Previous Post
have-you-studied-dr.-deming’s-teachings?

Have You Studied Dr. Deming’s Teachings?

Related Posts