Git tips & tricks collection

Common tricks in git and some helpful commands.

Global Git Settings with .gitconfig

Global .gitignore

Sometimes you want to ignore files across all your git projects (eg. .DS_STORE or thumbs.db) and you can opt to use a global one stored in ~/.gitignore and reference that in your ~/.gitconfig. This cleans up your .gitignore to focus on what’s most important for that repo.

Firstly setup your global git ignore in your home folder - note I’m appending just in-case you’ve already got something there!

echo '.DS_STORE' >> ~/.gitignore 

Then we need to tell .gitconfig to reference the file we just created.

git config --global core.excludesFile ~/.gitignore

git config --global core.excludesfile %USERPROFILE%\.gitignore

git config --global core.excludesfile "$Env:USERPROFILE\.gitignore"

Some useful things to add to the global .gitignore can be things like:


The negative side to this is that some users of your repository may not also have these in their global and thus inadvertently add these in later.

Exporting your repository without git artefacts

Sometimes you just want to export your git repo without the git gunk. Here’s something akin to svn export from the old days using the git archive command.

You can simply zip up a cloned repository immediately with:

git archive --format zip --output /tmp/

Or take it a little further, tar it up and BZIP it nicely:

git archive --format=tar --prefix=${PROJECT}-${VERSION}/ ${RELEASETAG} | bzip2 -9 > ${CURRENTDIR}/${PROJECT}-${RELEASE}.tar.bz2

git archive --format=tar --prefix=xbmc/ 3.2.1 | bzip2 -9 > xbmc.3.2.1.tar.bz2

But what if you have files that you want to exclude? Don’t worry, they’ve thought of that too with the .gitattributes file that details what we should ignore in an export:

/target export-ignore
.gitattributes export-ignore  
.gitignore export-ignore
.gitkeep export-ignore

Rebase commits since last push

With branches being so free in git, we make a lot of them, we also do lots of commits (at least I do). But before pushing them, I like to squash them where appropriate.

git rebase -i origin/feature-x feature-x

It’s only the local commits I squash rather than squashing the whole lot - which requires you to do a force push (instead let your merge after a PR squash your commits).

Sign your git commits

See Signing Git Commits, became too much in one heading to have it here!

Sign previous commits

If you want to sign your previous commits you can exploit the use of the git rebase --exec feature by signing all previous commits until a commit hash.

So let’s take this historical view of a repo:

$ git log --pretty=format:"%h - %an, %ar : %s"
b0f59fc - Thushan Fernando, 2 hours ago : Fixes issues with multiple tabs not containerising.
6abfdaf - Thushan Fernando, 2 months ago : fixes some formatting for figures.
7386c91 - Thushan Fernando, 2 months ago : Fix invalid dates on notes.
5fd5c8b - Thushan Fernando, 2 months ago : Hides last modified if it's the same as pubdate.
15482c5 - Thushan Fernando, 2 months ago : Adds last modified to posts.
f806691 - Thushan Fernando, 2 months ago : Slice of authors to flatten lists.
6e5283a - Thushan Fernando, 2 months ago : - Updates layout for home page view
5857b50 - Thushan Fernando, 4 months ago : Adds ATOM feed from kaushalmodi
594536d - Thushan Fernando, 4 months ago : Adds RSS+XML header.
fc25cf4 - Thushan Fernando, 8 months ago : TABS: radius on the border.
3453c6a - Thushan Fernando, 8 months ago : Selects first index in Tabs.
c771b2d - Thushan Fernando, 8 months ago : Align dots.
2885cd6 - Thushan Fernando, 8 months ago : Tab styling.

If you want to sign all commits until 594536d we simply run:

git rebase --exec 'git commit --amend --no-edit -n -S' -i 594536d

Checkout new branch from a base branch

Sometimes you branch, find you want to something else but want to re-branch from the base branch (example: master):

[working-branch] $ git checkout master
[master] $ git checkout -b new-branch
[new-branch] $ git ...

Short circuit having to revert to your base branch:

[working-branch] $ git checkout -b new-branch master
[new-branch] $ git ...

Prune local branches not in remote repository

Clean up any local branches tracking a remote branch that no longer exist with:

$ git remote prune origin

It’s advisable you give it a dry run first just to make sure:

$ git remote prune origin --dry-run

If you’ve got a lot of branches it’s pruned, you can also do a little house-keeping with:

$ git gc

Deleting a remote branch

If you want to clean-up remote branches, you can do that in two ways:

$ git push origin :[BRANCH]

Or in git v1.7.x+ something a little easier to read:

$ git push origin -d [BRANCH]

Search in git logs

You can search commit messages or the code itself in the log:

$ git log --grep="updated SSL certificate" # searches the commit messages
$ git log -S"[STRING]" --patch # searches the code change and shows a patch of change

Visualise Git History Since Start of branch

git log --graph --decorate --pretty=oneline --abbrev-commit

Gives us something akin to this:

Git history since start of branch / fork.
Figure 1. Git history since start of branch / fork.

More tips…

Check out the git tips collection on github, so many!

Related Articles

Post is 3785 days old, comments have been disabled.