A quick step-by-step guide through important Git commands—

This section covers all major and oughta-know Git commands with one-liner explanations that can be handy while dealing with GitHub from start to end.

It comprises of commands for managing your own local repository (folder) with your remote repository (GitHub). It also includes alternate commands and quick fixes to major impediments one may come across.

To check git is installed or not:

If not installed, get it from https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Note: Create a folder locally, open cmd -> /path_to_folder

Now let’s begin,

To make a repo locally —

It’ll generate a hidden file named .git

To get a project available remotely in our local repo

<copied_link> is the link copied from the remote repo on GitHub.

To push our local repo (project) in a new repo on GitHub

Here the <copied_link> will be from our new repo created on GitHub, and then,

For error - “fatal: remote origin already exists.” -> use some other name (eg: new_branch) instead of origin, OR

To update remote repo as per the changes made in the local repo —

Change the file(s) from unstaged area (RED) to staged area (GREEN) before committing the changes using following command.

To stage new files and modifications, without deletions

To stage modifications and deletions, without new files

To stage all changes

To check the files in staged area or the changes made in the project

To see exactly what changes are made

To commit the changes

To include all freshly changed files in the commit

Above command opens the editor where we have to type the commit message & close it.

To add/change in message in the same commit rather than generating new commit

It’ll open the vim editor where we can make changes and then to exit, press esc -> type :wq -> press enter

We can also open the vim editor using

To sync remote project with local

To sync local project with remote

To set the author name & email address resp to be used with commits

To check all the commits made remotely

and for just last 5

To check exactly what all changes made remotely

hash_id can be seen in logs

Branches stuff —

Initially, we are in the master branch

To check current branch

To create a new branch

To change the current branch

To rename a branch

To delete a branch

Make sure master branch is the current branch and then,

To merge a branch to master branch

Make sure master branch is the current branch and then,

To check the history while undoing changes

To undo the last commit(change)

The above command will create a new commit & record the “undo” in the history.

To rewrite the history so that it looks as though you never made that commit-

ORgit reset --hard HEAD^ (same as ~1)ORgit reset --soft <hash_id of the very next commit from log>

git reset changes, at minimum, where the current branch (HEAD) is pointing

— soft: uncommit changes, changes staged.

— mixed (default): uncommit + unstage changes, changes are left in working tree.

— hard: uncommit + unstage + delete changes, nothing left

In simple words, if we’re on branch master with this series of commits:

- A - B - C (master)

-> git reset — soft A will show B and C’s stuff in green (staged and ready to commit)

-> git reset — mixed A (or git reset A) will show B and C’s stuff in red (unstaged and ready to be staged (green) and then committed)

-> git reset — hard A and you will no longer see B and C’s changes anywhere (will be as if they never existed)

To remove a file from staged area

ORgit rm -cached <filename>ORgit restore --staged <filename>

To undo all the changes made to a file, go to the branch

Make sure to remove file from staged area, and then,

To exclude some files while adding to staged area, we create a .gitignore file using touch .gitignore in the master branch and then we can add filename and /dirname in this file as text.

After this, you should know how to do things straight away and what to look for in any case of complications. I hope it was helpful:)

Tech enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store