CodeNewbie Community

Cover image for Common handy git commands
Shanker
Shanker

Posted on

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
Enter fullscreen mode Exit fullscreen mode

Setup global username

$ git config --global user.name "Your name"
Enter fullscreen mode Exit fullscreen mode

Setup global email

$ git config --global user.email "Your email"
Enter fullscreen mode Exit fullscreen mode

git version

Displays the version of the git you are working on.

Usage:

$ git version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ git init <repo name>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
$ git add * (adds all the modified and new files to the staging area)
Enter fullscreen mode Exit fullscreen mode

git commit

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

Usage:

$ git commit -m “commit message”
Enter fullscreen mode Exit fullscreen mode

git status

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

Usage:

$ git status 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

create a new branch

$ git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

delete a branch:

$ git branch -D <branch_name>
Enter fullscreen mode Exit fullscreen mode

git checkout

Checkout command is used to switch between branches.

Usage:

$ git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

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

$ git checkout -b <your_new_branch_name>
Enter fullscreen mode Exit fullscreen mode

git log

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

Usage:

$ git log
Enter fullscreen mode Exit fullscreen mode

To see git logs in one line

$ git log --oneline
Enter fullscreen mode Exit fullscreen mode

git remote

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

Usage:

$ git remote add <url>
Enter fullscreen mode Exit fullscreen mode

git push

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

Usage:

$ git push origin <your_branch_name>
Enter fullscreen mode Exit fullscreen mode

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

Usage:

$ git push --set-upstream origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

git pull

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

Usage:

$ git pull <remote_url>
Enter fullscreen mode Exit fullscreen mode

git stash

Stash command temporarily stores your modified files.

Usage:

$ git stash 
Enter fullscreen mode Exit fullscreen mode

You can view all of your stashes

$ git stash list 
Enter fullscreen mode Exit fullscreen mode

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

$ git stash{3} apply 
Enter fullscreen mode Exit fullscreen mode

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  
Enter fullscreen mode Exit fullscreen mode

git show

Show command git show display details about a specific commit.

Usage:

$ git show <your_commit_hash>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

git merge

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

Usage:

$ git merge <diff_branch_name>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

git bisect

bisect command helps to find bad commits.

Usage:

to start the git bisect

$ git bisect start
Enter fullscreen mode Exit fullscreen mode

let git bisect know about a good commit

$ git bisect good a123 
Enter fullscreen mode Exit fullscreen mode

let git bisect know about a bad commit

$ git bisect bad z123
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

git pull --rebase

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

Usage:

$ git pull --rebase
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

git tag

tag command is used to manage the release.

Usage;

$ git tag -a v1.0.0
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

git verify-tag

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

Usage:

$ git verify-tag <tag>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

to compare two branches:

$ git diff <source branch> <target branch>
Enter fullscreen mode Exit fullscreen mode

git citool

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

Usage:

$ git citool
Enter fullscreen mode Exit fullscreen mode

git mv

mv command is used to rename a git file.

Usage:

$ git mv <old-file-name> <new-file-name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

git help

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

Usage:

$ git help <git_command>
Enter fullscreen mode Exit fullscreen mode

git whatchanged

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

Usage:

$ git whatchanged
Enter fullscreen mode Exit fullscreen mode

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

Buy Me A Coffee

Discussion (0)