Basic Git tips for efficient software development

Falke Stephan

30/04/2021

Git is one of the most essential tools in software development. However, due to its enormous range of features, there are also some functions that, while very useful, are not particularly well-known. We'll highlight a few useful features here.

To make it easy for you to test the commands while reading, we explicitly point out when commands make serious or irreversible changes to your repository. To be on the safe side, push all your changes beforehand. Basic Git knowledge is assumed below.

1. Better log

The command git log will be familiar to most, but it also offers advanced options to better display the information. -​-graph Git creates a graph that visualizes the relationships between commits and branches. Due to the amount of detail displayed, this is not yet particularly clear. The option -​-online, by displaying the commits in abbreviated form and using one line for each one. If you also use the option -​-all all commits are displayed. This allows you to git log -​-graph -​-oneline -​-all get a very good overview.
The aliases of these StackOverflow answer are also particularly recommended for this purpose. They clearly depict the development of the branches with all the important information.

2. The interactive mode

Before committing, you must select the changes you want to include in the commit. Often, this is done by selecting the modified files with git add path/to/file.c added. However, this method has a few disadvantages:

  • The file path of each file must be specified
  • If only part of the changes in a file are to be added, all other changes must be undone first
  • Small details such as deleted or added blank lines are also adopted and lead to a more confusing change history

The use of git add -p. With the option -p Git presents modified blocks one by one, and you can interactively decide which of them you want to include in the next commit. In interactive mode, Git displays the available options after pressing Enter. git add -p These are the most important:

  • y – Add current block
  • n – Do not add current block
  • q – Cancel (but retains previous changes)

If you only want to add some of the changes to a block, you can add the block with s further subdivide or, if that is not possible, with e Decide line by line. In addition, you can a and d for all blocks of the current file at the same time.
In addition to the advantages of more targeted selection of changes, git add -p It also makes it easier to double-check your changes. Whitespace errors are also particularly noticeable here.

If you want to discard changes that have not yet been committed, then git checkout -p The command is similar to git add -p in its use. Therefore, one must be careful not to mix it up and be aware that incorrect use of git checkout -p can lead to irreversible loss of uncommitted changes. Nevertheless, the command is recommended.

3. Commit efficiently

If you are satisfied with your selected changes, the next step is of course git commitGit will then open a text editor so you can specify the commit message. You can skip this step by passing the commit message directly to the command: git commit -m 'This is an example message'.
Additions to an already created commit can be made with git commit -​-amend be submitted later. Instead of creating a new commit, this command simply merges the content into the most recent commit. It can also be used to modify the commit message of the most recent commit. However, modifying the most recent commit is only recommended if it hasn't been pushed yet.

4. Clean up with git rebase

Many people have already made the mistake of branching from the wrong branch. Of course, you can delete the new branch and create it again in the correct place, but what if you have already committed changes? git rebase [target commit/branch] You can "move" the currently checked-out branch to the target commit or branch. Any conflicts that arise must then be resolved in the same way as with a merge.
This is also useful if, for example, you branched a feature branch from the master branch some time ago and now want to update your feature branch to the latest version of the master branch. This allows you to work with the latest version, and any conflicts arise during the rebase rather than later during the merge, making the work of the person who needs to merge the branch easier.

git rebase

However, this also becomes difficult if the branch has already been pushed. -​-force The branch can be pushed after the rebase, but this is problematic for everyone else who already relies on the branch. Therefore, you should only do this if you are aware of the consequences.
git rebase -i [target commit/branch] can also be used to edit the commits of the current branch. The command opens the text editor configured for Git, lists the relevant commits, and displays a brief summary of the options. Among other things, you can change the commit messages and order, as well as merge or remove commits, which can, of course, result in significant changes to the repository. However, it's difficult to accidentally remove commits here.

5. Rescue by reflog

If you have made a mistake with Git, the command git reflog The reflog, or reference log, contains some information about previously executed commands and can be used, among other things, to check out and restore a previous version. It's important to note that the reflog only contains changes to the local repository, and old entries are regularly deleted.
The following example shows how to git reflog How to restore a deleted branch. Please do not attempt this, as the example deletes the master branch, which obviously leads to significant changes. git reflog But you can also run it in your repository without any problem, as it only displays information.

$ git branch -D master #Do not execute! Deleted branch master (was 8f6505f). $ git reflog b776db8 (HEAD ->; feature_can) HEAD@{0}: commit: CAN: Fix init db61c14 HEAD@{1}: commit: Add CAN Module 80a3258 HEAD@{2}: checkout: moving from 80a3258 to feature_can 80a3258 HEAD@{3}: checkout: moving from master to 80a3258 8f6505f HEAD@{4}: commit: Add build instructions to readme d60b906 HEAD@{5}: commit: Add readme 80a3258 HEAD@{6}: commit (initial): Create environment $ git checkout 8f6505f #Originally master [...] $ git checkout -b master Switched to a new branch 'master'

6. Browse the repository

With git grep you can search for a specific text in the repository files (more precisely: starting from the current folder in tracked files). For example, after entering the command git grep 'contains' all occurrences of the text “contains” are displayed. With the option -n the line number of the hit is also displayed and with -i Case is ignored. Regex expressions are also allowed as search terms.

git grep

Do you have any questions or further tips? Let us know in the comments section!


Written by Falke Stephan

Falke Stephan has been working as a software developer at MEDtech Ingenieur since 2020. His responsibilities include the development of medical devices based on embedded Linux.


More articles

  • 09/09/2025
  • General, Software

In previous blog posts, I have introduced two essential components of a simple and universally applicable software architecture: events with dispatchers, listeners, and data pools. These already allow for many simple use cases ...

Read more
  • 12/11/2024
  • General, Software, Testing, Tools

In safety-critical software projects, software quality is paramount. Especially for Class C software, which must be certified according to strict standards such as IEC 62304 (medical technology), it is essential that ...

Read more
  • 08/08/2024
  • General, Electrical Stimulation, Software, Testing

Nowadays, apps in the healthcare sector are very important. Apps that can read and process data from medical sensors are particularly useful. Flutter is an open-source framework from Google that is excellent for ...

Read more
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.