4 Advanced Git Commands That Will Save Your Time
Tags: git, devtips, showdev, productivity
Published: 2019-04-26 12:51:10

---
tags: git, devtips, showdev, productivity
title: 4 Advanced Git Commands That Will Save Your Time
published: true
description: Git is a must-have to control the changes of multiple developers. Here are 4 advanced Git commands you should know to simplify the working process.
canonical_url: https://www.codica.com/blog/useful-git-commands-that-save-time/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/34u1fup30e5uv7ntexk0.jpg
---

*This article was originally published on [Codica Blog](https://www.codica.com/blog/useful-git-commands-that-save-time/).*

When we talk about any IT project development, we definitely cope with a version control system. One of the most popular is Git.

Using Git, you track and compare changes in project directories, manage and adjust the working process among multiple software engineers. It gives you an opportunity to automate and accelerate many processes and to get a full explanation of the question: Who did what and when?

Today, we want to pay your attention to **4 Useful Git Commands** that will make your life easier. They save your time, automate your process and allows to avoid a massive routine work.

So, let’s get down to business.

## <code>1. git branch --merged | egrep -v "(*|master|dev)" | xargs git branch -d</code>

This set of commands is a magic wand and lifesaver to delete already merged branches. Also, it allows you to indicate those important branches (like master or dev) that should not be removed. 

Let’s take a closer look at the example: 

Let us assume that you have lots of branches already merged into the branch master. You want to delete all of them specifying important ones like master and dev. The good news is that here is the solution to your problem: 

```
$ git branch --merged | egrep -v "(*|master|dev)" | xargs git branch -d

Deleted branch feature/one (was 949df0d).
Deleted branch feature/three (was 450980a).
Deleted branch feature/two (was 1ac4223). 
```

Let’s briefly discuss what is going on here: 
<code>git branch --merged</code>. This command line lists all the merged branches
<code>egrep -v "(^\*|master|dev)"</code>. This command line allows you to pass those important branches as arguments that should not be removed. 
<code>xargs git branch -d</code>. This command line processes the first two operations and actually deletes the merged branches.

## <code>2. git clean</code>

This command gives you an opportunity to remove untracked files that are not added to the staging area.

Let’s have a look at the example and see how it works:   

Let us assume that you are developing a particular functionality, you first create a directory to store the project files there. For example, you create a new folder called dogs and insert an image into this directory.
```
$ mkdir dogs
$ cp ~/Downloads/birman.jpg ./dogs
```
By the time some functionality is done, you realize that you need to delete particular project files. To remove these files, first you need to check your working directory for changes:
```
$ git status -s
?? dogs/
```
Now you see what files and directories were changed. Finally, you can delete all the files using the following command:
```
$ git clean -fdx
Removing dogs/
```
The truth is that you will use <code>$ git clean -ndx</code> and <code>$ git clean -fdx</code> commands predominantly. 

## <code>3. git pull --rebase --autostash</code>

This Git command gives you an opportunity to pull in the commits of another engineer and rebase your changes on top.

Let’s define what I mean by the following example: 

Let us assume that you begin the development by creating a new file (i.e. <code>new-changes.txt</code>), adding it to the staging area and committing all the changes.
```
$ touch new-changes.txt
$ git add new-changes.txt
$ git commit -m "New changes"
```
Then you create another file (<code>another-change.txt</code>) without adding it to the staging area. You log the history of changes and see that the first staged file is stashed to the top.
```
$ touch 'another-change.txt'
$ git log --oneline

c23f82a (HEAD -> master, origin/master) A lot of changes
ffe7aa0 birman
0a074ca hello, world
cd2639b Initial commit
```
If there are any uncommitted changes, you need to stash those changes first, then pull remote updates, and pop your stash to continue your work. This sounds tedious, but Git provides you with an additional <code>--autostash</code> option to automatically stash and pop your uncommitted changes.
```
$ git pull origin master --rebase --autostash -v
```
Now we can check that our file <code>one-more-change.txt</code>, that was not added to the staging area, did not disappear and automatically went in and out of the stash.
```
$ git status
?? one-more-change.txt 
```

## <code>4. git reflog</code>

This command allows you to list all the commands that were executed and then recover particular commits. 

To understand how this command works, I have prepared a clear case for you.

Let us assume that something went wrong and all the earlier created changes disappeared. To check the commit history, you need to use <code>$ git log</code> command: 
```
$ git log --oneline 
cd2639b (HEAD -> master) Initial commit
```
The terminal’s fatal output proves this fact. Initially, you need to get the list of the commands’ history by using the following command. 

Of course, we can pull in the changes from a remote repository, but we didn’t push our feature and all the changes were lost. Fortunately, here comes <code>$ git reflog</code> for help:
```
$ git reflog 
cd2639b (HEAD -> master) HEAD@{0}: reset: moving to HEAD~9
e8e8b2f HEAD@{1}: commit: [Feature] big feature
b7b2808 (origin/master) HEAD@{2}: pull origin master: Fast-forward
...
```
Now we can see that someone has deleted our commits or used Git commands incorrectly. The good news is that we can use <code>$ git reflog</code> to get back to the state before <code> $ git reset </code> was executed. 

All we need is to refer to the following commit index and use <code>$ git reset</code> command:
```
$ git reset e8e8b2f --hard
HEAD is now at e8e8b2f [Feature] big feature
```
## Conclusion 

So, we have discussed advanced Git commands that can become a helping hand in an hour of need. They are not meant to be used every day but are extremely valuable for some specific tasks.

For further reading, please check our article: **[4 Useful Git Commands That You Should Know](https://www.codica.com/blog/useful-git-commands-that-save-time/)**.