DEV Community

Devtonight
Devtonight

Posted on • Updated on • Originally published at devtonight.com

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
Enter fullscreen mode Exit fullscreen mode

Then install Git using the following command.

sudo apt-get install git
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create Your First Git Repository

First, create a new directory.

mkdir hello-git
Enter fullscreen mode Exit fullscreen mode

Go into that directory.

cd hello-git
Enter fullscreen mode Exit fullscreen mode

Initialize a new local Git repository

git init
Enter fullscreen mode Exit fullscreen mode

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

touch hello-git.html
Enter fullscreen mode Exit fullscreen mode

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

git status
Enter fullscreen mode Exit fullscreen mode

Add hello-git.html to the repository.

git add hello-git.html
Enter fullscreen mode Exit fullscreen mode

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

git add -A
Enter fullscreen mode Exit fullscreen mode

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

git commit -m "hello-git.txt added"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Push local repository to Github repository.

git push origin master
Enter fullscreen mode Exit fullscreen mode

Feel free to visit devtonight.com for more related content.

Latest comments (0)