Git/GitHub commands and their uses

Photo by Yancy Min on Unsplash

Git/GitHub commands and their uses

GitHub is considered to be a programmer's identity and without it one cannot express their coding skills. GitHub is a cloud-based hosting service for your git repositories, which is a version control system. Now, a version control system like git is a tool/software that programmers or teams use to keep a record of their code repositories and all the changes and deletions done to those repositories(and much more useful stuff). As a programmer, one can make projects and push those to GitHub using Git for organizations and hiring managers to see.

Initializing a git repository

After you have downloaded git, open the git bash terminal in your project folder by right-clicking in the folder. The bash terminal is like the Linux terminal, but it has a lot of added features.

first, you need to need set up a username and email id

git config --global user.name <username>
git config --global user.email <user email>

Now, initializing the repo and opening vs code

git init #initialises repository
code . #opens vs code

Staging a file

for adding a file to the staging area

git add filename.filetype
git add -A #for adding multiple files

Committing a file to git

for making a commit for a staged file

git commit filename.filetype

Once you enter the above command, bash will open the vim editor where you will be required to put a message denoting the purpose of the commit. First, you need to press i and then you can type your message. Once you have done that, you need to press ESC and then :wq to exit the vim editor.

Now, if you want to commit multiple staged files with a single message

git commit -m "commit message"

If you want to commit a file or all the files that are not staged yet, you can use the following command

git commit -a -m "commit message"

git log and git diff

Now, if you want to see a record of the changes you made to your repository, you can use

git log
git log -p -5 #if you want to see the last 5 commits

There is another command in git that lets you compare a committed/staged file with the same file in your working directory. This is useful when you have made changes to the file in your working repo and you want to see what changes you made specifically, that differs from the committed/staged version.

git diff
git diff --staged #checks the difference between the staged version and last committed version of the file

Removing a file

if you want to remove a staged/committed file from git, you first need to unstage the file and then remove it completely from the working repository

git rm --cached filename.filetype #for unstaging the file
git rm filename.filetype #for removing the file from working repo

Now, the reason behind not removing a file directly from git if it is committed is that, when we remove/delete a file, it is considered a commit. Therefore, first, we need to commit the unstaging and then remove it.

Creating Branches

To create a branch, separate from the master branch, we need to use the following command

git branch branchname
git branch # to view all branches

to switch to a different branch

git checkout branchname

if we want to merge a branch to the master branch

git merge branchname

if you want to clone an existing remote repository

git clone repo_url

you will get the repo url from the Code->clone section on GitHub

if you want to push a branch into a remote repository

git push origin master #for the master branch
git push -u origin branchname #for other branches

So, that's it for this article. It is an informative article and you can use it for your projects as well. If you are new to GitHub and didn't know about the commands, then you haven't understood the terminologies that I have used(like stage, commit etc). This is very natural and you need to understand git to work with these commands. I will suggest you to go to this free YouTube course on Git & GitHub Here. This will get you sorted with git and it's super easy, trust me ;)


If you have reached the end of this article, then I feel most obliged. Hope my article has brought something of value to you ;). Feel free to like or not like this article. And pls share your thoughts as comments, if you have any.

If you are a fan of newsletters, you can subscribe to my newsletter, given below.

You can also follow me on Hashnode and other platforms if you like my writing.

Help your friends out by sharing this article. Cheers!