Common handy git commands

In this article I'll explain the commonly used git commands every developer, DevOps engineer should know.

git config

Git config commands gives you the information about your git configurations. It can be used to setup initial configs when you install git.

Usage:

list all the configuration for the git

$ git config -l

Setup global username

$ git config --global user.name "Your name"

Setup global email

$ git config --global user.email "Your email"

git version

Displays the version of the git you are working on.

Usage:

$ git version

git init

This command use to initialise git in a new project. Once the git is initialised for a project you can add source code to track the commits.

Usage:

$ git init

Use init with repository name will create a new project with git initialized for it.

$ git init <repo name>

git clone

The git clone command will use an existing remote repository to copy.

The only difference between the git init and git clone is, Git clone makes a copy on an existing remote repository. The git clone command internally uses the git init command first and then checks out all its contents.

Usage:

$ git clone <your project URL>

git add

Git add command adds all the new code files or modified files into your repository's staging area.

Usage:

$ git add file_name (adds a single file to the staging area)
$ git add * (adds all the modified and new files to the staging area)

git commit

Git commit command adds your changes from staging area to your local repository.

Usage:

$ git commit -m “commit message”

git status

Status command allows you to see the current branch and how many files are added/modified.

Usage:

$ git status

git branch

When you work on multiple branches in your Git repository. Git branch command allows you to manage your branches. Git branch lists all the branches in your local repository.

Usage:

list all branches

$ git branch

create a new branch

$ git branch <branch_name>

delete a branch:

$ git branch -D <branch_name>

git checkout

Checkout command is used to switch between branches.

Usage:

$ git checkout <branch_name>

you can also create and checkout to a new branch in a single command

$ git checkout -b <your_new_branch_name>

git log

Git log command helps to see the history of previous commits.

Usage:

$ git log

To see git logs in one line

$ git log --oneline

git remote

Git remote command is used to connect your local repository with the remote repository.

Usage:

$ git remote add <url>

git push

Push command is used to push your local changes to the remote repository.

Usage:

$ git push origin <your_branch_name>

Git push should have origin and upstream set up before you push code to remote repository.

Usage:

$ git push --set-upstream origin <branch_name>

git fetch

Fetch command downloads all information for commits, refs, etc. You can review before applying those changes in your local repository.

Usage:

$ git fetch

git pull

Git pull command updates your local repository with the latest content from remote repository.

Usage:

$ git pull <remote_url>

git stash

Stash command temporarily stores your modified files.

Usage:

$ git stash

You can view all of your stashes

$ git stash list

if you need a apply a stash to a branch. Below apply stash{3}

$ git stash{3} apply

git shortlog

The shortlog command gives you a short summary from the Git log command. It displays who worked on what by grouping authors with their commits.

Usage:

$ git shortlog

git show

Show command git show display details about a specific commit.

Usage:

$ git show <your_commit_hash>

git rm

rm command deletes files from your code, it deletes tracked files from the index and the working directory.

Usage:

$ git rm <your_file_name>

git merge

merge helps to integrate changes from different branches into the current branch.

Usage:

$ git merge <diff_branch_name>

git rebase

Git rebase similar to the git merge command. It integrates changes from different branches into the current branch.

The only difference is git rebase command rewrites the commit history.

Usage:

$ git rebase <branch_name>

git bisect

bisect command helps to find bad commits.

Usage:

to start the git bisect

$ git bisect start

let git bisect know about a good commit

$ git bisect good a123

let git bisect know about a bad commit

$ git bisect bad z123

git cherry-pick

Git cherry-pick command allows to pick any commit from any branch and apply it to any other branch. It doesn’t modify the commit history of a repository, new commits are applied to the commit history.

Usage:

$ git cherry-pick <commit-hash>

git archive

Git archive command combines multiple files into a single file. It is useful to create a zip of a repository.

Usage:

$ git archive --format zip HEAD > archive-HEAD.zip

git pull --rebase

When you need to do a rebase instead of merge when using git pull command.

Usage:

$ git pull --rebase

git blame

blame is used to examine a file line by line and to determine who made the changes to a file.

Usage:

$ git blame <your_file_name>

git tag

tag command is used to manage the release.

Usage;

$ git tag -a v1.0.0

git verify-commit

git verify-commit command checks the gpg signature. GPG or “GNU Privacy Guard” is the tool used in sign files and contains their signatures.

Usage:

$ git verify-commit <commit>

git verify-tag

Just like verify-commit, you can confirm a tag.

Usage:

$ git verify-tag <tag>

git diff

diff command is used to compare two git files or branches before you commit or push.

Usage:

to compare the working directory with the local repo:

$ git diff HEAD <filename>

to compare two branches:

$ git diff <source branch> <target branch>

git citool

Git citool is a graphic tool alternative of the Git commit.

Usage:

$ git citool

git mv

mv command is used to rename a git file.

Usage:

$ git mv <old-file-name> <new-file-name>

git clean

clean command is used to remove/delete all the untracked files from your working directory. To remove tracked files you can use the git reset command.

Usage:

$ git clean

git help

To get more information or help on any of the git command, you can use git help.

Usage:

$ git help <git_command>

git whatchanged

git whatchanged command is same as git log but in a raw format.

Usage:

$ git whatchanged

Thank you folks. Please do check my other articles on https://softwareengineeringcrunch.blogspot.com

If you like my article, you can support me by buying me a coffee ->

22