git

Lesson 3
 
 

LEARning goals

  1. Describe what a version control system is and list the main advantages of using it
  2. Understand the file status lifecycle
  3. Initialize a local repository
  4. Create commits with a structured commit message

OUTLINE

PART 1 - THEORETICAL ASPECTS

  • Version control system. Definition and history.

  • File status life cycle. From untracked to committed.

PART 2 - HANDS ON

  • Git installation

  • Repository initialization

  • First commit

GIT internals

  • A whole copy of a file is done when this is modified
  • When we want to retrieve a specific version of the project, we just copy to the tree structure corresponding to that version in the working directory

GIT internals

commit structure

Git Internals

Chain of commits

Branches


Branches are "Pointers" to commits.


Diverging Branches


Branches can diverge.


Merging Branches


Branches can be merged.


Usually painless


Merge conflicts


• Different auto-merge strategies
(fast-forward, 3 way, etc...)
• If it fails, fix by hand
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

•  Then mark as resolved and trigger merge commit

$ git add index.html
            
$ git commit

Rebasing

  • Linear alternative to merging
  • Rewrites tree!  Never rebase published code!

Common Workflow



Workflow 1


  • Only commit stable code to master
  • Commit unstable to develop
  • Deploy hotfixes to master

Workflow 2


  • Commit development code to master
  • Create tags for stable releases
  • Branch from stable tag for hotfixes

Topic Branches


  • AKA feature branches
  • For each feature, c reate a topic branch
  • Merge early, merge often
  • If desired, squash commits

git flow


Main branches

  • Two main branches with an infinite lifetime
    • (master) Production-ready state
    • (develop) Development changes for the next release

Feature branches

  • Used to develop new features for the upcoming or a distant future release

Feature branches

  • Merging without fast-forward a voids losing information about the historical existence of a feature branch

release branches

  • Support preparation of a new production release
  • Allow for minor bug fixes and preparing meta-data for a release (version number, build dates, etc.)
  • All features that are targeted for the release-to-be-built must be merged in to develop at this point in time

Hotfix branches

  • Meant to prepare for a new production release, albeit unplanned
  • Arise from the necessity to act immediately upon an undesired state of a live production version (critical bugs)

Cloning a Repository


$ git clone https://github.com/dbrgn/fahrplan

CLONING A REPOSITORY


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


Undoing Things

 
Change last commit
$ git commit --amend
 
Unstage staged file
$ git reset HEAD file.py
 
Unmodify modified file
$ 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

UNDOING THINGS

Reset the whole repository to a previous state

UNDOING THINGS

RESET THE WHOLE REPOSITORY TO A PREVIOUS STATE

UNDOING THINGS

RESET THE WHOLE REPOSITORY TO A PREVIOUS STATE
  • Choosing Hard Reset the whole repository will look like the selected commit
  • Careful!! Any uncommitted changes will be lost!
  • Use Soft or Mixed reset

.gitignore

  • Blank lines or lines starting with # are ignored
  • Standard glob patterns work
  • End pattern with slash (/) to specify a directory
  • Negate pattern with exclamation point (!)


$ cat .gitignore
                
*.pyc
*.swp
/build/ /doc/[abc]*.txt
.pypirc
*.egg-info

Remotes

  • Other clones of the same repository
  • Can be local (another checkout) or remote (coworker, central server)
  • There are default remotes for push and pull

            $ git remote -v
origin git://github.com/schacon/ticgit.git (fetch)
origin git://github.com/schacon/ticgit.git (push)
        

Branch Management


Show All Branches
$ git branch
            
iss53
* master
testing

Show last Branch commits
$ git branch -v
            
  iss53 93b412c fix javascript issue
* master 7a98805 Merge branch 'iss53'
  testing 782fd34 add scott to the author list in the readmes

Branch Management


Merged branches
$ git branch --merged
            
  iss53
* master

Unmerged branches
$ git branch --no-merged
            
  testing


Branch Management


Create new branch
$ git branch iss53
$ git checkout -b iss53 master

Switch Branch
$ git checkout iss53

Delete Branch
$ git branch -d iss53

BRANCH MANAGEMENT

The file we see in the working directory are different depending on the current branch

Push to remote


Without default
$ git push <remote> <rbranch>

Setting A default
$ git push -u <remote> <rbranch>

Then...
$ git push

Pull from Remote


Fetch & Merge

$ git pull  [<remote> <rbranch>]


Fetch & Rebase

$ git pull --rebase [<remote> <rbranch>]

-> Rebasing can be dangerous!

Create Tags


Lightweight tags
$ git tag v0.1.0

Annotated tags
$ git tag -a v0.1.0 -m 'Version 0.1.0'

GPG-Signed tags
$ git tag -s v0.1.0 -m 'Signed version 0.1.0'
$ git tag -v v0.1.0

Stashing


Move changes to a separate "stash".

$ git stash
$ git stash pop
$ git stash list
$ git stash apply
$ git stash drop
$ git stash clear

Advanced Git


Partial add
$ git add -p file.py

Bisecting
$ git bisect start
$ git bisect bad
$ git bisect good v1.0

Advanced Git


Rewrite commits

$ git filter-branch --env-filter
'if [ $GIT_AUTHOR_EMAIL = gezuru@gmail.com ]; then GIT_AUTHOR_EMAIL=danilo.bargen@webrepublic.ch; fi; export GIT_AUTHOR_EMAIL'

Resources


CreditS

This presentation was originally made by
Danilo Bargen

Revised by
Simone Gaiarin

The 3 slides on the Git definition are taken from the presentation " Introduction to Git" by Matthew McCullough



Git - Lesson 3

By Simone Gaiarin

Git - Lesson 3

A basic introduction to git

  • 839