How to Install Git and Push Codes to Github

In Git on 2~4 minutes
How to Install Git and Push Codes to Github

This question explains how to set up a local Git repository and push it to Github.

Git Installation

Windows Installation

  1. Go to https://git-scm.com/downloads and click “Download For Windows”.
  2. Select 32 bit or 64 bit according to your PC architecture.
  3. Open the downloaded exe file.

The installation wizard will ask for basic information like the installation path, whether create desktop icons or not etc. You can leave them all in their default settings or make necessary changes and press “Next”. Ultimately, you will see the “Install” button at the end and it will begin the installation process.

Linux Installation

Open a terminal window and enter the following command, it will update the local software repository.

sudo apt-get update

Then install Git using the following command.

sudo apt-get install git

Mac installation

  1. Install Homebrew if not already installed
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Git
    brew install git

Visit https://git-scm.com/downloads and click “Download for Mac” for more information and alternative installation methods.

Check The Installation

Run the following command, it should display the installed Git version.

git --version

Create Your First Git Repository

First, create a new directory.

mkdir hello-git

Go into that directory.

cd hello-git

Initialize a new local Git repository

git init

Create a file called hello-git.html or otherwise, you can add your code files.

touch hello-git.html

Add some content into hello-git.html file and check the status of the repository. It will display the changed files.

git status

Add hello-git.html to the repository.

git add hello-git.html

If you have multiple files, you can add them all by using -A flag.

git add -A

Commit changes with a short and meaningful message like what you did in the commit.

git commit -m "hello-git.txt added"

Push To Github

Set the Github username. Your username is the part after the 3rd forward-slash (/) of your profile URL. For example, in https://github.com/bitcoin, bitcoin is the username.

git config --global user.username your_github_username

Create a new repository in Github by visiting https://github.com/new.

Link the Github remote repository to the local repository.

git remote add origin https://github.com/your_github_username/hello-git.git

Push local repository to Github repository.

git push origin master