Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Code and data management with Git

The git commit graph


The git commit graph

Table of contents

The git commit graph

Inspecting the commit graph

Navigating the commit graph

Manipulating the commit graph

Basic merge conflicts

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


The git commit graph

A linear history

12f82a1 H

Committing creates a new


2f5458a
commit on top of current one.

Parent of Every commit has a parent.


Time

1d438b8

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


The git commit graph

A linear history

2f5458a H

2f5458a

1d438b8

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


The git commit graph

The default master branch

A branch is a pointer to a commit.

master The default branch.

2f5458a H

2f5458a

1d438b8

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


The git commit graph

The HEAD pointer

master HEAD Points to the current branch / commit.

2f5458a
The working directory reects
the les state from the commit
to which the HEAD points to.

2f5458a

1d438b8

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


The git commit graph

Committing
Committing moves the current
master HEAD
branch to the new commit.
12f82a1

H  
it.

master HEAD

2f5458a

2f5458a

1d438b8

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


The git commit graph

Committing
Committing moves the current
master HEAD
branch to the new commit.
12f82a1

   it.

2f5458a

2f5458a

1d438b8

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


The git commit graph

A non-linear history

master HEAD some feature


12f82a1 33215af

2f5458a 441aa8c

S"#$
parent

2f5458a

The commit graph can become no !.

1d438b8

8/54 Code and data management with Git 14-06-2019


The git commit graph

Use branches to keep track of different code paths


another feature Branch names:
4564aaf - are easier to remember than commits ids.
- make it clear what commits are all about.

master HEAD some feature


11a5645 12f82a1 33215af

2f5458a 441aa8c

2f5458a

1d438b8

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


The git commit graph

Merging
another feature master HEAD Bringing together commits
4564aaf a12a223 from di%erent branches.

some feature
11a5645 12f82a1 33215af

2f5458a 441aa8c

2f5458a

1d438b8

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


Inspecting the commit graph

Table of contents

The git commit graph

Inspecting the commit graph

Navigating the commit graph

Manipulating the commit graph

Basic merge conflicts

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


Inspecting the commit graph

An example repository

master HEAD

c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

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


Inspecting the commit graph

Showing the current branch

$ git status
On branch master
nothing to commit, working directory clean.

Remember: you cannot type git status enough!

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


Inspecting the commit graph

Listing branches

$ git branch
interface
license
* master

Three branches, current branch is master.

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


Inspecting the commit graph

Listing branches

$ git branch
interface
license
* master

Three branches, current branch is master.

$ git branch -v
interface 8fc25c1 Trivial Python interface
license 0fbe3e3 Add MIT license
* master c7f3bd9 Add .gitignore file

-v: Shows the commit each branch points to.

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


Inspecting the commit graph

The commit log

$ git log --oneline --decorate


c7f3bd9 (HEAD -> master) Add .gitignore file
4a44c4e Merge branch ’license’
64af1ee Add course teachers to README
0fbe3e3 (license) Add MIT license
d1c7fd7 Initial commit with README

--oneline: Shows commit summary on one line.


--decorate: Adds branch information.

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


Inspecting the commit graph

The commit log as a graph

$ git log --oneline --decorate --graph --all


* 8fc25c1 (interface) Trivial Python interface
| * c7f3bd9 (HEAD -> master) Add .gitignore file
|/
* 4a44c4e Merge branch ’license’
|\
| * 0fbe3e3 (license) Add MIT license
* | 64af1ee Add course teachers to README
|/
* d1c7fd7 Initial commit with README

--graph: Shows the commit graph.


--all: Includes all branches instead of just the current one.
16/54 Code and data management with Git 14-06-2019
Navigating the commit graph

Table of contents

The git commit graph

Inspecting the commit graph

Navigating the commit graph

Manipulating the commit graph

Basic merge conflicts

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


Navigating the commit graph

Switching to another branch

$ git checkout interface


Switched to branch ’interface’

• Points HEAD to the named branch.


• Updates your working directory.

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


Navigating the commit graph

Switching to another branch

$ git checkout interface


Switched to branch ’interface’

• Points HEAD to the named branch.


• Updates your working directory.

$ git status
On branch interface
nothing to commit, working directory clean.

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


Navigating the commit graph

Switching to another branch

master HEAD

c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

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


Navigating the commit graph

Switching to another branch

master
c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

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


Navigating the commit graph

Detached HEAD state

$ git checkout 4a44c4e


Note: checking out ’4a44c4e’.

You are in ’detached HEAD’ state. You can look ...

If you want to create a new branch to retain


commits you create, you may do so (now or later) by
using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 4a44c4e... Merge Branch ’license’

21/54 Code and data management with Git 14-06-2019


Navigating the commit graph

Detached HEAD state

$ git status
HEAD detached at 4a44c4e
nothing to commit, working directory clean.

22/54 Code and data management with Git 14-06-2019


Navigating the commit graph

Detached HEAD state

master
c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

23/54 Code and data management with Git 14-06-2019


Navigating the commit graph

Detached HEAD state

master
c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

24/54 Code and data management with Git 14-06-2019


Navigating the commit graph

Detached HEAD state

master
c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

25/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Table of contents

The git commit graph

Inspecting the commit graph

Navigating the commit graph

Manipulating the commit graph

Basic merge conflicts

26/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

$ git branch lib

Creates branch lib at HEAD.

27/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

$ git branch lib

Creates branch lib at HEAD.


How can I see that it worked?

27/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

$ git branch lib

Creates branch lib at HEAD.


How can I see that it worked?

$ git branch
interface
lib
license
* master

27/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

$ git branch lib

Creates branch lib at HEAD.


How can I see that it worked?

$ git branch
interface
lib
license
* master

How can I switch to the new lib branch?

27/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

$ git checkout lib


Switched to branch ’lib’

28/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch


For Git pro’s, in one command:

$ git checkout -b lib


Switched to a new branch ’lib’

At this point, lib is just a label to the same commit as master.

29/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Creating a new branch

master lib HEAD

c7f3bd9
Add .gitignore file interface
8fc25c1
Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee
Add course teachers license
to README
0fbe3e3
Add MIT license

d1c7fd7
Initial commit with README

30/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Working on a branch
We can extend the lib branch by adding commits.

$ echo ’VERSION=1’ > testlib.py


$ git add testlib.py
$ git commit -m ’Add empty testlib’

31/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Working on a branch
We can extend the lib branch by adding commits.

$ echo ’VERSION=1’ > testlib.py


$ git add testlib.py
$ git commit -m ’Add empty testlib’

$ git log --oneline --decorate --graph --all


* 251a51b (HEAD -> lib) Add empty testlib
* c7f3bd9 (master ) Add .gitignore file
| * 8fc25c1 (interface) Trivial Python interface
|/
* 4a44c4e Merge branch ’license’
...

31/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Extended lib branch


lib HEAD

251a51b
Add empty testlib

master interface
c7f3bd9 8fc25c1
Add .gitignore file Trivial Python interface

4a44c4e
Merge Branch 'license'

license
64af1ee 0fbe3e3
Add course teachers Add MIT license
to README

d1c7fd7
Initial commit with README

32/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Fast forward merging


Let’s merge our work on lib back into master.

$ git checkout master


Switched to branch ’master’
$ git merge lib
Updating c7f3bd9..251a51b
Fast-forward
testlib.py | 1 +
1 file changed, 1 insertion(+)
create mode 100644 testlib.py

33/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Merged branch lib into master (fast forward).


lib master HEAD

251a51b
Add empty testlib

master HEAD interface


c7f3bd9 8fc25c1
Add .gitignore file Trivial Python interface

4a44c4e
Merge Branch 'license'

license
64af1ee 0fbe3e3
Add course teachers Add MIT license
to README

d1c7fd7
Initial commit with README

34/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Three-way merging
Let’s merge our work on interface back into master.

$ git merge interface


Merge made by the ’recursive’ strategy.
interface.py | 3 +++
1 file changed, 3 insertions(+)
create mode 100755 interface.py

This merge was harder:


• interface and master had diverged.
• Git determines changes in interface and master since their
most recent common ancestor.
• A new commit is created.
35/54 Code and data management with Git 14-06-2019
Manipulating the commit graph

Merged branch interface into master


master HEAD

fc054a6
Merge branch 'interface'
into characters

lib master HEAD

251a51b
Add empty testlib

interface
c7f3bd9 8fc25c1
Add .gitignore file Trivial Python interface

4a44c4e
Merge Branch 'license'

license
64af1ee 0fbe3e3
Add course teachers Add MIT license
to README

36/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Merged branch interface into master


master HEAD

fc054a6
Merge branch 'interface'
into characters

lib
251a51b
Add empty testlib

interface
c7f3bd9 8fc25c1
Add .gitignore file Trivial Python interface

4a44c4e
Merge Branch 'license'

license
64af1ee 0fbe3e3
Add course teachers Add MIT license
to README

37/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Note
Make sure that the working directory is clean before merging.
Otherwise:

$ git merge lib


error: Your local changes to the following files
would be overwritten by merge:
testlib.py
Please, commit your changes or stash them before
you can merge.
Aborting

38/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Deleting branches
Old branches that have been merged can be deleted.

$ git branch -d interface


Deleted branch interface (was 8fc25c1).

$ git branch -d lib license


Deleted branch lib (was 251a51b).
Deleted branch license (was 0fbe3e3).

No history is lost, just labels removed.

39/54 Code and data management with Git 14-06-2019


Manipulating the commit graph

Deleting branches

master HEAD

fc054a6
Merge branch 'interface'
into characters

251a51b
Add empty testlib

c7f3bd9 8fc25c1
Add .gitignore file Trivial Python interface

4a44c4e
Merge Branch 'license'

64af1ee 0fbe3e3
Add course teachers Add MIT license
to README

d1c7fd7
Initial commit with README
40/54 Code and data management with Git 14-06-2019
Basic merge conflicts

Table of contents

The git commit graph

Inspecting the commit graph

Navigating the commit graph

Manipulating the commit graph

Basic merge conflicts

41/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Merge conflicts
Git is pretty good at merging:
• The changes might have been in different files.
• Or in different parts of the same file.
• Git tries to figure out a sensible result.

42/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Merge conflicts
Git is pretty good at merging:
• The changes might have been in different files.
• Or in different parts of the same file.
• Git tries to figure out a sensible result.

Sometimes this is not possible:


• The changes might be incompatible.
• When we try git merge, Git gives up.
• This situation is called a merge conflict.

42/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Setting the stage (1/3)

$ git log --oneline --decorate --graph --all


* 5edaf08 (hue) State character ...
| * f1ef19c (HEAD, master) State character ...
|/
* 1f6d2ab Initial commit

We’d like to merge branch hue into master.

43/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Setting the stage (2/3)


The last commit on master:

$ git show
f1ef19c State character preference
diff --git a/FACTS.md b/FACTS.md
index de15194..ef40359 100644
--- a/FACTS.md
+++ b/FACTS.md
@@ -1,2 +1,4 @@
Facts about television series
=============================
+
+My favorite character is Eric Cartman.

44/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Setting the stage (3/3)


The last commit on hue:

$ git show --oneline hue


5edaf08 State character preference
diff --git a/FACTS.md b/FACTS.md
index de15194..5e69508 100644
--- a/FACTS.md
+++ b/FACTS.md
@@ -1,2 +1,4 @@
Facts about television series
=============================
+
+My favorite character is Milhouse.

45/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Creating a merge conflict

$ git merge hue


Auto-merging FACTS.md
CONFLICT (content): Merge conflict in FACTS.md
Automatic merge failed; fix conflicts and then
commit the result.

46/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Creating a merge conflict

$ git merge hue


Auto-merging FACTS.md
CONFLICT (content): Merge conflict in FACTS.md
Automatic merge failed; fix conflicts and then
commit the result.
$ git status
# On branch master
# Unmerged paths:
# (use "git add/rm <file>.." as appropriate)
# to mark resolution)
# both modified: FACTS.md
# no changes added to commit (use "git add" and/or
# "git commit -a")

47/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Resolving a merge conflict (1/3)

$ cat FACTS.md
Facts about television series
=============================
S
<<<<<<< HEAD
My favorite character is Eric Cartman.
=======
My favorite character is Milhouse.
>>>>>>> hue

What we had is under HEAD, what hue had is above hue.

48/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Resolving a merge conflict (2/3)


We resolve the conflict by hand.

$ nano FACTS.md
$ cat FACTS.md
Facts about television series
=============================
S
My favorite characters are Eric Cartman
and Milhouse.

49/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Resolving a merge conflict (3/3)


And can now finish the merge commit.

$ git add FACTS.md


$ git commit
[master 1e496cb] Merge branch ’hue’

50/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Aborting a merge
If you don’t feel like resolving the merge conflict, you can go back
with git merge --abort.

$ git merge hue


Auto-merging FACTS.md
CONFLICT (content): Merge conflict in FACTS.md
Automatic merge failed; fix conflicts and then
commit the result.
$ git merge --abort
$ git status
# On branch master
# nothing to commit (working directory clean)

51/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Resolving conflicts with git mergetool


We can also use graphical merge tools such as Meld.

$ git mergetool
merge tool candidates: meld opendiff kdiff3 ...
Merging:
FACTS.md
S
Normal merge conflict for ’FACTS.md’:
sslocal: modified file
ssremote: modified file
Hit return to start merge resolution tool (meld):

52/54 Code and data management with Git 14-06-2019


Basic merge conflicts

Meld example

Tools like Meld provide an editable three-way diff.

53/54 Code and data management with Git 14-06-2019


Acknowledgements

Martijn Vermaat
Wibowo Arindrarto
Szymon Kielbasa
Jeroen Laros

http://git-scm.com/book
https://www.atlassian.com/git

You might also like