Common handy git commands
Tags: git, devops, developers, github
Published: 2021-07-09 09:55:37

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

```bash
$ git config -l
```

Setup global username

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

Setup global email

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

#### git version

Displays the version of the git you are working on. 

Usage:

```bash
$ 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:

```bash
$ git init
```

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

```bash
$ 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:

```bash
$ 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:

```bash
$ git add file_name (adds a single file to the staging area)
```

```bash
$ 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:

```bash
$ git commit -m “commit message”
```

#### git status 

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

Usage:

```bash
$ 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

```bash
$ git branch 
```

create a new branch

```bash
$ git branch <branch_name>
```

delete a branch:

```bash
$ git branch -D <branch_name>
```

#### git checkout

Checkout command is used to switch between branches. 

Usage:

```bash
$ git checkout <branch_name>
```

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

```bash
$ git checkout -b <your_new_branch_name>
```

#### git log

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

Usage:

```bash
$ git log
```

To see git logs in one line 

```bash
$ git log --oneline
```

#### git remote

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

Usage:

```bash
$ git remote add <url>
```

#### git push

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

Usage:

```bash
$ git push origin <your_branch_name>
```

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

Usage:

```bash
$ 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:

```bash
$ git fetch 
```

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

Usage:

```bash
$ git pull <remote_url>
```

#### git stash

Stash command temporarily stores your modified files.

Usage:

```bash
$ git stash 
```

You can view all of your stashes 

```bash
$ git stash list 
```

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

```bash
$ 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:

```bash
$ git shortlog  
```

#### git show

Show command git show display details about a specific commit.

Usage:

```bash
$ 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:

```bash
$ git rm <your_file_name>
```

#### git merge

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

Usage:

```bash
$ 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:

```bash
$ git rebase <branch_name>
```

#### git bisect 

bisect command helps to find bad commits. 

Usage:

to start the git bisect 

```bash
$ git bisect start
```

let git bisect know about a good commit

```bash
$ git bisect good a123 
```

let git bisect know about a bad commit

```bash
$ 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:

```bash
$ 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:

```bash
$ 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:

```bash
$ 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:

```bash
$ git blame <your_file_name>
```


#### git tag

tag command is used to manage the release. 

Usage;

```bash
$ 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:

```bash
$ git verify-commit <commit>
```

#### git verify-tag

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

Usage:

```bash
$ 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:

```bash
$ git diff HEAD <filename>
```

to compare two branches:

```bash
$ git diff <source branch> <target branch>
```


#### git citool

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

Usage:

```bash
$ git citool
```

#### git mv

mv command is used to rename a git file.

Usage:

```bash
$ 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:

```bash
$ git clean
```

#### git help

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

Usage:

```bash
$ git help <git_command>
```

#### git whatchanged

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

Usage:

```bash
$ 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 ->


<a href="https://www.buymeacoffee.com/sn04" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>