Getting Started With Git

Muhammad Danyal
6 min readApr 19, 2020

--

Git is a fast, scalable, distributed version control system with a rich command set that provides both high-level operations and full access to internals. There are many online version control repositories available that are using Git, e.g. GitHub, BitBucket, GitLab, etc. You can create your account on any of these to sync with your local Git repository.

How to use Git

In order to use Git, first, you have to download the Git installer on your machine and install it locally. Use the following link to download the Git installer.

After Git is installed on your local machine, you are ready to start using it. Git installer provides you with Git bash (command-line tool) if you have selected the Git bash option while installation. Otherwise, you can also use your OS command-line interface.

Configuration

After installation, you need to configure your Git credentials. Git provides three different scopes for storing configuration data. Accordingly, when using the git config command, one of these three scopes can be specified:

  1. Local
  2. Global
  3. System

The scopes are cascading, so system scope trumps global scope and local scope trumps global scope. For Windows, here is the location for the Git configuration file.

The system Git config file is found in the mingw32\etc folder of the Git installation.

The global Git configuration file is found in the root of the user’s local profile or home directory (C:\Users\git-user\).

The local Git config file is stored inside the Git directory of the repository in which you are working.

The very basic information you need to provide in the configuration file is Git user name and email. Open Git bash or you OS CLI and use the following commands to set your Git credentials

1. git config global user.name “myusername”

2. git config — global user.email ”username@email.com

Getting a Git Repository

You can obtain a Git repository in one of two ways:

1. You can take a local directory that is currently not under version control, and turn it into a Git repository, or

2. You can clone an existing Git repository from elsewhere.

In either case, you end up with a Git repository on your local machine, ready for work.

To take a local copy that is not under version control yet, you need to run the following commands. This will create a .git folder in your working directory.

1. git init

2. git remote add origin repo-url

3. git pull

If you already have a remote repository and you want to clone it locally, you need to run the following command. It will copy all the remote files in your local working directory.

1. git clone repo-url

In Git, “origin” is a shorthand name for the remote repository that a project was originally cloned from. More precisely, it is used instead of that original repository’s URL and makes referencing much easier.

Pushing your code to the remote repository

Now as you have a local copy of your online repository and you want to push your local changes to the remote repository, you need to run the following commands

1. git add

2. git commit

3. git push

Git Add

Add means the files are getting tracked now by the Git and are added to the local staging area. Use, git add -A, to move all changed (deleted and new files) from the working area to the staging area. You can also use git add –u to adds only updated (modified+deleted) or (tracked only) files to the staging area. If you added some files in the staging area by mistake, then you can use git reset <file-name> to unstage them.

Git Commit

Commit is used to add staged changes to the local repository. You should always use –m flag with the commit command to include a commit message git commit -m “your message”. To see the file that was changed or added in a commit, use -stat argument with git log like this git log -stat. You can use git diff to see what code changes between the current state of the files and the state of the files in the previous commit. Also if you want to revert changes of a commit, you can use git reset -hard HEAD (git reset -hard HEAD~2 to go further back)

  • HEAD is a reference to the last commit in the current check-out branch. You can think of the HEAD as the “current branch”. When you switch branches with git checkout, the HEAD revision changes to point to the tip of the new branch

Git Push

The push term refers to upload local repository content to a remote repository. Pushing is an act of transfer commits from your local repository to a remote repository. Pushing is capable of overwriting changes and caution should be taken when pushing. Simply git push command will upload your changes from commit to the remote repository. If you want to specify the remote repository URL and branch, you can use the command git push -u origin master. If there are conflicts between your local changes and the remote branch, you need to fix them. If you want to force upload your changes, you can you the –force flag git push –force

At any stage, if you want to check the status of your changes, you can use the git status command. Also, you can create .gitignore file in your local directory if you want to skip some files to be tracked.

What are branches

Let’s say you are working on some cool features. You get the code from your remote repository and start working on it, but you don’t want to mess things up in the remote copy. Instead, you want to create a separate working copy of your code and later on merge it with the main copy. That’s where branches get involved.

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes.

The master branch is a default branch in Git. It is instantiated when the first commit made on the project. When you make the first commit, you’re given a master branch to the starting commit point.

To create a new branch, you can use the following commands

1. git branch <branch-name>

2. git checkout <branch-name>

The above two steps can be carried out at once using git checkout -b <branch-name>. The branch is created from the head, which is the last commit. When we run the command git push -u origin <branch-name>, this will also create the branch on remote if not present. A branch can be deleted using git branch -d <branch-name> command.

Git Merge

Git merge is used to merge two branches. If you want to merge your branch with the master branch you can run the command git merge master, and then you can run git push to upload your changes to the remote branch. When your changes are uploaded to your remote branch, you may want to merge your remote branch with the remote master branch. In order to do that you can also create a request to merge.

Merge or pull requests are created in a Git management application and ask an assigned person to merge two branches.

Tools such as GitHub and Bitbucket choose the name pull request since the first manual action would be to pull the feature branch.

Tools such as GitLab and Gitorious choose the name merge request since that is the final action that is requested of the assignee.

What's Next

That is almost all you need to know about Git, to get started using Git. But there is a lot more. There are a lot more commands and features provided by Git. Once you get familiar with these basics you can move forward to learn more advanced Git commands like git stash, cherry-pick, fetch, etc. This tutorial was about using Git command line but there are lots of tool available which provide graphical interfaces to use Git. Also, many advanced IDEs and tools like visual studio code also provide built-in integration with Git.

For more details, check the following

https://git-scm.com/docs

--

--