Getting started with Git

Total Views : 390
Zoom In Zoom Out Read Later Print

In this post, we will install and setup git on Mac, Linux, and Windows system. you can install it in multiple ways but two major ones are to install an existing package for your platform or from the source.


1. Installing Git
2. Setting user name and email
3. Set up SSH for Git
4. Setting up the upstream remote
5. Learning about a command
6. Creating, Adding and Commit files
7. Clone a repository
8. Sharing code

1. Installing Git

1.1. Installing on Windows

Installing Git on Windows is very straight forward. The Git project has one of the easier installation procedures on windows. Simply download the installer exe file from the website page, and run it:

After it’s installed, you have both a command-line version (including an SSH client that will come in handy in some cases, will use later) and the standard GUI.

1.2. Installing on Mac

There are three simple and easy ways to install Git on a Mac. The easiest is to use the graphical Git installer, which you can download from the SourceForge page( Click Here ).

The second way is to install Git via MacPorts. If you have MacPorts installed, install Git using following command

$ sudo port install git +svn +doc +bash_completion +gitweb

You don’t need to add all the extras, but you’ll probably want to include +svn in case you ever have to use Git with a Subversion repositories

Homebrew is another way to install Git. If you have Homebrew installed, install Git via the following command

$ brew install git

1.3. Installing on Linux

If you have Linux as your OS and want to install Git on Linux via a binary installer, you can do so through the basic package-management tool that comes with your Linux distribution.
If you’re on Fedora, you can use yum:

$ yum install git

Or if you’re on a Debian-based distribution like Ubuntu, you need to use apt-get:

$ apt-get install git

1.4 Installing from Source

If you can do this, it’s generally good to install Git from source, because you’ll get the most recent and updated version. Each version of Git tends to include useful UI enhancements, so getting the latest version is often the best if you feel good.
To install Git, you need to have the following libraries that Git depends on when installing from source: curl, expat, and libiconv, openssl, zlib

If you’re on a system that has yum (such as Fedora) use following command

$ yum install curl-devel expat-devel gettext-devel \ openssl-devel zlib-devel

if you’re on a system that has apt-get (such as Debian based system)

$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \ libz-dev libssl-dev

When you have installed all the necessary dependencies, you can go ahead and download the latest snapshot from the Git website:
After download  you have to compile and install :

$ tar -zxf git-2.26.0.tar.gz
$ cd git-2.26.0
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install

After this is done, you can also get Git via Git itself for updates:

$ git clone git://
2. Setting UserName and Email

You need to set who you are mean your name and email id before creating any commit to a repo. That will allow commits to have the right author name and email associated with them. It has nothing to do with authentication when pushing to a remote repository using your GitLab, BitBucket, or GitHub account.

To declare that identity for all repositories globally, use

git config --global

This will store all your settings in your user .gitconfig file: e.g. for Windows, %USERPROFILE%\.gitconfig. and $HOME/.gitconfig

git config --global "Your Name"
git config --global

# For Example
git config --global "ProDevs"
git config --global

If you want to use a single repository, to declare an identity for a single repository, use git config inside a repo. This will store the setting inside the individual repository, in the file $GIT_PRJ_DIR/config. e.g. /path/to/your/git/repo/.git/config.

cd /path/to/your/git/repo
git config "Your Name"
git config

Settings stored in a repository's config file will take precedence over the global config when you use that repository.

if you have different identities (one for an open-source project, one at work, one for a private repository, and you don't want to forget to set the right one for each different repository you are working on:

Remove a global identity from git config:

git config --global --remove-section
git config --global --remove-section

To force git to look for your identity only within a local repository's settings you are working on, not in the global config use:

git config --global user.useConfigOnly true

That way, if you forget to set your and values for a given repository and try to make a commit, you will see two errors:

no name was given and auto-detection is disabled
no email was given and auto-detection is disabled
3. Set up SSH for Git

If you are using Windows open Git Bash Terminal or If you are using Linux or Mac open your Terminal.

Before you generate an SSH key, you can check to see if you have any existing SSH keys or you can have a new one even if you have one.

List the contents of your ~/.ssh directory:

$ ls -al ~/.ssh
# Lists all the files stored in ~/.ssh directory

Check the directory listing to see if you already have a public SSH key. By default the filenames of the public keys are one of the following if not changed:

If you see an existing public and private key pair listed in the above result that you would like to use on your GitHub, Bitbucket or a similar account you can copy the contents of the id_*.pub file.

If no key exists, you can create a new public and private key pair using the following command

$ ssh-keygen

Press the Enter or Return key to accept the default location/directory. Enter the passphrase and then re-enter a passphrase when prompted, or leave it empty if you want.

Ensure your SSH key is added to the ssh-agent. Start the ssh-agent in the background if it's not already up and running:

$ eval "$(ssh-agent -s)"

Add your SSH key to the ssh-agent. Notice that you'll need to replace id_rsa in the command with the name of the private key file you have created:

$ ssh-add ~/.ssh/id_rsa

If you want to change the upstream of an existing repository from HTTPS to SSH you can use the following command:

$ git remote set-url origin ssh://

In order to clone a new repository over SSH you can use the following command:

$ git clone ssh://
4. Setting up the upstream remote

If you have cloned a fork you may not have push access to the upstream repository (e.g. an open-source project on Github), so you need both your fork but be able to fetch the upstream repository.
First, you need to check the remote names:

$ git remote -v
origin (fetch)
origin (push)

If upstream is there already (it is on some Git versions) you need to set the URL which is currently it's empty:

$ git remote set-url upstream

If the upstream is not there, or if you also want to add a friend/colleague's fork which is currently they do not exist:

$ git remote add upstream
$ git remote add devs
5. Learning about a command

To get more information about any git command – i.e. details about what the command does, available options and other documentation use the --help option after the command or the help command with the name of the command you want to see info.
For example, to get all available information about the git diff command, use:

git diff --help
git help diff

If you only want a quick help showing you the meaning of the most used command-line flags, use -h:

git checkout -h
6. Creating, Adding and Commit files

At the command line, first, verify that you have Git installed, on all operating systems because we are running git command so use:

git --version

On UNIX-like operating systems:

which git

If the command is not recognized or nothing returned, you may have to install Git on your system by downloading and running the installer or the installation is not done properly. Check the installation section for instructions or the Git homepage. After this Complete Step 2 for this to be done.

Navigate to the directory you want to place under version control and create an empty Git repository. run the following command

git init

This creates a hidden folder, .git, which contains the plumbing needed for Git to work. e.g. config files.
Next, check what files Git will add to your new repository; this step is worth special care, use following command:

git status

Review the resulting list of files; you can tell Git which of the files to place into version control or which to ignore (avoid adding files with confidential information such as passwords, or files that just clutter the repo):

git add <project/path/directory name #1> <project/path/directory name #2> < ... >

If all files in the list should be shared with everyone who has access to the repository, a single command will add everything in your current directory and its subdirectories:

git add

This will mark all files to be added to version control, preparing them to be committed in your first commit. For files that you want never under version control, create and populate a file named .gitignore before running the add command.

Commit all the files that have been added, along with a commit message:

git commit -m "Initial commit"

This creates a new commit with the given message Initial commit. A commit is like a save or snapshot of your entire project. You can now push it to a remote repository, and later you can jump back to it if necessary. If you omit the -m parameter, your default editor will open and you can edit and save the commit message there.

Adding a remote

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at.

The git remote add command needs two arguments:

1. A remote name, for example, origin
2. A remote URL, for example,

git remote add origin https://<git.server.address>/owner/repo.git
#git remote add origin

push or pull commits after adding your remote.

git push -u origin master
7. Clone a repository

The git clone command is used to copy an existing Git repository from a server to the local machine.
For example, to clone a GitHub project use the following steps:

cd path/of/the/clone/save/folder/
git clone

For example, to clone a BitBucket project use the following steps:

cd path/of/the/clone/save/folder/
git clone

This creates a directory called the project name on the local machine, containing all the files in the remote Git repository. This includes source files for the project, as well as a .git sub-directory which contains the entire history and configuration for the project.

To specify a different name of the directory, e.g. GitFolder:

git clone GitFolder

Or to clone in the current directory you are in use:

git clone .

When cloning to a specified directory, the directory must be empty or non-existent which you are cloning.

You can also use the ssh version of the command if you want to:

git clone
8. Sharing code

in order to share your code, you need to create a repository on a remote server and copy your local repository. To minimize the use of space on the remote server you create a bare repository: one which has only the .git objects and doesn't create a working copy in the filesystem. As a bonus, you set this remote as an upstream server to easily share updates with other programmers.

On the remote server run:

git init --bare /path/to/repo.git

On the local machine run:

git remote add origin ssh://username@server:/path/to/repo.git

Now copy your local repository to the remote:

git push --set-upstream origin master

Adding --set-upstream (or -u) created an upstream (tracking) reference which is used by argument-less Git commands, e.g. git pull.

See More

Latest Photos