7 Powerful Git Tricks to Boost Your Productivity
Discover practical Git shortcuts—including automatic command correction, commit counting, repository cleanup, untracked file backups, .git folder insights, cross‑branch file viewing, and advanced searching—that can streamline your workflow and save you valuable development time.
1. Enable Git’s automatic command correction
Activate help.autocorrect to let Git suggest and automatically run the most likely correct command when you mistype a subcommand. $ git config --global help.autocorrect 1 Omit --global to apply the setting only to the current repository.
2. Count commits easily
Use git rev-list --count <branch-name> to obtain the total number of commits on a branch, which is handy for build numbers or project progress tracking.
$ git rev-list --count master
$ git rev-list --count dev3. Optimize your repository
Maintain a clean .gitignore to exclude unnecessary files, and run garbage collection to prune unreachable objects.
$ git gc --prune=now --aggressive4. Backup untracked files
Create a zip archive of all untracked files (excluding those ignored by .gitignore) with a single pipeline command.
$ git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.zip5. Explore the .git directory
The hidden .git folder stores all repository metadata, including HEAD, refs, and configuration. Viewing files like .git/HEAD or .git/description reveals the current branch and repository description.
$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/description6. View a file from another branch without switching
Use git show to display the contents of a file on a different branch directly in the terminal.
$ git show main:README.md7. Search across the entire repository
Combine git rev-list and git grep to search for a string in all commits and branches.
$ git rev-list --all | xargs git grep -F ''
$ git rev-list --all | xargs git grep -F 'font-size: 52 px;'These tips can significantly improve your efficiency when working with Git.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
