Everyday GIT

Cheat sheet

Kamil Kubicki
Active Developement
7 min readJun 29, 2022

--

Photo by Yancy Min on Unsplash

To speed up the code integration process most developers use SmartGit, SourceTree or other Git versioning client tool.

Things go touchy when you need to repeat same process in a command line. Hopefully there are several solutions for that.

This article will focus on Git commands & techniques you most likely use or need on daily basis.

1. Git aliases

First element that can speed-up the code integration are git ‘aliases’. Using this technique, you can transform long and complex git commands into simple ones and share them easily with your team. This is a real time saver that avoids copy/paste bad habits.

First things first, let’s verify what is under the hood of our global/local git configuration:

  • --global: stands for ‘global config file’ ~/.gitconfig
  • --local: stands for ‘local config file’ /.git/config
  • -l: stands for ‘list all’

My configuration is alias free - an excellent practice🎮ground! Now we will discuss two ways to add the git alias.

First method relies on Git config file content modification:

Globally: ~/.gitconfig
Locally in project: /.git/config

New alias should be included below [alias] section:

Second method consists on using the command line:

To explore the power of alias, I propose you to start with a simple example for three following commands:

  • git checkout
  • git status
  • git log

In order to add an alias for each command, we need to amend the Git config file:

Easy as is, isn’t it? We can immediately test the changes:

The same result achieved using a command line is shown below (remember to use --global or --local parameter depends on your configuration requirements):

Great job! We are ready to jump into more complex definitions discussed under next section.

2. Git log & graphs

(!) Before going any further, please read carefully below section.

Troubleshooting: A problem was noticed on Windows OS. The screen freezes when user tries to resize the terminal with git log longer than one terminal screen.

To handle that issue we can get rid of git pager (not recommended method for significant repositories) or reduce the number of commits that will be prompted on the screen.

  • cat : tells to not use pager and write output directly to the command line
  • less --FX: paginate only when the output is longer than the terminal screen — type Q to exit the pager

Alternatively, instead of cat configuration you can use --no-pager parameter directly inside the command.

The solution, that suits best our needs is to reduce the lines of command’s result using -n parameter followed by the number of commits you wish to print in terminal (you will notice that the output is verbose, this is something we will focus on in a minute).

Well done! Now we are ready to move forward and usegit logcommand.

As you probably guessed, the command displays commit logs. However, the power of log depends on other parameters that help to take full advantage of the git feature and take the output to the next leverl— a bit closer to the notorious SmartGit or SourceTree display tree results.

  • --no-pager : tells Git to not use pager
  • --oneline : shorthand for ‘ --pretty=oneline --abbrev-commit’. Retrieves partial prefix for commit object name and ensures output’s squashing to one line per commit
  • -n 10 : defines max number of commits in output
  • --decorate / --no-decorate : prints out the ref names
    (example: HEAD -> 6.2, origin/HEAD, origin/6.2)
  • --graph: an excellet way to display the state of our repository in a human readable way. Draws a text-based graphical representation of the commit history

The goal of this article is to speed-up daily routine - we will add now previous command to our alias list — as we learned in previous section:

Voila! Our alisas showing git branches in terminal is fully operational 🎊.

Bonus:

Git is awesome — I bet, you already know that 😄It won’t be then surprising for you that we can go a step further and define the decoration of log output command.

--pretty=format:"..." helps to define the structure of each commit line displayed on the screen. Here are main format elements used in this article’s examples:

  • %h: (abbrev) commit hash
  • %d : ref names
  • %s : commit subject
  • %cn: committer name
  • %cr: committer date

Every part of pulled information can be also highlighted with
%C(your_chosen_color) parameter — this one needs to be placed straight before the element to decorate.

Github official documentation about colours:

The basic colors accepted are `normal`, `black`, `red`, `green`, `yellow`,`blue`, `magenta`, `cyan` and `white`. The first color given is theforeground; the second is the background. All the basic colors except`normal` have a bright variant that can be speficied by prefixing thecolor with `bright`, like `brightred`

Hex Code #RRGGBB colours format can be also used to decorate your prompted lines.

Thank you for attention and hope you have learned something new.

More information about you can find here: https://git-scm.com/doc

3. Git “must know” commands

Git init

Creates a brand new git repository in the current directory.

Git status

Shows project status (displays current branch name and impacted files).

Below is a short version of the command that might be handy if you require minimalist display of changes (s stands for --short, b for --branch):

M = modified
A = added
D = deleted
R = renamed
U = updated
?? = not tracked

Git add

Adds a file to the index (once added, the file can be comitted/validated and sent to a distant repository). If you don’t want to specify the files one by one and add them all at once, you can use a dot . instead the filename.

or

Git commit

Validates all files previously added to index — each commit action should integrate a message.

Git diff

Shows the difference (modifications in files) between local content and remote repository.

Git fetch

Gets the content from remote repository — it helps to visualise the distant changes. The command is different from git pullcause does not merge the code and all local files and changes stay as are.

Git pull

Gets the remote state of content and sync local files with the distant reference repository.

Git push

Sends local changes to the remote repository.

Git branch

There are three main reason of using the command — to list/create or delete an existing branch.

List branches available locally.

Add new branch.

Delete existing branch.

Git checkout

Updates local files to match the remote version.

Creates a new branch and switches to it by updating the index and the files in the working tree.

If the branch you want switch to already exists, you can use the command below:

Git merge

Mergeyour_existing_branch_name content into the current branch.

Git log

In its simplest version it displays commit’s informations in desc order (commit id, author, date & message etc.).

--

--