Git and GitHub Basics: Part 2— git commands
This is the continuation of my git and GitHub Basics tutorial. In part 1, we talked about git and tracking files in git. In this tutorial, we will learn to create a repository in GitHub and push the code of our GitKickStarter folder to this repository (repo).
What is a repository?
A repository is storage location for a single project. A repository, (usually shortened as repo), is a collection of files, images, sub-folders or anything else that a project needs. In GitHub, a repository is hosted on the site itself.
How to create a repository?
To create a repository, you need to first sign in to your GitHub account, then on the top right click the + and click on Add repository
A new page is displayed where you can add the name of your repository. We will add the name GitKickStarter in our repo name.
You can choose to add the repository as Private or Public. A private repo can only be seen and used by the user or those who have been given permission by the user. A public repo can be accessed by everyone on GitHub.
There are also options to add a README file, a .gitignore file and a license. We will be talking about the README and .gitignore files in later paragraphs.
With this, our repository has been created. The next step involves connecting our repository of GitHub to our GitKickStarter folder in our computer.
Side note: All the repositories on GitHub can be accessed by a unique URL. https://github.com/prabodh44/GitKickStarter.git is the URL for our GitKickStarter repo.
The URL takes the form of github.com/<GitHub_username>/<repo_name>.git
Now we can connect the project in our computer to the repository that is hosted in GitHub. To do this type the following code in git bash
git remote add origin https://github.com/prabodh44/GitKickStarter.git
The git commit commands captures the state of the project at that point in time. git commit takes a snapshot of the project at that particular point in time and saves it to the local repository, i.e. in your own computer.
You have to explicitly tell git which changes to make a snapshot of. This is done by the git add command discussed in the previous post
Here is how to write a commit in git
git commit -m <commit message>git commit -m "Added some changes to the file"
Things to remember:
- git commit takes the snapshot of changes to the project
- what “shots” to take must be explicitly defined
- the “shots” are saved in your own computer
git add along with git commit are some of the most frequently used programs.
The git push command is used to “push” commits from our computer (local repository) to a remote one. The git push command transfers the changes done in the local repository to the remote repository.
Note: To be able to push the changes to the remote repository, all the changes in the local repository need to be committed first
Here is how you push code to the remote repository
git push <remoteName> <branchName>
In the GitKickStart project, the new changes are committed before they are ready to be pushed.
In the above code, the command
git push origin master is used. The command pushes the code that is in the local repository branch called
master to the remote repository which is denoted by
- Create a repository on GitHub
- Commit the changes of the code
- Push the committed changes from the local repository to the remote repository