Git Commands that you should know.

Midhun GeorgeMidhun George
4 min read

One of the most used version control systems that help developers to track changes when working on projects as a team is Git. Whether you work on a team or by yourself, it has now become an integral part of code change management. Merely closing this section felt a pain because mastering Git can be tricky due to the commands, and features of Git make it a bit difficult, especially for newcomers. Here I am trying to list most used and powerful git commands with it's usecases.

Initialization

To initialize a new Git repository in the current directory, run the following command:

git init

This creates a hidden .git directory in the current directory that tracks changes to your code.

Cloning

In the most common usage, git clone is used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. Note that the original repository might be running on the same machine, from a file accessible using a supported protocol. The git clone command is to get a copy of an existent Git repository.

git clone <repository url>

This creates a new directory on your computer with a copy of the repository.

Adding Changes / Staging changes

In Git, having a modified file does not guarantee that the next commit includes that file. Instead, you must tell Git explicitly which changes should be included in the next commit from amongst your modifications. This is achieved by staging a change to the Staging Area.

git add <file/directory>

You can also stage all changes in the current directory by running:

git add .

This tells Git which changes you want to include in your commit.

Committing changes

To commit the changes in the staging area with a commit message, run the following command:

git commit -m "<message>"

Checking status

To check the current status of your repository, run the following command:

git status

This will show you which files have been modified, which files are staged for commit, and which files are untracked.

Viewing Changes

To view the changes between the working directory and the staging area, run the following command:

git diff

To view the changes between the staging area and the last commit, run the following command:

git diff --cached

Branching

In Git, a branch is a new/separate version of the main repository.

Branches enable you to work on disparate parts of a project separate from the main branchThis is because once our work is done, we will have to merge our branch to main project.You can even navigate and work on different branches/projects without having conflicts between them.

To create a new branch with the specified name, run the following command:

git branch <branch name>

To switch to the specified branch, run the following command:

git checkout <branch name>

You can also create and switch to a new To push changes to a remote repository, run the following command:branch in one command by running:

git checkout -b <branch name>

To merge the specified branch into the current branch, run the following command:

git merge <branch name>

Pushing changes

To push changes to a remote repository, run the following command:

git push <remote><branch>

Pulling changes

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

git pull

or git pull <remote> <branch>

Reverting changes

If you need to undo a commit, you can use the git revert command. This creates a new commit that undoes the changes made in the specified commit.

Resetting changes

The git reset command is a complex and interesting tool but here is how you can rollback a commit. This will delete the mentioned commit as well as all the follow up commits from the commit history. Git reset has three options: --soft, --mixed, and --hard.

--soft resets changes the commit history and leaves the changes in staging.

--mixed- resets your commit history and leaves the changes unstaged.

hard : Resets the commit history, un-stages the changes and delete all subsequent changes that comes after the specific commit.

If you want to reset the last commit with - soft for example then run this command:

git reset --soft HEAD~1

To reset the last commit using --mixed, run the following command:

git reset --mixed HEAD~1

To reset the last commit using --hard, run the following command:

git reset --hard HEAD~1

git reset --hard will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under Git tracking.

Alternatively, you can do the following (beware though - that removes all ignored files too)

git clean -df

git clean -xdf CAUTION! This will also delete ignored files

0
Subscribe to my newsletter

Read articles from Midhun George directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Midhun George
Midhun George