Git Tips
Here are some useful but less-common Git features.
git clean
This removes untracked files from the working tree. Why would you do this? Let’s say
you run a build process that litters the working tree with temporary files in
dozens of folders, instead of being neat and tidy and putting it in a build
folder.
This will remove all untracked files, including ignored ones, and remove untracked directories as well:
git clean -d -x
There are a handful of options, but this is my preferred “make it really clean”
approach. You can use this with git reset
to get back to a pristine working
directory.
git pull –rebase
Prefer to pull with rebasing instead of merging. Generally, your local changes were meant to be applied to the tip of the remote repository, so why not get in the habit of keeping your files rebased?
git pull --rebase
This is essentially the same as this
git fetch
git rebase
You can also set a few entries in config so that this happens without needing
the --rebase
flag. To set this so master
branch always rebases:
git config branch.master.rebase true
or to set it up so new branches always rebase automatically:
git config --global branch.autosetuprebase always
There will still be cases where you will want to merge, but generally this would be an explicit step of merging a (feature) branch onto a (production) branch.
See what’s been cherry-picked
If you have a workflow where you are cherry-picking commits from one branch to
another, it’s useful to see what has been cherry-picked. If you want to see what
has and hasn’t been cherry-picked from the current branch to master
:
git cherry -v master
This shows all commits on the current branch, prefixed with +
for commits
that have not yet been cherry-picked and with -
for commits that have
already been cherry-picked.