10 Git Tips and Tricks for Better Source Code Management published 4/6/2023 | 5 min read

Git has become the leading tool for version control and source code management, thanks to its flexibility, speed, and a plethora of features. Whether you are a beginner or an experienced developer, there are always new things to learn that can help you work more efficiently and effectively. In this post, we will cover 10 useful Git tips and tricks that will help you better manage your source code.



1. Use Git aliases to save time

Git provides the ability to create aliases using the git config command. This allows you to create shorter aliases for commonly used Git commands. For example, instead of typing git commit -m, you can create an alias for gcm. To create an alias, use the following command:

  
git config --global alias.<alias name> '<full command>'

For example:

  
git config --global alias.gcm 'commit -m'

After creating the alias, you can use it like a regular Git command. For example:

  
git gcm "commit-message"



2. Use Git log to visualize your repository history

The git log command allows you to view the commit history of a repository. By default, Git log shows a list of commits in reverse chronological order. However, you can add several flags to the command to customize the output. For example:

  
git log --graph --oneline --decorate

This command will print out a graph of the commit history in a compact one-line format with branch and tag names.

3. Use Git bisect to find the source of bugs

The Git bisect command can help you find the commit that introduced a bug in your codebase. To use Git bisect, start by running the following command:

  
git bisect start

Then mark the last good commit and the first bad commit using their hashes. Git will then automatically check out a commit between the two and ask you if it is good or bad. Based on your feedback, Git will checkout another commit and repeat the process until it has found the commit that introduced the bug.

4. Use Git stash to temporarily store changes

Git stash can help you save your current changes without committing them. This is particularly useful when you are in the middle of working on a feature and need to switch to another branch to fix a bug. To stash your changes, use the following command:

  
git stash

To retrieve the stashed changes later, use the command:

  
git stash apply



5. Use Git blame to see who made changes to a file

The Git blame command allows you to view the commit history of a file, including the author of each line. This is particularly useful when you are working with an unfamiliar codebase and want to see who made changes to a particular file. To use Git blame, simply run the command followed by the file name:

  
git blame <filename>

6. Use Git cherry-pick to apply specific commits to another branch

The Git cherry-pick command allows you to apply specific commits from one branch to another. This is particularly useful when you have made a commit on the wrong branch and want to apply it to the correct one. To use Git cherry-pick, start by checking out the branch where you want to apply the commit. Then run the following command, replacing <commit hash> with the hash of the commit you want to apply:

  
git cherry-pick <commit hash>

7. Use Git reflog to recover lost commits

The Git reflog command allows you to view the history of all the actions performed in your repository, including commits that were accidentally deleted. To recover a lost commit, start by finding its hash using the Git reflog command. Then use the git cherry-pick command to apply the commit to your current branch.

8. Use Git amend to change the last commit message

The Git amend command allows you to change the message of the last commit you made. This is particularly useful when you noticed a typo or forgot to include important information in the commit message. To use Git amend, first make the changes you want to the code. Then run the following command:

  
git commit --amend -m "New commit message"

9. Use Git revert to undo a commit

The Git revert command allows you to undo a commit by creating a new commit that is the inverse of the original commit. This is particularly useful when you need to undo changes that have already been pushed to a shared branch. To use Git revert, start by running the following command:

  
git revert <commit hash>



10. Use Git rebase to clean up your commit history

Git rebase allows you to modify the commit history of a branch. This is particularly useful when you have made several small commits and want to combine them into a single commit or modify the order of the commits. To use Git rebase, start by checking out the branch you want to modify. Then run the following command:

  
git rebase -i <commit hash>

This will open an interactive rebase window that allows you to modify the commit history. Note that modifying the commit history can cause problems if you have already pushed the branch to a remote repository.

In conclusion, these are just a few Git tips and tricks that can help you better manage your source code. By using these features, you can work faster, more efficiently, and with greater confidence in your codebase.