Blog | 14 June, 2023

Mastering Git: Easy-to-Use Tips and Good-to-Know Tricks

Are you interested in exploring various Git options and techniques to improve your development workflow? In this article, we'll uncover the differences between Git stash apply and pop and discover time-saving commands like quick merge, flexible log filtering, and powerful cherry-picking. Whether you're new to Git intricacies or looking to expand your understanding, the tips described further will assist you in optimizing your version control practices and streamlining your development process.

The following Git life hacks, proposed by Andrii Poltavets, a software development engineer at AMC Bridge, are presented in a console-based format, so it's important to understand that while all these options may be available through GUI clients, the workflow of executing them might differ.

Understanding the options: Git stash apply vs. pop

git stash pop is a command familiar to all Git users. However, have you ever needed to preserve the content after applying pop just once? If so, it's worth trying to apply changes using git stash apply. Now you can confidently experiment with applied changes without the fear of losing your valuable stash. The author finds the push-and-apply method useful when it is up to changing a few branches within one stashed content—for example, when it is needed to test algorithm changes in several released versions of the app. The push-and-pop technique is often used for something trivial that doesn't need to be preserved for a while—for example, a few lines of code, some changes in third-party unit tests, and so on.

Atlassian Sourcetree works in push-and-apply as default option but it can be overridden.

Another flexible way to apply the stash command is using the --keep-index flag. It comes in handy when you only want to stash a portion of files. Suppose you want to commit an algorithm changes but also you have made changes to a view section that you don't want to save. In that case, you can use git add to stage the files you don't want to stash, create the stash, and then return the staged files back to avoid accidentally committing them.

Quick merge

To synchronize their branch with, for example, develop, developers often switch from their own branch to develop, perform git pull, switch back, and finally perform git merge develop.

A faster way is to:

  1. Update the repository state with git fetch.
  2. Merge develop directly from the server rather than from the local branch: git merge origin/develop.

By the way, origin is a default alias for the remote Git server, so it may have a different name.

Flexible log

git log --oneline

Remember that there is a more concise log format available.

git --grep="the text you're looking for"

If you need to find a commit but remember it should contain the words fix flickering, then the --grep parameter is at your service. Provide the text you remember as the parameter, and the log will be filtered accordingly: git log --grep="fix flickering". Noteworthy, this parameter is case-sensitive. To let it look for the commits in a case-insensitive manner, just run the --regexp-ignore-case parameter.

In fact, the mechanics are similar to using the grep command with Unix shell pipes but adapted for working with commits.

git log --author "User Name" | grep -e test (--grep=test)

Want to find all your commits? Specify your name as the value for the --author parameter.

And if there are many merge commits associated with your name, you can filter them out by adding --no-merges.

Summary

To summarize, the command below will filter the concise log to show commits containing the phrase fix flickering, authored by User Name, and without any merge commits:

git log --oneline --author "User Name" --grep="fix flickering" --no-merges

Git mv

Sometimes it happens that you move a file to another folder, and Git marks it as deleted while considering the file in the new location as new. To avoid it, you can use the command git mv old/path/to/the/file new/path/to/the/file. As a result, the file will be moved, and the Git issue will be resolved.

By the way, this command also works for renaming.

Difference between git reset --hard and git reset --soft

A soft reset does not remove changes in files; it only removes commits from the history that correspond to those changes. On the other hand, a hard reset clears the history along with the changes in files.

Knowing this difference, you can replace an inconvenient squash of commits through rebase with git reset --soft HEAD~2. It will remove the last three commits, allowing you to create a separate commit.

Note that this operation is valid only if you are certain that the last three commits were yours.

Git checkout -f

It is a force reset version. The state of the files is reset to the state of the latest commit. In particular it could be useful when you move a repository to a different place in your filesystem.

Instead of moving the whole directory, you just move the “.git” folder to a new place. The only thing you have to do now is to execute the git checkout -f command. It will restore the state of all files to the state of HEAD, which is the latest commit, in the new location.

Git cherry-pick -n

Task: Transfer a commit with specific changes without creating a separate second commit.

Solution: Use git cherry-pick with the -n parameter. This way, the changes will be transferred from the commit, but the commit itself will not be created.

Keep in mind that to cherry-pick a commit in a remote branch, it is not necessary to download it into the local repository. It is sufficient to execute git fetch, after which you can cherry-pick based on the SHA identifier.

Git bisect

Often, there is a need to find a commit where something went wrong. If an approximate location of such a commit cannot be determined, the binary search comes to the rescue. Using this methodology, Git offers to enter the hash/tag of a commit where everything was fine and another commit where the issue exists. By sequentially inputting git bisect good or git bisect bad, the problematic commit will be found faster than with a linear search from the bad to the good commit.

Git commit -amend

Don't forget that you can fix the name of a commit and even push the renamed commit. However, it is only possible if no one else has pulled that branch except for you.

Hooks

Git enables executing custom commands directly before or immediately after a commit. One possible application of hooks is to perform automatic code formatting using external tools. For example, LLVM bundle provides a pre-configured hook that can be applied in your own project to execute the clang-format command. If clang-format detects changes that do not adhere to the applied code style, such a commit will be canceled, but the tool will suggest its own changes.

Commit early, commit often

Indeed, a key recommendation for working with Git is to commit early and commit often.

Return to blog page

Subscribe to our news

We will keep you updated with the latest news

scroll down to explore
to the top