git

Lesson 2
 
 

LEARning goals

  1. Initialize a local repository
  2. Perform basic operation on a local repository (add, remove, stage a file. Perform a (partial) commit with a descriptive message)
  • Git installation

  • Repository initialization

  • Commit

  • Add, stage, remove and move files

  • Show diff

OUTLINE

Installing Git


Installing Git


Windows
http://git-scm.com/downloads

Linux
Debian/Ubuntu: sudo apt-get install git

OS X

http://code.google.com/p/git-osx-installer
...or via MacPorts

INSTALLing git gui

Windows

http://www.syntevo.com/smartgit/
http://www.sourcetreeapp.com/

Linux

http://www.syntevo.com/smartgit/

Configuration

 

Identity

$ git config --global user.name "John Doe"
                
$ git config --global user.email johndoe@example.com
            

 

Editor

$ git config --global core.editor nano

 

Colors

$ git config --global color.ui true
                

            

 

configuration


ConfiGURATION

ALIAS

Add your favorite aliases to the .gitconfig file located in your home folder

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  type = cat-file -t
  dump = cat-file -p

AUTO-Completion

To enable git commands auto-completion in the bash shell download git-completion.bash to

/opt/local/etc/bash_completion.d (MAC)

/etc/bash_completion.d/ (LINUX)

Creating a Repository


$ git init

CREATING A REPOSITORY


Show Status


$ git status

SHOW STATUS


Tracking files

 
SPECIFIC Files:
git add README.md
 
GROUP OF FILES:
$   git add   *.py
 

tracking files

TRACKING FILES


TRACKING FILES



staging files


git add *.py
git add README.rst

STAGING FILES


SPECIFIC FILES:
git add README.rst

GROUP OF FILES:
$   git add   *.py

It's the same procedure used to add new files!

STAGING FILES


STAGING FILES


Creating a Commit


Specific changes:
$ git add *.py
$ git add README.rst
$ git commit -m 'First commit'

All changes:
$ git commit -am 'First commit'

creating a commit

creating a commit

CREATING A COMMIT

After the commit (if all the modification has been commmitted)  the repository is again in a clean state

 

commit manual of style

  • Each commit should contain small atomic changes
    ( A commit can consist of a single modified line)
    • Examples
      • Add a method
      • Fix a bug
      • Add the initial version of a new file
  • You should commit very often
  • The commit message should be very explanatory, so that the other users can understand why that modification has been made and how it has been implemented
    • The messages can be used later to understand the code with git blame!

commit manual of style

BAD PRACTISES
  • Avoid situation where thousand of modified lines on tons of different files are committed together
  • Avoid commit messages like
    • "Update bunch of files"
    • "I am adding this..", "I am doing this.." (use imperative form)

Show log


Entire (paged)
$ git log

Date filtering
$ git log --since=2.weeks
$ git log --since="2 years 1 day 3 minutes ago"

SHOW LOG


SHOW LOG


SHOW LOG


SHOW LOG


Show Commits


Last commit
$ git show

Specific commit
$ git show 1776f5
$ git show HEAD^

SHOW Diffs


Unstaged changes
$ git diff

Staged changes
$ git diff --cached

Relative to specific revision
$ git diff 1776f5
git diff  HEAD^

SHOW DIFFS


SHOW DIFFS

Removing files


From staging area

$ git rm --cached file.py


From index and file system
$ git rm file.py

REMOVING FILES


REMOVING FILES


REMOVING FILES


REMOVING FILES

Create a commit to remove the file from the git repository

REMOVING FILES

 
  • Removing a file manually is not enough to remove it from git
  • We can still recover the file!

Moving files


Git tracks content, not files. Although there is a move command...

$ git mv file1 file2

...this is the same as...

$ mv file1 file2

$ git rm file1

$ git add file2

Partial commit

Partial commit​​

UNDOING THINGS

 
CHANGE LAST COMMIT

git commit --amend

 

UNSTAGE STAGED FILE

git reset HEAD file.py

 

UNMODIFY MODIFIED FILE (Discard changes)

git checkout -- file.py

UNDOING THINGS

CHANGE LAST COMMIT

 

UNDOING THINGS

Git doesn't delete commits

 

UNDOING THINGS

UNSTAGE STAGED FILE

 

UNDOING THINGS

UNMODIFY MODIFIED FILE

LEARning goals

  1. Initialize a local repository
  2. Perform basic operation on a local repository (add, remove, stage a file. Perform a (partial) commit with a descriptive message)

CreditS

This presentation was originally made by
Danilo Bargen
 
Revised by
Simone Gaiarin
 
 

Git - Lesson 2

By Simone Gaiarin

Git - Lesson 2

init and commit

  • 838