Tutorial to get started using Github

I got tripped up the first time I tried to publish my own code on Github, so I thought I would write up a short tutorial for other newbies like me. I am going to explain how I use Github from the command line of your computer, and I recommend sticking to this method, since using the graphical interface through the Github web site or the Github app will hobble your abilities. In contrast, learning how to use git from the command line will empower you

The first thing to do is to follow these instructions to create your account on Github. After you have an account, go to https://github.com and login. Then, click on the green “New” button in the upper left hand corner of the screen to create a new repository:

Then fill out the form to create a new repository. I recommend not marking the option to create the README.md, since you can do that later using the text editor of your choice. If you decide to mark the “Choose a license” to select your new program’s license from a list, you will see that Microsoft does not like certain licenses and doesn’t include them as options (especially some of the copyleft licenses from the Free Software Foundation). You can always add your license files later.

After clicking on “Create repository”, the URL for you new repository will appear.

The next step is to get an access token so you can make commits to your new Git repository from the command line. Unfortunately, Github no longer allows you to push commits from the command line with a normal password, so you will need to use an access token.

To get a Github access token, go to https://github.com and login using your username and password. Once you are logged in, click on your Account icon on the right hand side and select “Settings” from the dropdown menu.

In the next page, click on the “Developer settings” option on the left side of the screen and then click on “Personal access tokens” in the next screen.

In the next screen, click on “Generate new token”. Then in the “New personal access token” screen, add a description of your new access token in the “Note” box. Github wants users to constantly be creating new access token or renewing their existing tokens. I don’t make commits very often, and having to generate a new token every time I want to make a commit is frankly a pain in the ass and a waste of my time. Under “Expiration”, I recommend selecting “No expiration” or selecting ” Custom” and selecting a date which is just under one year in the future.”

Under “Select scopes”, I recommend just marking the “repo” option and its suboptions. The other options maybe useful for some people, but I have never bothered using them, so I see no reason to let the access token have access to these options.

Finally, click on the green “Generate token” button at the bottom of the screen. The access token will be something like “ghp S4Ejl4fIHvxy3YSeQe3za72mnGR6HJ3hZmAU” which is impossible to remember, so you need to immediately click on the copy icon next to it to copy it and then paste it into a text file which you should save in some secure location on your computer. It is also helpful to add your Github username, password and the expiration date of your access token to that same file, so you don’t forget it.

Now that you have an access token, go to https://git-scm.com/downloads and download git and install it on your local computer if you are using Windows or Mac OS. If using Linux, it is recommended to install git from your distribution. For example, in Debian-based distros, you can use the command:
sudo apt install git

Once you have git installed on your computer, then create a directory where you will store the source code for your program. Inside that directory add a README.md file to explain the purpose of your program and how to use it.

For example in Linux, you can use the following commands in a terminal:
mkdir myprogram
cd myprogram
echo "# myprogram" >> README.md

Add any code and other content that you want to this same directory. You may be tempted to use the graphical interface on the Github web site or the Github app to create your README.md file and other files, but that is a bad idea if you want to later switch to the command line on your computer. Any changes that you make through the graphical interface have to then be imported into your directory through git. I found Github very hard to use when trying to make changes to my repositories through both the graphical interface and the command line, so I recommend using either one or the other, but not both.

Then, initialize your new git repository and add the new README.md and any other files or directories:
git init
git add README.md

You can use the * wildcard to add all the files and subdirectories in the directory to your repository:
git add *
or for using glob-style file matching:
git add mydir/widget*.java

If you make a mistake, you can use the git rm command to remove files from your repository:
git rm myfile.java
To remove all the files inside a subdirectory, use git rm -r, which means to remove recursively:
git rm -r mydir

Then, make a commit and add a message to identify your commit, such as:
git commit -m "my first commit"

The first time you make a commit to a new repository, you will have to specify that you are updating the “main” branch and specify this repository is stored remotely at https://github.com/username/repository.git, where username is your Github username and repository is the name of your repository that you created in Github. Finally, use git push to upload the first commit to your new repository in Github.

For example:
git branch -M main
git remote add origin https://github.com/janedoe/mycoolprogram.git
git push -u origin main

If you already have an existing git repository on your computer that you want to upload to your new Github repository, then you can issue these commands instead:

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

After issuing the git push command, you will be asked for your Github username and password. The password is the access token that you obtained which you can copy and paste from the file where you saved it. Remember that it should start with “ghp “, so it include that part and then press ENTER. Then, the first commit should be uploaded to the new Github repository under your account, so it will be available at: https://github.com/username/repository/

If you forgot to save your access token, you can go to https://github.com/settings/tokens and click on your token to edit it. In the following screen, there will be a “Regenerate Token” button that you can click to get an updated access token.

After the first commit to a repository, later commits are easier to make. Make the changes that you want to the directory on your computer where you are storing your local copy of the repository.

To see the changes that you have made since your last commit, either use the commands git diff or git show. Add or remove any files that you want using the git add or git rm commands.

Then, add a new commit and give it a message to identify it:
git commit -a -m "my description of commit"
git push

After entering the password and access token for the password, the new commit will be uploaded to Github.

Have fun and add more code to your first Github repository.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s