Tuesday, December 15, 2020

Git commands

 Command №1: git diff

git-diff is the command for you if you need to check different commits or between commits and working tree. If you’re not familiar with the concept, the working tree is the directory associated with your repository on your system.

To analyze the status of a repo git diff command is often used in addition to git status and git log.

Usage

In general, git diff is used to get the difference between two “things.” These two things can be one of 6 options:

  1. It can be used to show changes within a local repo. That would be shown if some changes occurred anywhere in the repo’s file directory.
  2. It can be used to show the difference between local and remote repos. So, if you made changes on your local device and some on the Git repo, git diff can help you identify exactly what changed.
  3. Git-diff can also be used to identify differences between two commits of the repo in general.
  4. It also shows the difference between two specific files in two or more commits by showing the changes' line numbers.
  5. Show the difference between two local or remote branches.
  6. Show the difference between two tags of the repo. Tags are often used to refer to working versions of the repo. For example, you can use git diff to identify the differences between version 1.0.0 and version 1.1.0 or your application.

Command №2: git filter-branch

This command is used to rewrite your repo’s history. It does that by applying custom filters to each revision of the repo. The custom filters can change the working-tree or the commits' information, but it can’t change the commit times or merge information.

Usage

The general syntax for this command is git filter-branch <filters> branch_name . There are 7options of filters that you can use in this command to rewrite history for different aspects of the branch.

  1. subdirectory-filter: This filter only checks out a specific subdirectory or the branch.
  2. env-filter: This filter is often used to rewrite the environment information of a specific commit. For example, rewrite the author’s name, email, or time of the commit.
  3. tree-filter: This filter option is very powerful; you can use it to check out all commits to the branch. Which means it can change, remove, add, or even move or change files.
  4. index-filter: Similar to the tree-filter, but this one doesn’t check out the entire tree, only the indices of it. Hence it is much faster, especially for large repos.
  5. parent-filter: This option changes the parents' list of a commit.
  6. msg-filter: If you only want to change the commit messages, this filter is the way to go.
  7. tag-name-filter: If you want to edit the tags of your commits, use this command.

Command №3: git bisect

This is probably one of the most important Git commands — in my opinion. Bugs can kill your application, and sometimes debugging a repo is not an easy task. git bisect can be used to find bugs in a repo.

The entire idea behind git bisect is to perform a binary search in the commits history to find a particular regression bug — a regression bug is a problem resulting from an unrelated change in the code.

git bisect walks you through all recent commits, asking you if they are good or bad — that is, if the regressing bug is present in the commit or not. Doing so narrows down the options to the broken commit.

Command №4: git grep

Trying to find something in your repo? Want to search all your branches for a specific file? git grep is here to help you achieve this smoothly and with ease. git grep is basically used to search for a pattern in a working-tree.

You can use git grep to search for wither exact words or regex in the repo. There are various options you can use with this command. Assume we are looking for the doc in the repo; we can use one of these options:

  1. Search by line number git grep -n doc .
  2. Search only file namesgit grep -l doc .
  3. Search using a regex pattern git grep "f[^\s]\w" .
  4. Specify how many matches in files git grep -c doc .

git grep can also be used to search for multiple words using and/or relations. Moreover, it can search in a specific commit, branch, or find all the occurrences between two commits or tags in the repo.

Command №5: git blame

The git blame command is used to display the information of the author of each commit. It can be used to track bugs and find the commits the produced an error. On a higher level, git blame is used to inspect specific points in repo history and obtain information on who last committed and what they really changed.

git blame displays the last author that modified a line; you can even specify exact line numbers and get the commits that affected that line and who performed them.

Some people are often confused between git blame and git log. Although they may sound similar, if you just need to display the commits performed, what they changed, and when they were done, it’s troublesome to use get blame to achieve that. In this case, git log is the better option. However, if you only want to display the metadata of the person who performed the commit, then git blame should be your command of choice.