12 Essential Git Tips to Supercharge Your Version Control
Discover twelve practical Git tricks—from configuring global and repository settings to using aliases, visualizing commit graphs, safe force‑pushes, and powerful add/checkout options—that can dramatically improve your workflow and efficiency.
Git, the distributed version control system that turned twelve on April 7, is the default tool for open‑source development, yet many users miss powerful features that can streamline their workflow. This article presents twelve practical tips, ranging from basic configuration to advanced command tricks, to help you get the most out of Git.
1. Your ~/.gitconfig file
When you run a Git command for the first time, you may see a prompt to set your name and email. Those commands edit the global ~/.gitconfig file, which stores global options such as aliases, diff algorithms, and merge strategies. You can also include conditional configuration files based on path.
2. Repository‑specific .git/config
Using git config --global updates the global file, while omitting --global modifies the repository’s .git/config. Settings in .git/config override global ones, allowing per‑repository identities—for example, a different email for a personal fork of a work project.
3. Aliases
Aliases let you create short commands that expand to longer Git commands with preset options. Define them with git config --global --add alias.st status or edit ~/.gitconfig directly:
[alias]
st = status4. Shell aliases
You can also define aliases that run arbitrary shell commands. For example, to keep a fork up‑to‑date while preserving local changes:
upstream-merge = !"git fetch origin -v && git fetch upstream -v && git merge upstream/master && git push"The leading ! tells Git to execute the string in a shell.
5. Visualizing the commit graph
For projects with many branches, a visual graph helps understand relationships. GUI tools like GitLab’s graph viewer are useful, but you can also generate a similar view in the terminal:
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relativeThis adds a graph on the left, short SHA hashes, and relative dates.
6. Safer force‑push
Instead of plain git push --force, use git push --force-with-lease. It aborts the push if the remote branch has moved, preventing accidental overwrites of others’ work.
7. git add -N
The -N (or --intent-to-add) flag stages a new file as “intent to add” without actually adding its contents, allowing you to include the file in a later commit after reviewing changes.
8. git add -p
Interactive patch adding lets you select hunks to stage, ensuring each commit contains a single logical change. Use git add -p and follow the prompts to stage, skip, or split hunks.
9. git checkout -p
Similar to git add -p, this option lets you interactively discard selected changes from the working tree, useful for removing temporary debug statements before committing.
10. Rebase with command execution
Use git rebase -x <cmd> to run a command after each rebased commit. For example, git rebase -x "npm test" runs the test suite after every commit, ensuring the branch stays buildable.
11. Time‑based revisions
Many Git commands accept date specifications. git diff HEAD@{yesterday} shows changes since yesterday; git diff HEAD@{'2 months ago'} or git diff HEAD@{'2010-01-01 12:00:00'} work for longer periods.
12. The omniscient reflog
If you lose a commit during a rebase, git reflog lists all actions on the current branch, providing the SHA of each entry. You can recover a dropped commit with git checkout <sha> and then return to the latest state with git checkout HEAD.
These twelve tips cover both fundamental configuration and advanced workflow enhancements, helping you become more efficient and confident 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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
