A Comprehensive Guide on Publishing your existing code to GitHub for Absolute Beginners

Cheat Sheet to Push existing Code to GitHub - TutLinks
A Comprehensive Guide on Publishing your existing code to GitHub for Absolute Beginners – TutLinks.com

This tutorial will help you publish your existing project on your local PC to a new GitHub repository. Follow along the steps that will help you publish your code to the GitHub. If you are familiar with the basics of Operating System and other details about the internals of git, you can skip directly to the Quick Reference Cheat Sheet section to actually understand how to push your local repository to GitHub.

For most of those who love โค๏ธ programming and start to learn coding, it is very obvious that they need to track their changes made to a software programming code over a period of time. It could be a personal hobby project or an assignment submission through programming or a code maintained for an enterprise level software, there is always a need for tracking the changes made to your code through various versions. There are several version control tools available ranging from commercial to open source, but Git has been the go to tool for most of the Software Programmers. Tortoise SVN (free), IBM Rational ClearCase (Commercial) to name a few of other source control version tracking tools.

GitHub is an online platform where you can host your programming related code or projects for absolutely free. Your repositories are publicly visible by default. You have the option to toggle the visibility to Private mode too. You can have unlimited public or private repositories on your GitHub account.

This tutorial will walk you through the step by step approach about how to add your local code base to your GitHub account.

Prerequisites

To help you get started, you need to have the below mentioned prerequisites.

  • A Personal Computer with popular OS like Linux/Unix, Windows OS or Mac OS.
  • A reliable Internet Connectivity.
  • A free GitHub Account.
  • Install free Git Bash for Windows. You can install from Git-Scm too which has the in-built GUI client available for all popular operating systems like Windows, Linux/Unix and Mac OS.

Signup for Free GitHub Account

Signup here for the free GitHub account and have your email id verified.

Install Git SCM or Git Bash

Download and install Git Bash for Windows OS from GitForWindows or Git-Scm. Click here for Linux/Unix and here for Mac OS and install for your respective OS.

Verify the Installation

Once you are done with the installation, verify the installation by typing the following command in the command line terminal/ command prompt.

Open the command prompt

Windows OS Users

Click the Windows + r key and type cmd and click OK. You should see the windows command prompt launched.

Linux / Mac OS Users

Right Click on the desktop and from the context menu click on Open in Terminal option. You should see the command terminal.

Once you are on the command prompt type the following command to verify the installation of git

git --version

You should see the git version which was installed on your operating system.

Create a directory and Set it as Current Working Directory

Now we are interested in organizing our code in a single directory. Let us create a directory with the name that resembles the name of our project. In the terminal, type the following command to create a directory named my-first-repo-on-github and hit enter.

mkdir my-first-repo-on-github

Set the current working directory to the my-first-repo-on-github by typing the following command.

cd my-first-repo-on-github

Create a Simple Python Project

Create a python programming file

We will create a simple python program that prints Hello from TutLinks.com! In the command terminal type the following command and hit enter to create a file name main.py

Windows OS Users

echo print('Hello from TutLinks.com') > main.py

Linux / Mac OS Users

echo "print('Hello from TutLinks.com')" > main.py

The above command will create a file named main.py where .py is the default extension that python programming language code files are identified with.

Execute the Simple Python programming file

To boost your enthusiasm, let us run the first python programming file we created.

Windows OS Users

To run the new python file main.py, you can type the following command provided that you’ve installed python.

python main.py

For Windows OS users, Python doesn’t comes as a default installation. Check out this video tutorial in case you want to know how to install Python on Windows 10 OS.

Tutorial on How to Install Python on Windows 10 OS – YouTube.com/TutLinks
Linux / Mac OS Users

Python comes installed default with Linux. So in order for you to run this file, you need to refer to Python version 3. This is because the print statement is following python 3 syntax. So, type the following command in the terminal.

python3 main.py

The output should show Hello from TutLinks.com

Create a repository in your GitHub account

Now that we have our first and glorious simple python project that prints out the warm wishes Hello from TutLinks.com, we proceed and publish this project to GitHub.

Steps to Create a new repository in GitHub

  • Login to your verified GitHub account by visiting https://github.com/login and enter the credentials that you have provided during the signup.
  • From the logged in account navigate to the new project creation page by clicking on the + button visible on the top right of the header navigation or you can simply visit https://github.com/new. You will be landed on the Create a new repository page.
  • Provide the Repository name of your choice. For example we will give the name of the project my-first-repo-on-github as Repository name which we created on our PC.
  • Choose the visibility of your repository as Public or Private depending on your need.
  • Check the option to Initialize this repository with a README. It is always recommended to add a README file so that you can have certain help commands or How To stuff documented in that file. This will help yourself and other contributors of this repository as a reference doc. Having a README.md is optional though.
  • Select the option to Add .gitignore file and look for the programming language or programming framework that matches the project that you are adding to this repository. This option will add a nameless file with .gitignore extension according to the selection you have made. In our case, we will search for and select Python.
  • If you are publishing a new open-source library that you have implemented and feel that it has potential to help tech world, you can Add a license that you deem to match your need on how people should use your awesome library you have implemented. Or you can leave it as default option None.

.gitignore file will help you ignore the files and directories that are not crucial for the programming you implement. These include IDE setting directories like .vscode or compiled dlls or __pycache__ directories for python applications to mention a few. Thus only the files that are crucial for the project to compile and run are checked in to the repository and helps reduce the memory size the repository occupies. The contents of .gitignore file varies accordingly based on the programming language or framework you choose while creating the repository.

Advantage of .gitignore file

Once you have configured your settings for the new repository my-first-repo-on-github click on Create Repository button. You will be landed on your new repository page. Copy the url that ends with .git which looks similar to the url mentioned below.

https://github.com/your-awesome-username/my-first-repo-on-github.git

Notice that the your-awesome-username will contain your username that you chose while creating the account. Ensure you replace it with your username once you copy the above url.

Initialize and Configure repository on Local PC

Initialize Git Repository on Local PC

From command terminal, where the current working directory which is already set to my-first-repo-on-github type the following command to initialize git for our project.

git init

This command will create a hidden directory named .git that holds all the metadata related to our repository with regards to git such as config files that holds any remote urls that this repository is pointing to, or most recent commit message that is located in COMMIT_EDITMSG of .git and various other files. It is recommended to take a look at this .git to gain more understanding but you must be warned about editing anything inside this file. If you are not sure about the changes you are doing inside the .git hidden directory, then it is better leave it as is. Otherwise this might lead to corruption of git config for projects.

Point local repository to remotely located GitHub Repository

After initializing the repository with git, you must point your local repository to the remote GitHub repository by running the following command in the terminal.

git remote add origin https://github.com/your-awesome-username/my-first-repo-on-github.git

Check the remote repository pointed to local repository

In the command terminal, type the following command to verify to which remote repository you have configured so that the local project changes will be synced to when your code is pushed.

git remote -v

You should see the following output. Verify that you are pointed to the correct repository.

origin https://github.com/your-awesome-username/my-first-repo-on-github.git (fetch)
origin https://github.com/your-awesome-username/my-first-repo-on-github.git (push)

Sync your local with remote repository

Get the latest code available on the repository by typing the following command.

git pull origin master

The above command tells the git to pull the latest changes from the master branch. master is the name of the branch which gets created by default when you create a repository.

The best practice is to pull the latest changes available on the remote and then start working on the latest changes. By doing so, you can avoid overwriting any changes made to the same file by other contributors when you push your changes to the repository. This way you can avoid merge conflicts between your local code and the latest code on the remote repository.

To summarize, pull the changes and then start working on the repository.

Dealing with fatal: refusing to merge unrelated histories

Sometimes, you will end up seeing the following error when you perform pull from remote.

fatal: refusing to merge unrelated histories

This happens when the synchronization fails when the histories of the remote vary with local. In such cases, its a best practice to perform --allow-unrelated-histories

git pull origin master --allow-unrelated-histories

If you face the same issue on a different branch, you can replace master with the name of the branch in the --allow-unrelated-histories command mentioned above.

Stage Files to be pushed to GitHub

Add the file to be pushed to the GitHub by typing the following command.

git add main.py

In case you have multiple files in your existing project you can type in the following command to stage all of them at once so that they can be pushed to GitHub.

git add .

Commit the changes made to files

After staging all the files you wish to push, you must commit those changes with a descriptive message about what the changes that are going along with this commit. For example type the following command to commit your initial changes.

git commit -m "My Initial Commit in git"

The above command commits all the changes to the git with the message argument -m that has a one liner description My Initial Commit in git about the commit. Note the usage of " (double quotes) for the commit message. This will allow you to write descriptive messages separated by spaces. If you use ' single quote, you can only enter one word description such as init.

Push Changes to GitHub

Once you commit the changes, you finally have to publish the changes to GitHub. This activity is often referred to as Push. Type the following command in the terminal to reflect/ push your changes on to the remote GitHub repository.

Command to Push existing Code to GitHub

git push origin master

Its worth pulling the changes by running the command mentioned in the section to Sync your local with remote repository when you are working on a repository that has multiple contributors actively pushing code changes to. Also it is highly recommended to branch out and create a pull request. This is a little bit advanced concept which I would suggest you to go through this help document to understand how to create a branch and then create a pull request to master

To Summarize, Sync before you push and resolve merge conflicts on your local repository

Check the repository updated with latest changes

After the code gets successfully pushed to the remote branch, navigate to your new repository that you have created and pushed the changes to https://github.com/your-awesome-username/my-first-repo-on-github and ensure your changes got reflected there.

Make Changes to the main.py

Make any change to the main.py file like add another new line that prints your name. Save it.

List the file names that have a Change using Git

Type the following command to see the list of files that shows files that are different that that of the ones present on the remote.

git diff --name-only

The above command only shows the file names that have changes made on your local working branch.

List the content changes made across all files if any

In order to display the line by line changes in all the files wherever the changes are made, type the following command in the terminal.

git diff

If you have too many changes the command terminal will keep on scrolling as long as you hit enter key. To exit out of the displayed changes, hit q on the keyboard.

Once you are done with a look at the differences and are confident that they could be pushed, follow these steps in the order mentioned below.

Forcefully undo all changes in git

At any point, if you are not satisfied with the changes and want to start afresh, type in the following command to forcefully undo all the changes in your local repository.

git reset --hard

Quick Reference Cheat Sheet

If you found this article useful please bookmark ๐Ÿ“‘ this page for your future references. Once you become expert at managing repositories with git, the following full command snippet will become handy. This cheat sheet ๐Ÿฆ„ to add existing repository to git has step by step approach to add and push an existing code base to a new GitHub repository.

echo "# my-first-repo-on-github" >> README.md
git init
# Create New repository on GitHub
# git remote add origin url
git remote add origin https://github.com/your-awesome-username/name-of-your-repository.git
git remote -v
git pull origin master
# or in case of fatal: refusing to merge unrelated histories
git pull origin master --allow-unrelated-histories
git add .
git commit -m 'init'
git push origin master
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments