Blog Engineering Git tips and tricks
December 8, 2016
6 min read

Git tips and tricks

Handy Git commands for everyday use

git-tricks-cover-image.png

[Git] comes with a ton of commands, and that's probably an understatement.

[The internet] is full of Git tips and it's hard if not impossible to know
them all, but sometimes you stumble upon an aha! moment that changes your
whole workflow.

In this post, we gathered some Git tips and tricks we use at GitLab everyday.
Hopefully they will add up to your aha! moment.

  • TOC

Intro

Almost everybody at GitLab will need to use Git at some point. For newcomers
who know nothing about Git that can be a fearsome experience. We have a
[Git cheatsheet] and a #git-help chat channel where we ask questions and
provide help if some of us get stuck. That's a quick way to provide help, and
if something is complicated or someone has messed up their local repository and
needs immediate help, there's always a person to jump on a quick call.

Here's a pack of Git tricks that will leverage your Git-fu and you'll hopefully
find useful. Remember, the list is far from exhaustive :)

Git's built-in help

The majority of users rely on sites like [StackOverflow] to find answers to their
Git problems, but how often do you use Git's built-in help to find more about a
command you are struggling with?

The most common commands

Run git help to print a list of the most common commands. You'll probably
notice you've used most of them, but how well do you really know them?
Thankfully, there is a help page for every command!

A help page for every command

Git's documentation is comprehensive and is automatically installed with Git.
Run git help <command> to find out all about a command's behavior and what
options it can take.

Git guides

Git comes with a handful of guides ready for you to explore. Run git help -g
to see what's available:

The common Git guides are:

   attributes   Defining attributes per path
   everyday     Everyday Git With 20 Commands Or So
   glossary     A Git glossary
   ignore       Specifies intentionally untracked files to ignore
   modules      Defining submodule properties
   revisions    Specifying revisions and ranges for Git
   tutorial     A tutorial introduction to Git (for version 1.5.1 or newer)
   workflows    An overview of recommended workflows with Git

Jump to a Git tutorial with git help tutorial, go through the glossary with
git help glossary or learn about the most common commands with
git help everyday.

See the repository status in your terminal's prompt

It's very useful to be able to visualize the status of your repository at any
given time. While there are 3rd party tools that include this information
([oh-my-zsh][ohmyzsh] anyone?), Git itself provides a script named git-prompt.sh
that does exactly that. You can [download it][gitprompt] and follow the
instructions in it to install and use it in your system. If you're using Linux
and have installed Git with your package manager, it may already be
present on your system, usually under /etc/bash_completion.d/.

Go ahead and replace your boring shell prompt with something like this:

Git shell prompt

Taken from oh-my-zsh's [themes wiki][git-shell-info-source]

Autocompletion for Git commands

You may also find it useful to use the [completion scripts] that provide Git
command completion for bash, tcsh and zsh. Again, follow the instructions
inside the scripts to learn how to install them. Once done, you can try out
typing a command.

Let's say you want to type git pull. If Git completion is enabled, typing
just the first letter with git p followed by Tab will show the
following:

pack-objects   -- create packed archive of objects
pack-redundant -- find redundant pack files
pack-refs      -- pack heads and tags for efficient repository access
parse-remote   -- routines to help parsing remote repository access parameters
patch-id       -- compute unique ID for a patch
prune          -- prune all unreachable objects from the object database
prune-packed   -- remove extra objects that are already in pack files
pull           -- fetch from and merge with another repository or local branch
push           -- update remote refs along with associated objects

To show all available commands, type git in your terminal followed by
Tab+ Tab, and see the magic happening.

It's a kind of magic

Git plugins

Since Git is free software, it's easy for people to write scripts that extend
its functionality. Let's see some of the most common ones.

The git-extras plugin

If you want to enhance Git with more commands, you'll want to try out the
[git-extras plugin][gitextras]. It includes commands like git info (show
information about the repository), git effort (number of commits per file),
and the list goes on. After you [install][extras-inst] it, make sure to visit
the [documentation on the provided commands][commands] in order to understand
what each one does before using it.

The git-open plugin

If you want to quickly visit the website on which the repository you're on is
hosted, git-open is for you. All major providers are supported (GitLab, GitHub,
Bitbucket) and you can even use them all at the same time if you set
them as different remotes.

[Install it][install-open], and try it out by cloning a repository from
GitLab.com. From your terminal navigate to that
repository and run git open to be transferred to the project's page on
GitLab.com.

It works by default for projects hosted on GitLab.com, but you can also use it
with your own GitLab instances. In that case, make sure to set up the domain
name with:

git config gitopen.gitlab.domain git.example.com

You can even open different remotes and branches if they have been set up.
Read more in the [examples section][git-open-examples].

.gitconfig on steroids

The .gitconfig file contains information on how you want Git to behave on
certain circumstances. There are options you can set at a repository level,
but you can also set them in a global .gitconfig so that all local config
will inherit its values. This file usually resides in your home directory.
If not, either you'll have to create it manually or it will be automatically
be created when you issue a command starting with git config --global as
we'll see below.

The very first encounter with .gitconfig was probably when you set your
name and email address for Git to know who you are.
To know more about the options .gitconfig can take, see the [Git documentation
on .gitconfig][gitconfig].

If you are using macOS or Linux, .gitconfig will probably be hidden if you are
trying to open it from a file manager. Either make sure the hidden files are
shown or open it using a command in the terminal: atom ~/.gitconfig.

Let's explore some of the most useful config options.

Set a global .gitignore

If you want to avoid committing files like .DS_Store, Vim swp files, etc.,
you can set up a global .gitignore file.

First create the file:

touch ~/.gitignore

Then run:

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

Or manually add the following to your ~/.gitconfig:

[core]
  excludesFile = ~/.gitignore

Gradually build up your own useful list of things you want Git to ignore. Read
the gitignore documentation to find out
more.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum. Share your feedback

Ready to get started?

See what your team could do with a unified DevSecOps Platform.

Get free trial

New to GitLab and not sure where to start?

Get started guide

Learn about what GitLab can do for your team

Talk to an expert