Cover image for 4 Advanced Git Commands That Will Save Your Time

4 Advanced Git Commands That Will Save Your Time

codicacom profile image Codica Updated on ・4 min read

This article was originally published on Codica Blog.

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.

1. git branch --merged | egrep -v "(*|master|dev)" | xargs git branch -d

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:
git branch --merged. This command line lists all the merged branches
egrep -v "(^*|master|dev)". This command line allows you to pass those important branches as arguments that should not be removed.
xargs git branch -d. This command line processes the first two operations and actually deletes the merged branches.

2. git clean

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 $ git clean -ndx and $ git clean -fdx commands predominantly.

3. git pull --rebase --autostash

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. new-changes.txt), 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 (another-change.txt) 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 --autostash option to automatically stash and pop your uncommitted changes.

$ git pull origin master --rebase --autostash -v

Now we can check that our file one-more-change.txt, 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 

4. git reflog

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 $ git log 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 $ git reflog 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 $ git reflog to get back to the state before $ git reset was executed.

All we need is to refer to the following commit index and use $ git reset command:

$ git reset e8e8b2f --hard
HEAD is now at e8e8b2f [Feature] big feature


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.

Posted on by:

codicacom profile



A team of web developers building great products with Ruby on Rails, React, Angular and Vue.js. Our Github: https://github.com/codica2


markdown guide