GitHub Tips And Tricks

for beginners and experienced programmers.

Raj Anand
CodeChef-VIT
4 min readMar 2, 2021

--

Have you ever wondered where your local repository is stored? Is cloning the only way you have used to interact with a repo? Wondered what is SSH?

This article aims to contain a few of the many little gems hidden under the mountain that is GitHub. If you are a newbie or an experienced programmer, these tips will surely come in handy.

Where’s my local repo?

Imagine you clone an existing project to your local drive, or, you made a new folder a created a new repo with the git init command. You make changes, crash stuff, fix stuff, etc. (But mostly crash stuff)

But where IS the repo? Where is the location to store and track the changes taking place?

It’s the .git folder

You can see this folder by enabling to show hidden files, folders.

The .git folder contains all the information that is necessary for your project in version control and commits, remote repository address, etc. It also contains a log that stores your commit history for rolling back.

Deleting that folder permanently removes the local repo.

Pushing a local repo to an existing private repo

So you made a local repo, did some top secret stuff, and want to push it to your newly created private repo.

The first time you try that, chances are, you tried a million different git commands that do everything except pushing to a newly created private repo.

Use these three lines below to easily push your local repository to a newly created private repo on Github.

git remote add origin https://github.com/username/repo-name.git
git branch -M main
git push -u origin main

These three lines are also visible when you create a new repo without initializing a readme file.

But pushing to a private repo needs your username and password to be entered in the terminal every time. If you are lazy (like me), there’s a really cool workaround. You set up an SSH key.

Setting up an SSH Key

An SSH key is an access credential for the SSH (secure shell) network protocol. This authenticated and encrypted secure network protocol is used for remote communication between machines on an unsecured open network. SSH is used for remote file transfer, network management, and remote operating system access.

In other words, SSH key helps GitHub identify that you are pushing from your own machine. You do not need to enter username and password every time you push.

Here are the steps to generate an SSH key and add it to your Windows computer:

Note for Mac users — replace ‘clip’ with ‘pbcopy’ below. The rest of the instructions are the same.

STEP 1: Open Git Bash

STEP 2: Type the following line

ssh-keygen -t rsa -b 4096 -C "email"

Use the email connected to your GitHub account. Press enter.

STEP 3: Customize where you want to save your SSH file.

Press Enter without typing anything for default. (Recommended)

STEP 4: Enter your passphrase to access your SSH key.

This is recommended to prevent anyone who acquires your SSH key to use it.

Your key should be generated. Now we have to tell our computer that we want to use it.

STEP 6: Start the SSH agent by typing the following line

eval $(sss-agent -s)

STEP 7: Add the SSH key

ssh-add ~/.ssh/id_rsa

~/.ssh/id_rsa is the default location. You might have to change it if you gave a custom file name in step 3.

STEP 8: Enter the passphrase

The identity has been added. Now we need to copy the public key so we can add it to GitHub.

STEP 9: Copy the key by typing

Windows
clip < ~/.ssh/id_rsa.pub
Mac
pbcopy < ~/.ssh/id_rsa.pub

Now, we add the key to GitHub

STEP 10: Login to GitHub, go to your settings → SSH and GPG keys

STEP 11: Click on NEW SSH key. Give a title you want to represent your machine with. Paste your key. Add it.

That’s it.

You can now push to your private repositories without authenticating them every time.

Switching remote repo

You set up everything, you are about to push to a repo, and then you realize — you are pushing to the wrong remote repository. There’s an easy fix for it.

$ git remote set-url origin new-url

If only we could switch between life choices that easily.

I hope that you had a great time reading this article!

--

--