Home Git Tricks
Post
Cancel

Git Tricks

Git Tricks

In this blog, we’ll share some useful Git hacks and tricks to help you work more efficiently with Git and get the most out of this powerful tool.

Use Git aliases

Git aliases are shortcuts for Git commands that can save you time and reduce typing errors. For example, you can create an alias for git status using the following command:

1
2
git config --global alias.st status

After running this command, you can use git st instead of git status to check the status of your repository.

You can create aliases for any Git command, and you can even chain commands together using the ! operator.

For example, you can create an alias for git log that shows a nicely formatted graph of the commit history using the following command:

1
2
git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(auto)%h %C(cyan)%<(12)%an%C(auto)%d %Creset%s %Cgreen(%cr)%Creset' --all"

After running this command, you can use git lg to see a graphical representation of your commit history.

Use Git hooks

Git hooks are scripts that can be triggered automatically at certain points in the Git workflow, such as before or after a commit. You can use Git hooks to perform automated tasks, such as running tests or linters before committing code.

To use Git hooks, create a script in the .git/hooks directory with the appropriate name (e.g., pre-commit, post-commit, etc.) and make the script executable. Git will automatically run the script when the corresponding Git command is executed.

For example, you can create a pre-commit hook that runs a linter on your code using the following steps:

  • Create a file named pre-commit in the .git/hooks directory.

  • Add the following code to the file:

1
2
3
4
5
6
7
#!/bin/bash
lint_output=$(my_linter_command)
if [[ $lint_output ]]; then
  echo "$lint_output"
  exit 1
fi

  • Make the file executable using the following command:
1
2
chmod +x .git/hooks/pre-commit

After completing these steps, Git will automatically run your linter before every commit, and the commit will be aborted if the linter reports any errors.

Use Git rebase instead of merge

Git merge is a powerful tool for combining changes from different branches, but it can also create messy and cluttered commit histories. Git rebase is an alternative tool that can be used to combine changes from one branch into another while preserving a linear commit history.

To use Git rebase, use the following command:

1
2
3
git checkout <feature-branch>
git rebase <target-branch>

This will apply the changes from feature-branch onto target-branch in a linear fashion. After the rebase is complete, you can use git push –force to update the remote branch with the rebased changes.

Use Git stash to temporarily store changes

Git stash is a useful tool for temporarily storing changes that you’re not ready to commit. To use Git stash, use the following commands:

1
2
git stash save "My temporary changes"

This will save your changes to the stash with a descriptive message. You can then use git stash list to see a list of stashed changes and git stash apply to apply the changes.

Use Use Git bisect to find bugs

Git bisect is a powerful tool that can help you find the commit that introduced a bug by performing a binary search through the commit history. To use Git bisect, follow these steps:

  • Start with a known good commit (e.g., the last commit where the bug was not present).
  • Use git bisect start to start the bisect process.
  • Use git bisect bad to mark the current commit as bad (i.e., the commit where the bug is present).
  • Use git bisect good <commit> to mark a known good commit.
  • Git will checkout a new commit between the known good and bad commits.
  • Test the new commit to determine if the bug is present.
  • Use git bisect bad or git bisect good to mark the new commit as good or bad.
  • Repeat the previous two steps until Git has found the commit that introduced the bug.

Use Git cherry-pick to apply specific commits

Git cherry-pick is a tool that can be used to apply specific commits from one branch to another. To use Git cherry-pick, follow these steps:

  • Use git log to find the commit you want to apply.
  • Use git cherry-pick <commit> to apply the commit to the current branch.
  • Git cherry-pick will apply the changes from the specified commit to the current branch, creating a new commit that includes only those changes. This can be useful when you want to apply a specific fix or feature from one branch to another without merging the entire branch.

Use Git reflog to recover lost commits

Git reflog is a tool that can be used to recover lost commits. Git reflog keeps a log of all the commits that have been checked out in the local repository, even if they have been deleted or lost due to a hard reset. To use Git reflog, follow these steps:

  • Use git reflog to see a list of all the commits that have been checked out in the local repository.
  • Find the commit that you want to recover.
  • Use git checkout <commit> to checkout the lost commit.

Git reflog can be a life saver when you accidentally delete a commit or lose it due to a hard reset.

These are just a few of the many Git hacks and tricks that can help you work more efficiently with Git. By mastering these tools and exploring more advanced Git workflows, you can become a more effective and productive developer. to see a list of all the commits that have been checked out in the local repository.

This post is licensed under CC BY 4.0 by the author.