Today, Git and GitHub have become a standard in the Tech Industry. These tools are used to manage projects in a better way and primarily, increase the efficiency while collaborating on a project.
Alright! First we need to understand that Git and GitHub are two different tools which serve different purposes.
What is Git?
Git is an open source distributed Version Control System(VCS). Now, what is a VCS? A Version Control, records changes made to the files over time, and hence, it helps to maintain different versions of the files which helps the developers to refer to it at any point in time.
What is GitHub?
GitHub is a place where all the developers can store their repositories. GitHub enables multiple developers to work on the same project while working from different places.
A Small Tip! If you’re a student, GitHub gives a GitHub student pack that enables you to maintain unlimited private repositories and allows you to host them on GitHub pages for free.
Setting up Git and GitHub
The first step is to download and install the latest Git version that suits your operating system. You can use this link https://git-scm.com/downloads. Once, you’ve installed Git on your local machine, you will need to create a GitHub account where all your projects can be stored. Click on this link https://www.github.com to go to GitHub.
After you have your GitHub account handy, you’ll need to configure Git on your computer in order to work with your projects . To do that right-click on your PC, open git bash and type the following commands. In a Mac/Linux machine you can directly proceed with the default terminal.
git config --global user.name “your GitHub username”git config --global user.email “your GitHub email id”
Note: The “ — global” option links the username and email globally to all your current and future projects at once. Otherwise, you would have to configure your username and email every time you push your repository to GitHub
Creating your first git repository
Now that the configuration part is over, let’s create our first repository on GitHub. Once you’ve logged on to GitHub you can click the New button to create a new repository. After clicking the new button you will have to name your repository and then click the Create Repository button. Tadaa!! You’ve now created your first git repository. Now, let’s learn how to move your files into your GitHub Repo from your local machine.
Create a repository on your Local Machine
Open a New folder on your local machine, move into the folder, right-click and open git-bash. Inside the terminal type the following command.
Note: Whenever I use the word repository/repo, it refers to a project folder that you’re working with.
Once you type the command you will see a message that says “Initialized an empty git repository in …”. That means, the current folder is a local git repository. Now, you need to link this local repository on your computer with the repository that you have created previously on GitHub.
Linking your local repo and GitHub Repo
You can link your local repository and the GitHub repository by using the following command.
git remote add origin <your repo url>
Example: git remote add origin https://github.com/<YouUsename>/TestRepo.git
You can find the repository URL inside your repository as shown below if it’s empty. In case, if you want to connect a repository that already has some data ( like the readme.md file) then you can find the URL by clicking on the Code button available on the repo dashboard.
Note: Make sure that you’re using the https url and not the SSH url
Now let’s make some changes to our repository and see if the changes are reflected on the GitHub repository or not.
You can check the status of your repository at any point in time using the git status command. Now, add a dummy text file to the repository. Then, type the git status command again in your git-terminal. You’ll see a message similar to the below one.
This message means that the file is present in the folder but is not yet moved to the staging area. While using you’ll need to understand that the files are moved to a place called the staging area before they are moved to GitHub.
To move the files to the staging area, use the following command
git add <filename> git add . # . adds moves all the files
The git add . command will move all the files to the staging area. Now, when you type the git status command again,you’ll notice that file name changes to green color. It means that now your files are successfully moved to the staging area
Now your files are moved to the staging area which is one step before committing your files to your repository. Now you’ll need to commit your changes that you’ve made to the repository.
To commit the changes made to your repository, you need to type the below command
git commit -m “<Your commit message>”
The command will show changes made along with your commit message.
Now when you check your repo on GitHub, you’ll find that the repo is still empty or the same as before. To find the changes that you made to local repo on GitHub you’ll have to type one final command as shown below.
git push origin master
When you break the command down you’ll understand it better. The first argument ‘origin’ is the name of the remote that we used along with the url in the beginning to link the repos. The second argument ‘master’ is the name of the branch that you’re currently working with. The default branch is set to master.
Voila! You pushed your first project to GitHub and how easy it was!Do give a clap if you liked the article!
I’ll explain about branches, handling pull requests and merge conflicts while working with multiple people on a project in my coming article.