Intro to Git

 

Why learn git?

My software never has bugs. 
It just develops random features.

http://explainxkcd.com/wiki/index.php/1597:_Git

So what is it?

Git is a distributed version control system that was made with an emphasis on performance
(speed and data integrity)

Agenda

 
  • Install

  • Review basic commands

  • 2 more commands

  • Git workflows

  • Hands on

 

Install git

Windows

 
  • Install git (with bash)
    https://git-scm.com/download/win
  • Install Cmder
    https://github.com/cmderdev/cmder
  • Install Cygwin
    https://cygwin.com/install.html
 

Mac

Comes pre-installed

 

Linux

Should come pre-installed

  • Debian-based
    sudo apt-get install git-all
  • Fedora variant 
    sudo yum install git-all
 

Initialize repository

 
$  git init [project-name]
 

creates a new Git repository

 

* if repo does not already exist

re·pos·i·to·ry

noun

  • a place, building, or receptacle where things are or may be stored.
  • COMPUTING 

    a central location in which data is stored
    and managed.
    "the metadata will be aggregated in a repository"

git remote

 

Connects your local repository to a remote instance
(eg. Github, BitBucket, personal) 

 

* if repo does not already exist

$ git remote add origin [url]

git clone

 

Copies existing repository files to your machine

 

NOTES:

  • Will create a new folder with the repo's name
  • Requires an existing repo
 
$  git clone [url]

Adding & Removing Files

 

 

Add or remove files to a
"staging index" for commiting

 
$  git add [file]
$  git rm [file]
$  git add .

Shortcut to add all
changed files

Question:

why use                               instead of                             ?

git add .            git add *

To be answered later...

$  git commit -m "[message]"

Commit your changes

 

Saves a snapshot of the changes ("delta")
made to your files and when.
The message should be relevant and significant.

C0
C1

A Visualization

https://explainxkcd.com/wiki/index.php/1296:_Git_Commit

Ignore some stuff

 

Let git forget about and ignore non-critical files.

$  touch .gitignore
$  git add .gitignore
$  git commit -m "Add gitignore"

What is non-critical?

  • development config files
  • log files
  • 'secret' files
  • mock data
  • thumbnails
    • Thumbs.db, .DS_Store, ._*
  • IDE config files
    • *.sublime-workspace, *.idea/

Question:

why use                       instead of                       ?

git add .           git add *

The latter does not respect the .gitignore!

Push your changes

 

Update remote repo with associated changes

$  git push

Push implicitly points to master

 

You could specify where to point your changes to

$  git push origin master
$  git push -u origin otherremote

but you probably will never need to.

Get what has changed

 

This changes your local files.

$  git pull

                    implicitly does a                         and then

git pull              git fetch         git merge

Check what has changed

 

Reports what changes have been made to the remote.

This does not change your local files.

$  git fetch

Branch out

 

Branch early, and branch often!

$  git branch new-branch-name

Simply points to a specific commit from history.

Default branch is called                 

master
$  git branch -b newbranch

is shorthand for

$ git branch newbranch
  $ git checkout newbranch

checkout switches to the specified branch and updates the working directory

C0

A Visualization

C1
C2
B1

Merge back in

 
$  git merge other-branch

Simply points to a specific commit

 

Create a new branch, develop a new feature,
and combine it back in

C0

A Visualization

C1
C2
C3
B1

But... not everything
goes smoothly

 
$  git merge other-branch

 If the same part of the same file changed differently in the two branches being merged, Git can't merge them cleanly

Merge conflicts need to

be resolved

 

Git adds conflict-resolution markers so you can identify

them and manually resolve those conflicts

<<<<<<< HEAD:index.html
<div id="footer">contact : support@email.com</div>
=======
<div id="footer">
 please contact us at support@email.com
</div>
>>>>>>> newbranch:index.html

Merge conflicts need to

be resolved

 

Once resolved, simply commit  to finish the merge...

<div id="footer">contact us at support@email.com</div>
$ git commit

Merge branch 'newbranch'

Conflicts:
    index.html
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#    .git/MERGE_HEAD
# and try again.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#    modified:   index.html
#

2 more commands

 
$  git status
$  git diff [file]

Lists all new or modified files to be commited

Shows file differences not yet staged

Part II

Workflow strategies

Trunk

Feature Branch

master
feature
master
feature 1
feature 2
master
feature 1
feature 2

Protip: Merge master into feature, and then feature back to master.

Gitflow

master
develop
hotfix
release
feature

1.0.0

1.0.1

1.1.1

New repo

  • git init
  • git remote add https://...
  • git add .
  • git commit -m "msg"

Existing repo

  • git clone https://...git
  • Make some change to a file
  • git add .
  • git commit -m "msg"

Hands on

Git config

$ git config --global user.name "John Doe"
$ git config --global user.email "e@mail.co"

Additional Practice

 
  • https://www.codecademy.com/learn/learn-git
  • https://try.github.io/
 

Resources

 
  • https://git-scm.com/doc
  • https://www.pluralsight.com/courses/git-fundamentals
 

Intro to Git

By Carlos Filoteo

Intro to Git

  • 508