10 Git Tips To Save Time And Improve Your Workflow
Git, the stupid content tracker according to its manpage, is packed with features and some can be very intimidating. So we resort to just using the same few commands we can memorize, over and over again, instead of leveraging all the power that was given to us.
Table of Contents
This article is also available in Chinese:
https://www.infoq.cn/article/tgdOxMBqYHoVslvlZqxT
Tip #1: Improve your configuration
Git is highly configurable, on a global, user, and local level.
https://git-scm.com/docs/git-config
Lookup Order
Every setting can be overwritten:
Change Settings
Either edit any of the files with your favorite editor or use the CLI:
Values need to be quoted if they contain any white space characters.
Show Current Settings
Helpful Config Settings
https://git-scm.com/docs/gitcredentials
Tip #2: Aliases
To save commonly used git commands, create an alias:
Helpful Aliases
Tip #3: Finding Commits and Changes
By Commit Message
By Change
By Date
Tip #4: Adding Hunks
Instead of just adding all changes of a file with git add <filepath>
, the argument --patch / -p
will stage hunks interactively.
https://git-scm.com/docs/git-add#Documentation/git-add.txt–i
Tip #5: Stash changes without committing
A stash is a temporary shelve of the current changes. With their help, we can return to the current state of the index and can reapply the stashed changes later on.
By default, only changes in currently tracked files will be stashed, new files will be ignored.
Multiple stashes can be created and applied independently.
https://git-scm.com/docs/git-stash
Creating
Listing
Viewing
Applying
Cleaning
Tip #6: Dry Run
Many git operations can be quite destructive.
For example, a git clean -f
will remove all untracked files, without any chance to bring them back.
To avoid such a catastrophic result, many commands support a dry-run to check the result before it actually happens. Sadly, the option used isn’t always the same:
Be aware that git commit -n
isn’t dry-run at all!
It’s actually the option --no-verify
, ignoring any pre-commit
/commit-msg
githooks.
Tip #7: Safer Force Push
It’s easy to mess up a branch by working on an older commit, creating a new head, etc.
The remote changes could be overridden with git push --force
, but they shouldn’t!
git push --force
is a destructive and dangerous operations, because it works unconditionally, and destroys any commits already push by other committers. This doesn’t have to be fatal for the repository of others, but changing history and affecting others is not a good idea.
A better option is using git push --force-with-lease
instead.
Instead of unconditionally overwriting the upstream remote, git checks for any remote changes that aren’t available locally.
If so, it fails with a “stale info” message and wants us to git fetch
first.
https://git-scm.com/docs/git-push#Documentation/git-push.txt—force-with-leaseltrefnamegt
Tip #8: Changing Commit messages
Commits are immutable, and can’t be changed. But we can amend an existing commit with a new message, instead. This will replace the original commit, so don’t use it on already pushed commits.
https://git-scm.com/docs/git-commit#Documentation/git-commit.txt—amend
Tip #9: Changing history even more
Changing the history of a repository doesn’t stop at the last commit message.
With git rebase
, multiple commits can be changed:
A reverse-ordered list of commits is presented in the configured editor. Something like this:
By changing the actual content of the editor, we can give git a blueprint of what/how to rebase:
After saving the editor, git will run this blueprint to rewrite history.
The e, edit
will pause the rebase, to edit the current state of the repository.
To finish up, run git rebase --continue
.
If any problems occur, like merge conflicts, and we want to start over, there’s always git rebase --abort
.
https://git-scm.com/docs/git-rebase
Tip #10: Archive Tracked Files
We can compress the tracked files for a specific ref in different formats (zip
or tar
):
<ref>
can be a branch, commit hash, or a tag.
https://git-scm.com/docs/git-archive
Bonus Tip: The Single Dash
There is a shortcut representing the previously used branch: a single dash-
The single dash is equal to @{-1}
.
https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-ltbranchgt
Resources
- Git SCM Documentation (Git)
- Pro Git Book (free eBook)
- GitHub Git Cheat Sheet (PDF)
- Interactive Cheat Sheet