The popular Github site along with the git tool makes for an excellent resource to not only distribute and showcase your work, but also to facilitate efficient and professional version control.

In this tutorial, we will explore how to create a Github repository, push commits, tag releases, and more.

Create a Repository

To create a new repository, first log in to Github or register for a new account. Once logged in, click on the Create New button in the top-right corner of the screen, followed by the Repository link in the drop-down list that appears.

You will see the create repository screen:

The repository name and description can be anything you wish, and for this example "muo_demo" was used as the name. Leave the rest of the fields as they are / blank, and hit the Create new Repository button. You will be brought to a page displaying your new blank repository.

Related: What Is GitHub? An Introduction to Its Basic Features

Initiate Local Repository

Now that a repository on Github has been created, you need to initialize the repository on your local PC. Run the following commands in the terminal to create a blank directory, and add a Readme.md file.

        mkdir myrepo
    
        cd myrepo
    
        echo "# My Test Repository" > Readme.md
    
        echo "A temporary file" > temp.txt
    

When viewing a repository on Github, the contents of the Readme.md file is always displayed to describe the repository, or as the first page of the manual. The .md file extension stands for markdown format, and if you're unfamiliar with Markdown, check out our excellent Markdown format cheat sheet.

You can now initiate the repository within the terminal with the commands.

        git init
    
        git remote add origin https://github.com/mdizak/muo_demo.git
    

In the second command, you need to change the "mdizak" to your Github username, and the "muo_demo" part to the name of your repository. For example, if your Github username is "johndoe" and the name of your repository is "test_repo", the command would be:

        git remote add origin https://github.com/johndoe/test_repo.git
    

First Commit

You can now sync the local and Github repositories, and add the two files to Github, with the following commands in terminal.

        git add Readme.md temp.txt
    
        git commit -m "My first commit"
    
        git push -u origin master
    

You will be prompted for your Github username and password, and upon successful entry the two files will be uploaded to your Github repository. If you reload your Github repository in your browser, you will now see the two files along with the "My Test Repository" header within the Readme.md file.

Large Commit Messages

Instead of only specifying a small single line commit message, it is also possible to include a larger text message. In your favorite text editor, enter the contents of the commit message, which can be as large and as many lines as desired. When you commit the latest changes, use the command.

        git commit --file=/path/to/commit.txt
    

Ensure the command points to the text file of your commit message, and its contents will be used instead of the single line message defined via the -m option.

Deleting Files

Deleting files is done in much the same way, except for using the above git add command, we use the git rm command. To delete the temp.txt file you previously added, run the following commands in terminal.

        git rm temp.txt
    
        git commit -m "Deleting temp file"
    
        git push -u origin master
    

You will be prompted for your username and password again, and once done, the temp.txt file will be deleted from your Github repository. That's all there is to it!

Related: How to Delete Unwanted Repositories on GitHub

Tagging Releases

From time to time, once you're perfectly happy with your tested project, you may want to tag the current state as a release. This informs others that the project in its state is complete and ready for distribution to the public.

Marking your repository as a release is done by adding a tag with the version number. For example, to release our current repository as v1.0.0 within the terminal run the commands.

        git tag 1.0.0
    
        git push --tags
    

After entering your Github username and password, reload the repository page in your web browser. You will notice there is now one release to your repository. Clicking through to view all releases will provide a link to the TAR.GZ archive for the full repository in the state of when the release was tagged.

Clone Repository

At times you will need to clone and reinitialize your repository from scratch. This is easily done with the following commands in the terminal.

        git clone https://github.com/mdizak/muo_demo.git myrepo
    
        cd myrepo
    
        git init
    

Same as when we initialized the repository, within the first line you need to change the Github username and repository name within the URL.

This will download the contents of the repository from Github into the /myrepo/ directory, and will then reinitialize it with the git init command. From there, you can continue adding and deleting files the same as above.

View Commit History

You can go back and view your previous commits with the git log, such as:

        git log - 3
    

The above command will display the last three commits made to the repository. This can come in useful if you ever need to modify or delete a commit for any reason.

Amend a Commit Message

If you have already pushed a commit to Github, then later realized you made a mistake to the commit message, there is a way to amend it. To amend the latest commit message, within the terminal run the command.

        git commit --amend
    

This will open your default text editor with the previous commit message. Make any necessary changes, and close the editor. If using nano as the text editor, you may close it by pressing Ctrl + X, followed by the Y and Enter keys.

Once you have saved the new commit message, push it to Github with the command:

        git push --force origin
    

Ready to Show Off Your Work!

Congratulations, you've now learned the basics of using the popular Github web site along with the git command-line tool. You have successfully created a repository, and can now add/delete files, push commits, tag releases, and clone repositories.

If you wish to learn more advanced git commands, check out the Advanced git Commands site.