GitHub is used by 31 Million developers worldwide. If you are working on a private or open source project GitHub always comes handy to keep tracks of your work. GitHub uses Git for version control. In this GitHub tutorial you will see basic git commands.
Table of Contents – GitHub Tutorial
Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source-code management in software development, but it can be used to keep track of changes in any set of files.Wikipedia
Now if you are wondering what is version control, it is what you do to manage changes in your program or document. In this article we will focus on GitHub basics — creating repository on GitHub, doing your stuff on your computer and uploading the changes to GitHub.
As you can see this part hear includes information about creating a GitHub account. If you already have an account you can skip this part and proceed to creating repository. However, if you wish to create account please follow these instructions,
- At first, please go to github.com,
- You may create account from the landing page at GitHub. You can also go to the account creation page by clicking up Sign up. Make sure to verify your email after you create an account.
Now that you have an account you are good to go.
- First of all, please login with your credentials if you have not already,
- After that, on the homepage you will see some buttons like New, New repository and Start a project, please click any of them to create a new repository,
- Now, please write down a name for the repository and write a short description of what is the repository about. For example,
Note: While naming something we follow some simple techniques to make the names better. Like if you name something myfirstrepo, writing it myFirstRepo (Camel case notation) will make it easier to read. You can also go with this — my_first_repo (Snake case notation) format.
- Here comes privacy. This is the part where you decide whether the repository will be public or private. Please choose the option you want,
- This part here asking whether you want to add a readme file to your repository. GitHub usually asks readme for every repository created. Basically readme is where you want to tell the user what your project about, configuration, requirement and other things. You can check the option if you want to create a readme. If you do, a simple readme file will be created using the repository description which you will be able edit anytime.
- There’s another two option you will see below the readme setting. First one is to add a
.gitignore. In case you are wondering what is
According to Mureinik’s answer on Stack Overflow
.gitignoretells git which files (or patterns) it should ignore. It’s usually used to avoid committing transient files from your working directory that aren’t useful to other collaborators, such as compilation products, temporary files IDE create etc.
- Another one is license, you can include an open source license in your repository to make it easier for other people to contribute. If you wish to learn more about licensing a repository, I suggest you take a look at this GitHub help article.
- At the end, click on Create repository .
You have successfully created a repository. Time to look whats inside 🎁.
# Publish your work
Now that you have created a repository let’s publish something there. But before that please download and install Git on your computer and configure it executing these git commands on command prompt/terminal,
git config --global user.name <your_github_username>
git config --global user.email <your_github_email>
Recommended read: How to Install Git on Linux, Mac or Windows
Now let’s get publish our codes to GitHub,
- At first please create a directory/folder in your local machine,
- Now please open terminal or command prompt in the directory,
- After that please execute
git initto initialize an empty repository in that directory to initialize an empty repository in that directory.
- Now you will have to tell git where to store these files of your local machine – your GitHub repository. To do that, please execute
git remote add origin https://github.com/<your_username>/<repository_name>.git
- After that please create a / some files that you want to push (store) in the repository.
- Now in the terminal or command prompt execute
git add filename.extto add the file to staging area. For multiple files,
git add file1name.ext file2name.extand to add all the files of the directory to staging area, execute
git add .
- After that please execute
git push origin masterto push the code from staging area to github repository.
- That’s all! Check your repository and you will see the update.
Recommended read: Understanding the GitHub flow
Note: Here we are pushing to master because it is the main branch and which is automatically created in the process of creating a repository. If we had another branch and we needed to add things to that branch, we could have executed,
git push origin <branch_name>
Recommended read: Creating and deleting branches within your repository
Now, let’s say you have edited a file through GitHub’s website in your repository, you didn’t edit in your local machine and pushed it to GitHub repository like we did. Then if you try to push file(s) from your local machine like the way above, repository won’t accept it! Because the branch or your local machine and the GitHub repository are not equal. To make this right you will have to execute,
git pull origin <branch_name>
This will fetch repository from GitHub and will remove possible conflicts. But sometimes if the two branches that have no common base by default then you will have to add
--allow-unrelated-histories flag to the pull command to force the merge. For example,
git pull origin <branch_name> --allow-unrelated-histories
The git commands shown in this GitHub tutorial are very basic and ‘all time needed’ type of commands. But there are plenty of command you need to know if you want to master git. For example,
git branch and so on. Take a look at the GitHub cheat sheet to learn these commands.
Disclaimer: Featured image used in this article is from https://github.blog/2018-06-04-github-microsoft/