What is git blame & git clean

Amit Prajapati
MindOrks
Published in
5 min readFeb 7, 2020

Today’s Inspiration

“Change is the end result of all true learning”.

Let’s get started… 👨‍💻

In Git, we know sometimes, someone can edit/delete something, after that the project starts popping up errors everywhere.

The first action is finding the error, then if your team are more than four developers, spotting who introduced(by checking commits) the error could be painful and this is the second action. Knowing who did it. Luckily, we can use a not know(for beginners) command git blame.

Basically, we execute git blame path_of_the_file_where_the_error_is or directly the name of the file.

Git blame is an awesome tracking command for Git. It shows the author information of each line of the project’s last modified source file. You can find the author name, author email, the commit hash etc of the last modified source file line by line. You will see shortly what I mean practically.

e.g: I have a repository with commit.

repo with commit

Suppose, I or someone changed the files in past. When trying to run the application we got an error. If you want to know who changed the code with the date and time, you can do it with git blame.

index.html file

When executing git blame command, you can see the name, time and date in each line of code.

As you see, we have an error in line number 6, forget to add ‘/’ in <p> tag. I am responsible for this error… 😐

With this command, we can figure out the error quickly.

There are various options we can use with git blame command.

options available in blame command

Let use some of these options:

  • -L <start>,<end>

Annotate only the given line range. May be specified multiple times. Overlapping ranges are allowed. The <start> and <end> is the range that you have to provide. e.g: -L 2, 8 . This will display the line ranging from line 2 to line 8.

option -L <start>, <end>
  • -e, --show-email

If you want to view email id instead of name then you have to use -e option.

option -e
  • -f, --show-name

If you also want to view the file name with name and timestamp then use -f option.

option -f

Aliasing Git Blame

Some programmers don’t like the word ‘blame’, because of the negative connotation ‘blaming someone’ brings with it. Also, the tool is rarely (if ever) used for blaming someone, but rather to ask for advice or understand the history of a file. Therefore, sometimes people use an alias to change git blame to something which sounds a bit nicer such as git who, git history or git praise. To do that you simply add a git alias like this:

git config --global alias.who blame
change blame to who

Keep your git directory clean with `git clean` command

Do you have your git directory full of untracked files and it starts to bothers you when you are picking the changes for commit? Well, I have a tip for you!

The command git clean operates on untracked files. Untracked files are files that have been created within your repo's working directory but have not yet been added to the repository's tracking index using the git add command.

This is a builtin command to clean up the untracked files. Be careful with this one, it deletes files permanently!

e.g: These are the untracked files.

Untracked files

As you see we have 3 untracked files.

Let’s delete the bye.html file because we don’t need this file anymore.

fatal error

At this point, executing the default git clean command may produce a fatal error. The example above demonstrates what this may look like. By default, Git is globally configured to require that git clean be passed a "force" option to initiate. This is an important safety mechanism. When finally executed git clean is not undo-able. When fully executed, git clean will make a hard filesystem deletion, similar to executing the command line rm utility. Make sure you really want to delete the untracked files before you run it.

So, to delete this file we have to use -f (force) option.

  • -f or --force

The force option initiates the actual deletion of untracked files from the current directory. Force is required unless the clean.requireForce configuration option is set to false. Let us now execute git clean in our repo.

file deleted

The command will output the files that are removed. You can see here that bye.html has been removed. Executing git status at this point or doing a ls will show that untracked_file has been deleted and is nowhere to be found. By default git clean -f will operate on all the current directory untracked files.

  • -f -d

If you also want to remove directories, run git clean -f -d <directory_name> .

  • -n

The -n option will perform a “dry run” of git clean. This will show you which files are going to be removed without actually removing them. It is a best practice to always first perform a dry run of git clean. We can demonstrate this option in the demo repo we created earlier.

list of files and directories

The output tells us the will be removed when the git clean command is executed. Notice that the directories are also listed here with -d option. By default git clean will not operate recursively on directories. This is another safety mechanism to prevent accidental permanent deletion.

Hope you will learn a new topic from this article.

Thank you & Peace. ✌

--

--