You are on page 1of 46

Code and data management with Git

Git Tips and Tricks

Wibowo Arindrarto

Department of Human Genetics


Center for Human and Clinical Genetics
Introduction

We have only been playing with some of what git offers. In reality,
you can do much more with it. It also allows for a wide array of
customizations.

We will look into some of these customizations now.

1/20 Code and data management with Git 14-06-2019


Custom Prompt

Checking which branch you are working on and its status is routine.

2/20 Code and data management with Git 14-06-2019


Custom Prompt

Checking which branch you are working on and its status is routine.

“git status” is useful, but quickly feels repetitive.


1 $ git branch
2 * master
3 $ git status
4 nothing to commit , working directory clean

Listing 1: Routine git checking

2/20 Code and data management with Git 14-06-2019


Custom Prompt

Checking which branch you are working on and its status is routine.

“git status” is useful, but quickly feels repetitive.


1 $ git branch
2 * master
3 $ git status
4 nothing to commit , working directory clean

Listing 1: Routine git checking

Solution: use a custom shell prompt that displays git status.


1 ( master ) $ git st
2 nothing to commit , working directory clean

Listing 2: Git checking with custom prompt


2/20 Code and data management with Git 14-06-2019
Custom Prompt

The git-approved way to do this is to use the “\_\_git\_ps1” shell


function defined in the “git-prompt.sh” file.

3/20 Code and data management with Git 14-06-2019


Custom Prompt

The git-approved way to do this is to use the “\_\_git\_ps1” shell


function defined in the “git-prompt.sh” file.

The location of this file depends on your OS and git version. For
now, you can download a copy of this file from our GitLab.

3/20 Code and data management with Git 14-06-2019


Custom Prompt

The git-approved way to do this is to use the “\_\_git\_ps1” shell


function defined in the “git-prompt.sh” file.

The location of this file depends on your OS and git version. For
now, you can download a copy of this file from our GitLab.

Then, in your “.bashrc” file, add “\_\_git\_ps1” to the “PS1” vari-


able, and source your “.bashrc” again.

3/20 Code and data management with Git 14-06-2019


Useful git commands

git blame
Git tracks each line of each file in its repository.

4/20 Code and data management with Git 14-06-2019


Useful git commands

git blame
Git tracks each line of each file in its repository.

You can view who committed the line change, the commit hash, and
the commit time using “git blame”.
1 $ git blame README
2 a4394d28 ( bow 2014 -06 ...) Second version .

Listing 3: git blame command

4/20 Code and data management with Git 14-06-2019


Useful git commands

git blame
Git tracks each line of each file in its repository.

You can view who committed the line change, the commit hash, and
the commit time using “git blame”.
1 $ git blame README
2 a4394d28 ( bow 2014 -06 ...) Second version .

Listing 3: git blame command

Similar functionality is also available in GitLab by clicking on the


blame button when viewing a file.

4/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
Sometimes, it is practical to refer to a commit with a name instead
of a hash.

5/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
Sometimes, it is practical to refer to a commit with a name instead
of a hash.

Official releases, for example, are better referred to as v1.0 than


a76a0fx.

5/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
Sometimes, it is practical to refer to a commit with a name instead
of a hash.

Official releases, for example, are better referred to as v1.0 than


a76a0fx.

This can be done using “git tag”.

5/20 Code and data management with Git 14-06-2019


Useful git commands

git tag

1 $ git tag " v0 .0.1"


2 $ git show v0 .0.1
3 commit a 4 3 9 4 d 2 8 e 6 b a 3 0 b e 1 9 3 1 8 e e 7 4 f 7 3 2 a 1 0 3 b 8 f f d f 2
4 Author : bow < bow@bow . web . id >
5 Date : Sat Jun 21 13:47:03 2014 +0200
6
7 Second commit
8
9 diff -- git a / README b / README
10 index efe6f7c ..4 fe6328 100644
11 --- a / README
12 +++ b / README
13 @@ -1 +1 @@
14 - First version .
15 + Second version .

Listing 4: Adding a tag

6/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
What we did previously is to add what is called a lightweight tag.
Lightweight tags are essentially commit aliases.

7/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
What we did previously is to add what is called a lightweight tag.
Lightweight tags are essentially commit aliases.

There is another type of tag, called the annotated tag.

7/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
What we did previously is to add what is called a lightweight tag.
Lightweight tags are essentially commit aliases.

There is another type of tag, called the annotated tag.

Annotated tags contain more information: tagger identity, tagging


message, tagging date, and can be verified with GPG.

7/20 Code and data management with Git 14-06-2019


Useful git commands

git tag
1 $ git tag -a " v0 .0.1" -m " Alpha version "
2 tag v0 .1.0
3 Tagger : bow < bow@bow . web . id >
4 Date : Sat Jun 21 15:11:53 2014 +0200
5
6 Alpha version
7
8 commit a 4 3 9 4 d 2 8 e 6 b a 3 0 b e 1 9 3 1 8 e e 7 4 f 7 3 2 a 1 0 3 b 8 f f d f 2
9 Author : bow < bow@bow . web . id >
10 Date : Sat Jun 21 13:47:03 2014 +0200
11
12 Second commit
13
14 diff -- git a / README b / README
15 index efe6f7c ..4 fe6328 100644
16 --- a / README
17 +++ b / README
18 @@ -1 +1 @@
19 - First version .
20 + Second version .

Listing 5: Adding an annotated tag


8/20 Code and data management with Git 14-06-2019
Useful git options

git diff -w
Sometimes, you want to hide whitespace differences when using
“git diff”.

9/20 Code and data management with Git 14-06-2019


Useful git options

git diff -w
Sometimes, you want to hide whitespace differences when using
“git diff”.

This can be done via “git diff -w”.


1 $ git diff -w

Listing 6: git diff without whitespace

9/20 Code and data management with Git 14-06-2019


Useful git options

git diff -w
Sometimes, you want to hide whitespace differences when using
“git diff”.

This can be done via “git diff -w”.


1 $ git diff -w

Listing 6: git diff without whitespace

Note that while this aids visualization of the diff, git will still commit
the whitespace change.

9/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


Git gives you total control over your history, which means you can
also change them.

10/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


Git gives you total control over your history, which means you can
also change them.

This practice is potentially dangerous and should not be part of your


regular workflow. This is especially true for public commits.

10/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


Git gives you total control over your history, which means you can
also change them.

This practice is potentially dangerous and should not be part of your


regular workflow. This is especially true for public commits.

Still, there are times when changing that one last commit makes
more sense than doing a “git revert”

10/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


Git gives you total control over your history, which means you can
also change them.

This practice is potentially dangerous and should not be part of your


regular workflow. This is especially true for public commits.

Still, there are times when changing that one last commit makes
more sense than doing a “git revert”

“git commit --amend” allows you to do that: changing your last com-
mit.

10/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


When run on a clean branch (no uncommitted changes),
“git commit --amend” allows you to change your last commit mes-
sage.

11/20 Code and data management with Git 14-06-2019


Useful git options

git commit –amend


When run on a clean branch (no uncommitted changes),
“git commit --amend” allows you to change your last commit mes-
sage.

You can also use the command to meld current staged changes to
your last committed change.
1 $ git status
2 Changes to be committed :
3 modified : README
4 $ git commit -- amend -m " Update README "
5 [ master b60437d ] Second update
6 1 file changed , 1 insertion (+) , 1 deletion ( -)

Listing 7: git commit –amend

11/20 Code and data management with Git 14-06-2019


Useful git options

git add –patch


Often, uncommited change overextends. You start with the inten-
tion of fixing bug A, but in the middle found that you can implement
feature B and feature C while also squashing bug D.

12/20 Code and data management with Git 14-06-2019


Useful git options

git add –patch


Often, uncommited change overextends. You start with the inten-
tion of fixing bug A, but in the middle found that you can implement
feature B and feature C while also squashing bug D.

At the end of the day, only “git commit -am "Updates"” is done and
the whole change is saved in a single comit.

12/20 Code and data management with Git 14-06-2019


Useful git options

git add –patch


Often, uncommited change overextends. You start with the inten-
tion of fixing bug A, but in the middle found that you can implement
feature B and feature C while also squashing bug D.

At the end of the day, only “git commit -am "Updates"” is done and
the whole change is saved in a single comit.

This defeats the purpose of tracking your changes in commits. Com-


mits ideally represents a single, functional update, which you can
understand later on.

12/20 Code and data management with Git 14-06-2019


Useful git options

git add –patch


You can, infact, commit the line changes selectively. This helps split
a mesh of changes into separate commits.
1 $ git add -- patch

Listing 8: git add –patch

13/20 Code and data management with Git 14-06-2019


Main Configuration File

Viewing
In Linux, git uses the “~/.gitconfig” file as its main configuration
file.

14/20 Code and data management with Git 14-06-2019


Main Configuration File

Viewing
In Linux, git uses the “~/.gitconfig” file as its main configuration
file.

To see the current configuration values, use “git config --list”.


1 $ git config -- list
2 user . name = bow
3 user . email = bow@bow . web . id
4 color . ui = auto

Listing 9: Git config values

14/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying
To edit the file directly, open “~/.gitconfig” in a text editor and
save your changes.
1 $ vim ~/. gitconfig

Listing 10: Modifying the config file

15/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying
To edit the file directly, open “~/.gitconfig” in a text editor and
save your changes.
1 $ vim ~/. gitconfig

Listing 10: Modifying the config file

You can also use the “git config --global” to set the values via the
shell.
1 $ git config -- global user . name " Linus Torvalds "

Listing 11: Modifying via the shell

15/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


We will look at two examples now: setting a global ignore file and
setting aliases.

16/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


We will look at two examples now: setting a global ignore file and
setting aliases.

In addition to setting a directory-specific “.gitignore” file, you can


also set a global ignore file.

16/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


We will look at two examples now: setting a global ignore file and
setting aliases.

In addition to setting a directory-specific “.gitignore” file, you can


also set a global ignore file.

You can name this file anything. The convention is to use


“.gitignore\_global” and place the file in your home directory.

16/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


The global ignore file has the same format as the per-directory
“.gitignore” file, only visible to all git repositories.

17/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


The global ignore file has the same format as the per-directory
“.gitignore” file, only visible to all git repositories.

1 $ echo "*. out " > ~/. gitignore_g l ob a l


2 $ echo " testing . txt " >> ~/. gitignore_g lo ba l
3 $ git config -- global core . excludesfile \
4 "~/. gitignore_g l ob a l "

Listing 12: Setting the global ignore file

17/20 Code and data management with Git 14-06-2019


Main Configuration File

Modifying: global ignore


The global ignore file has the same format as the per-directory
“.gitignore” file, only visible to all git repositories.

1 $ echo "*. out " > ~/. gitignore_g l ob a l


2 $ echo " testing . txt " >> ~/. gitignore_g lo ba l
3 $ git config -- global core . excludesfile \
4 "~/. gitignore_g l ob a l "

Listing 12: Setting the global ignore file

1 $ echo " Is this the real life ?" > this . out
2 $ touch testing . txt
3 $ git status
4 nothing to commit , working directory clean

Listing 13: Ignoring via the global file


17/20 Code and data management with Git 14-06-2019
Main Configuration File

Aliases
You can alias simple commands.
1 $ git config -- global alias . st status
2 $ git st
3 nothing to commit , working directory clean

Listing 14: Simple alias

18/20 Code and data management with Git 14-06-2019


Main Configuration File

Aliases
You can alias simple commands.
1 $ git config -- global alias . st status
2 $ git st
3 nothing to commit , working directory clean

Listing 14: Simple alias

Or more complex commands.


1 $ git config -- global alias . qlog \
2 " log -- pretty = oneline "
3 $ git qlog
4 a4394d28e ... Second commit
5 80 eafd7e6 ... First commit

Listing 15: Complex alias

18/20 Code and data management with Git 14-06-2019


GitLab and GitHub

Demo time!

19/20 Code and data management with Git 14-06-2019


Acknowledgements

Martijn Vermaat
Wibowo Arindrarto
Szymon Kielbasa
Jeroen Laros

http://git-scm.com/book

You might also like