Back

Github

We now start a discussion of one of the big topics: Github. It is a host platform for git repositories. There are important reasons to use Github, for example, to backup our projects, improve collaborations, and be part of the open-source community.

Cloning repos

With cloning, we do not need to create all our projects from scratch; we can clone an existing repo. All we need is the URL of the repo we want to clone. The command is:

git clone <url>

Note that anyone can clone a public repository. Moreover, this command is not tied exclusively to Github.

Github setup

The first thing we need to do is to generate SSH keys. See details here. The whole process is a bit tedious, but not difficult.

Creating Github repos

Now we want to create new Github repositories. There are two situations:

Uploading repos

If we already have repositories in our machine, and we want to upload them to Github, we follow these steps:

  1. Create a new Github repo
  2. Connect a local repo (add a remote)
  3. Push up our changes

Starting from scratch

The other option is when we want to start a brand-new project. In this case, we do the following:

  1. Create a new Github repo
  2. Clone it to our machine
  3. Do some work locally
  4. Push up our changes

This second option is easy if we understand the first.

Git remote

Once we create a Github repo, we need to tell git about the existence of the Github project. To view the existing remotes on my computer, we use the following command:

git remote -v

The -v stands for verbose. If the remote does not exist, we can create a new one with:

git remote add <name> <url>

A standard name is origin. We can change the name of a remote or delete it with:

git remote rename <old_name> <new_name>
git remote remove <name>

Git push

Now we want to push our modifications. We do it with the command:

git push <remote> <branch>

We do not need to push the branch we have locally to the same branch on Github. We can even change the branch names with:

git push <remote> <local_branch>:<remote_branch>

Finally, pay attention to the flag -u:

git push -u <remote> <branch>

The -u stands for upstream. It links the Github repo to our local machine, simplifying future commands. After setting this, we can push our modifications with:

git push

Master & Main

In line with Github’s recent updates, we can use the term main instead of master. To change the branch name of our repos, we use:

git branch -M main

Forcing push

If we amend a commit and try to push this modification to Github, it will reject the changes as a safety measure. To force the push, we can use:

git push -f

This command will overwrite the previous commit (the one we are amending). If someone else is working on it, it may cause problems. To avoid this in team settings, we can use personal branches. For solo projects, it should be fine.