Basics of Git

A short intro on git before we move on to the code.

Dhayaalan Raju
IVYMobility TechBytes
5 min readApr 8, 2020

--

What is Git?

Git is generally termed as Version Control. Let's see what it means.

Version control is a system that records changes to a file or set of files over time, so that you can recall specific versions later. It allows you to revert selected files back to a previous state, compare changes over time, see who last modified something that might be causing a problem, and more.

States of Git:

Git has three main states that your files can reside in: modified staged, and committed

  • Modified means that you have changed the file but have not committed it to your database yet.
  • Staged means that you have marked a modified file in its current version to go into your next commit.
  • Committed means that the data is safely stored in your local database

Okay. Enough with the intro. Let's move on to the code.

The command line is the only place you can run all Git commands, most of the GUI’s implement only some of the Git functionality for simplicity.

The Topics covered are:

  1. Getting a Git Repository
  2. Cloning
  3. Checking Status
  4. Removing Files
  5. Moving Files
  6. Viewing Commit History
  7. Undoing Things
  8. Working with Remotes
  9. Tags
  10. Aliases

1. Getting a Git Repository

There are two ways in getting a git repository, take a local directory and turn it into a git repository or clone an existing git repository.

Initializing a Repository

First, go to the project directory, it differs depending on which Operating System you are running. For windows, it goes like this

$ cd D:/User/myProject

and then types

$ git init

This creates a git repository skeleton. At this point nothing is tracked yet. It is accomplished with few git add commands that specify the files followed by a git commit

$ git add .$ git commit -m ‘Initial project version’

Here, git add .’ adds all the files in the folder. To add a specific file type the file name.

2. Cloning

If you want to get a copy of an existing Git repository the command you need is git clone

$ git clone <URL>

3. Checking Status

To see the state of a file the command used is

$ git status

4. Removing Files

The command used to remove a file is

$ git rm <file name>

To remove a file from git it has to be removed from tracked files and then commit it, the above command does this job.

Simply removing the file from the working directory does not complete the job.

5. Moving Files

$ git mv <file_from> <file_to>

This git mv has another use. It is equivalent to renaming a file

6. Viewing Commit History

After creating several commits if you want to look back to see want happened the command used is

$ git log

This command is one of the power full commands in git. Running this command lists the commits made in that repository in reverse chronological order.

A huge number and variety of options to the git log command are available to show you exactly what you’re looking for. Let's see some of the most popular ones.

$ git log -- patch

This shows the difference introduced in each commit.

$ git log -- stat

This option prints each commit entry, a list of modified files, how many
files were changed, and how many lines in those files were added and removed. It also gives a summary of the information at the end.

Some other options are

name-only, --graph, --pretty, —-online, etc.

Limiting Log Output

These options let you show only a subset of commits. Some of the common options are -<n>, — since and — until

$ git log - 2

This shows the last 2 commits.

$ git log -- since=3.weeks

This gives the list of commits made in the last 3 weeks.

Some other options are

—-after, --before, --author, --grep etc.

7. Undoing Things

This is one of the few areas in git where we might lose some work if we do something wrong.

If you want to redo a commit then make the additional changes you forgot, stage them, and commit again using the — amend option:

$ git commit --amend

8. Working with Remotes

Remote repositories are versions of your project that are hosted on the Internet.

Viewing Remotes

$ git remote

This command shows the configured remote servers.

$ git remote -v

This command shows the URLs of the remote repositories and lists all the available remotes.

Adding Remote Repositories

$ git remote add <short name> <URL>

In this command <short name> just makes reference easy.

Fetching and Pulling from Remotes

$ git fetch <remote>

This command just downloads the data to your local repository it doesn’t automatically merge it with any of your work. To accomplish this operation the command used is git pull

Pushing to Remotes

When your project is ready and you need to share it u push it.

The command for this is git push <remote> <branch>

If you want to push your master branch to your origin server then you can run this

$ git push origin master

Inspecting a Remote

To see information about a particular Remote this command is used

$ git remote show <remote>

Renaming and Removing a Remote

You can rename a remote’s short name using this command

$ git remote rename <old name> <new name>

To remove a remote this command is used

$ git remote remove <remote>

You can also use rm instead of remove

9. Tagging

This functionality is used to mark release points and important versions.

There are two types of tags:

i)Annotated Tags- These are stored as full objects in the Git database. They’re checksummed, contain the tagger name, email, and date and have a tagging message.

Creating the annotated tag is done with -a

$ git tag -a <version description> -m "message description"

Here -m specifies a tagging message

ii)Lightweight Tags- It’s just a pointer to a specific commit.

$ git tag <version description>

For creating lightweight tags just don’t use -a or -m

Deleting Tags

There are two steps in deleting a tag. You should delete it locally and in the remote also.

To delete a tag in the local repository the command used is

$ git tag -d <tag name>

To delete a tag in remote the command used is

$ git push origin -- delete <tag name>

10. Git Aliases

If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config.

$ git config -- global alias.ci commit

Here, instead of typing git commit, you can just enter git ci

If you are ready to learn about Git’s so-called “killer feature” you can click here

So far we have covered basic Git operations like creating and cloning a repository, modifying files, committing the changes, then we learned about remotes and tags and finally learned about aliases.

--

--