An essential guide on what is git and how to use git and github.

Mateen Kiani

Mateen Kiani

Published on Sat Jul 29 2023·9 min read

git and github

What is git?

Git is a distributed version control system developed by Linus Torvalds back in 2005. It is a free and open source software and was initially designed to make collaboration process for linux kernel development more convenient and streamlined, but later it gained so much popularity and is now the most popular version control system. By using Git you can track changes that you made in your files and it also makes collaboration with other people painless.

Why git and github?

There are so many reasons to learn these technologies:

  1. With Git every commit you make is not like saving some text in a word file rather it is saved like we have checkpoints in video games and you can go back to any point whenever you want so you don’t have to make multiple copies of your code just to make sure that if your update crashes, you can revert back.
  2. Git makes it so much easy to collaborate by using the concept of branches. You can create a master branch for the project that is always bug free and deploy able and some other branches for development. Each team can work on their own separate branch, add features, merge those new features to a single branch and eventually push it to the master branch.
  3. Why store all of your important code on a hard drive that can fail? By using github you can push your code to the cloud and don’t need to worry about it anymore.
  4. Github is free and has a huge community of people you can make your project opensource so anyone can make a contribution and you can also make some geek friends there 🙂
  5. You can make your portfolio on github and show off all of the projects that you have done. One nice way to do that is by using GitHub pages.
  6. You are gonna need it anyways so better learn it now.

Installation

I will not get into the details of installation as there is already an official, quick and easy guide on how to install it on Windows, Linux or macOS. You can check it here.


How to use Git?

When you make any change in a file you are changing it in working area not in the actual repository so you need to add it to staging area and then to the repository in the form of a commit.

Note: The terms staging area and index have same meaning and are used interchangeably.

How git works How Git works?

Now you know what git is here is how you can use it to maintain your workflow in a good way.

The very first step you need to do is initialize you repository. Simply create an empty folder add some files to it and open your command line in that directory. Type the following command to create an empty git repository or reinitialize the existing one.

git init //initialize or reinitialize a repository

You can also clone an existing repository like this

git clone https://github.com/mateenkiani/react-tutorials // clone a repository

After initialization your repository is empty and there is no file in the staging area. By executing following commands you can add files to staging area before making a commit.

git add hello.py // add hello.py file to staging area
git add -A // add new files, update modified files and remove deleted files from staging area
git add -u // update modified files and remove deleted files from staging area

You can check the current status of working tree by using git status command, it will show if there are any unstaged changes. It will also show the changes that are in staging area (index) but not commited to the repository yet.

git status // current status of the working tree

To see the all the unstaged (indexed) changes that you have made you can use git diff command, it will show all the changes since the last time you added files to staging area, you can also specify a file name to review what changes you made in a particular file.

git diff // show all the unstaged changes
git diff index.html // show all the unstaged changes in index.html file

To commit all the changes to repository you need to use git commit command. It has various options -a or –all is used to stage all the modified and deleted files to staging area but not newly created files and commit them to the repository as well.

Note: you need to specify commit message with every commit by using -m option or a text editor will open asking you for commit message and if you leave it empty commit will be aborted.

git commit -m "commit message here" // commit staged changes
git commit -a -m "commit message here" // stage and commit changes but ignores newly created files.

If you want to unstage files from index (staging area) you can use git reset. It is the opposite of git add and clears all files from staging area.

git reset // unstage all the files
git reset file.html // unstage only file.html

You can also use git reset to discard last commit and revert back to the previous one like this

git reset --hard HEAD~1 // discard last commit and checkout the commit done before
git reset --hard HEAD~3 // discard last 3 commits and revert back to the 4th last commit

To remove a file from working directory as well as index you need to use git rm . This will delete file from working directory and also from index (staging area) but you need to commit to save those changes permanently.

git rm index.html // delete index.html from staging area and working directory.

To move or rename a file you need to use git mv command. Its syntax is git mv . To rename a file use same source and destination but change the file name.

Note: After mv you don’t need to re index any change.

git mv index.html file.html // rename index.html to file.html
git mv index.html ./src // move index.html to ./src/index.html

For branching you need to use git branches command. It can be used to create a branch based on another branch or a tag its syntax is as follows:

git branch <new-branch> // create a new branch with all the contents of working directory
git branch <new-branch> f71ac24d // create a new branch based on a commit
git branch <new-branch> v1.2 // create a new branch from a tag
git branch --track <new-branch> origin/<base-branch> // create a new branch from remote branch
git branch -d <branch-name // delete a branch

You can use git switch to switch between branches like this

git switch <branch-name> // switch to specified branch

Git checkout is used to switch branches and if you don’t specify a branch name it updates files in the working tree to that in the index (staging area).

git checkout // updates the working directory with the files in the index.
git checkout master // alternate way to switch between branches

To see all the commits that you have made you can use git log.

git log // shows all the commits you made along with author name and time of commit

To update your code to a cloud storage like github you need to use git push, but before you can push you have to add a remote first with this syntax git remote add <remote-name> <repo-url>

git remote add origin <repo-url> // add a remote named origin to a remote repository
git remote -v // verify the remotes

After adding a remote you can push your files to the remote repository like this

git push origin master // pushes to a remote master branch, where origin is name of the remote you added

The opposite of push is fetch and you can fetch from a repository by executing this

git fetch origin master // fetch from a remote repository

To merge a branch with current directory use

git merge <branch-name> // merge a branch with current directory

Pull is equivalent to fetch + merge you can use it to fetch the content of remote repo and merge it as well. Its syntax is git pull <branch-name>

git pull origin master // fetch and merge from remote repository

If you want to switch to another branch but have some local changes that you don’t want to commit now you can save those changes by executing git stash.

git stash // save all the local changes
git stash pop // apply and drop all the saved changes
git stash apply // only apply all the previously saved changes

You can use git tag to mark some points as being important.

git tag <tag\_name> <commit\_id> // add a tag to a commit
git tag // display all the tags
git checkout <tag> // checkout a specific tag

Git cheatsheat

git init //initialize or reinitialize a repository
git clone https://github.com/mateenkiani/react-tutorials // clone a repository
git add hello.py // add hello.py file to staging area
git add -A // add new files, update modified files and remove deleted files from staging area
git add -u // update modified files and remove deleted files from staging area
git status // current status of the working tree
git diff // show all the unstaged changes
git diff index.html // show all the unstaged changes in index.html file
git commit -m "commit message here" // commit staged changes
git commit -a -m "commit message here" // stage and commit changes but ignores newly created files.
git reset // unstage all the files
git reset file.html // unstage only file.html
git reset --hard HEAD~1 // discard last commit and checkout the commit done before
git reset --hard HEAD~3 // discard last 3 commits and revert back to the 4th last commit
git rm index.html // delete index.html from staging area and working directory.
git mv index.html file.html // rename index.html to file.html
git mv index.html ./src // move index.html to ./src/index.html
git branch <new-branch> // create a new branch with all the contents of working directory
git branch <new-branch> f71ac24d // create a new branch based on a commit
git branch <new-branch> v1.2 // create a new branch from a tag
git branch --track <new-branch> origin/<base-branch> // create a new branch from remote branch
git branch -d <branch-name // delete a branch
git switch <branch-name> // switch to specified branch
git checkout // updates the working directory with the files in the index.
git checkout master // alternate way to switch between branches
git log // shows all the commits you made along with author name and time of commit
git remote add origin <repo-url> // add a remote named origin to a remote repository
git remote -v // verify the remotes
git push origin master // pushes to a remote master branch, where origin is name of the remote you added
git fetch origin master // fetch from a remote repository
git merge <branch-name> // merge a branch with current directory
git pull origin master // fetch and merge from remote repository
git stash // save all the local changes
git stash pop // apply and drop all the saved changes
git stash apply // only apply all the previously saved changes
git tag <tag\_name> <commit\_id> // add a tag to a commit
git tag // display all the tags
git checkout <tag> // checkout a specific tag

Like and share with friends it really helps a lot. Mention the commands that I missed in the comments below.

Happy coding 😉


Mateen Kiani
Mateen Kiani
kiani.mateen012@gmail.com
I am a passionate Full stack developer with around 3 years of experience in MERN stack development and 1 year experience in blockchain application development. I have completed several projects in MERN stack, Nextjs and blockchain, including some NFT marketplaces. I have vast experience in Node js, Express, React and Redux.