Git Useful Tricks

From RidgeRun Developer Wiki

Introduction

The Git version control has provided several command-line tricks that makes our lives easier. The Typical Workflow while using Git is shown on this webpage Git Typical Workflow. In this specific wiki RidgeRun provides some of the known commands that makes things a bit simpler.


Useful commands to handle Git repositories

git new branch

Create branch

git branch -b <BRANCH_NAME>

Branch Checkout

git checkout <BRANCH_NAME>

Push new branch

git push -u origin <BRANCH_NAME> 

Delete a local and remote branch

In order to delete a local branch we can just type:

git branch -d <branch_name> 

However this will not delete the remote branch. To do it so, then type:

git push origin :<branch_name>

or

git push origin --delete <branch_name>

Cloning a specific branch

While the need of cloning several times a repository is not common, sometimes we must do it in order to compile from scratch, for instance when doing a release. Even though the repositories shouldn't be huge, sometimes we face that case but we just need a specific branch. Cloning using:

git clone -b <branch_name> <repository.git>

clones the whole repository but checks out into the specific <branch_name>. Nevertheless if we would like to avoid cloning all the repo and speed up things, we could use:

git clone -b <branch_name> --single-branch <repository.git>

With that, we can avoid download all the control information, including tags, branches and others.

Tags

Tags list:

git tag

Annotate tags:

git tag -a v1.4 -m 'my version 1.4'


Lightweight tags:

git tag v1.4-lw


Push tag:

git push origin <tag_name>


Git Patches

Create a diff between two different commit versions

git diff 0c8ab34c562f8771acf942f6f94e100e93fc6270 c0e42b004e6e8a421f7b1f780466e5150936c922

Now, to create a patch we just have to redirect the output of the git diff to a file:

git diff 0c8ab34c562f8771acf942f6f94e100e93fc6270 c0e42b004e6e8a421f7b1f780466e5150936c922 > add_feature.patch

If you want to send the patch by email with signature, you can use the 'format-patch -n' and the '-o patches_rev' (output) flags. This will create a patch for the last 'n' revisions on the directory 'patches_rev':

git format-patch -2 -o patches_rev 

This will create a directory 'patches_rev' and the patches inside it will have the next header format:

From c0e42b004e6e8a421f7b1f780466e5150936c922 Mon Sep 17 00:00:00 2001
From: Author <firstname.lastname@ridgerun.com>
Date: Thu, 4 Feb 2016 13:32:51 -0500
Subject: [PATCH 2/2] Fix Ethernet pinmux settings, Add support for
 12V_Out_EN, & Enable VADC clock

---
 arch/arm/include/asm/arch-vf/crm_regs.h    | 12 ++++++++
 arch/arm/include/asm/arch-vf/iomux-vf610.h |  3 +-
 board/dummy.c                 | 43 ++++++++++++++++++++-------
 3 files changed, 47 insertions(+), 11 deletions(-)

Search for a file in all branches

 FILENAME_KEYWORD=*hello*
 git log --pretty=oneline --name-status --all -- $FILENAME_KEYWORD

Display log with files

 git log --pretty=oneline --name-status

See who created a specific branch

git for-each-ref --format='%(authorname) %09 -%(refname)' | sort


Hide whitespace on diff and show output

git diff -w
git show -w <commit>


Show only changed words instead of whole lines

git diff --word-diff

See which branches you have recently worked on

git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"


References