intro to git and git hub

61
Introduction to Git and GitHu Venkat Malladi

Upload: venkat-malladi

Post on 12-Apr-2017

105 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Intro to git and git hub

Introduction to Git and GitHubVenkat Malladi

Page 2: Intro to git and git hub

Outline 

I. Introduction to Git A. What is Git?B. Git workflow: Creating a new repoC. HEADD. Basic Git commandsE. Concept of branchesF. Creating a branch/switching between branchesG. Merging branches and resolving conflicts

H. Concept of stashing

II. Introduction to GitHubA. What is GitHub? Basic GitHub conceptsB. GitHub in practice: Distributed version controlC. Cloning a remote repoD. Fetching/Pushing to a remote repoE. Collaborating using Git and GitHub

Page 3: Intro to git and git hub

What is a ‘version control system’?• a way to manage files and directories

• track changes over time

• recall previous versions

• ‘source control’ is a subset of a VCS.

Page 4: Intro to git and git hub

What is Git?

Page 5: Intro to git and git hub

What is Git?

• created by Linus Torvalds, April 2005

• a command line version control program

• uses checksums to ensure data integrity

• cross-platform

• open source, free

• distributed version control

Page 6: Intro to git and git hub

Distributed version control• No central server

• Every developer is a client, the server and the repository

Source: http://bit.ly/1SH4E23

Page 7: Intro to git and git hub

Git distributed version control• no need to connect to central server

• can work without internet connection

• no single failure point

• developers can work independently and merge their work later

• every copy of a Git repository can serve either as the server or as a client and has complete history

• Git tracks changes, not versions

• Bunch of little change sets floating around

Page 8: Intro to git and git hub

What is a repository?• “repo” = repository

• usually used to organize a single project

• repos can contain folders and files, images, videos, spreadsheets, and data sets – anything your project needs

Page 9: Intro to git and git hub

Git uses a three-tree architecture

Staging index

Working copy

Repository

checkout

   add

commit

Page 10: Intro to git and git hub

Before starting to use git• Setup your name and email so others can know who committed

changes.

• git config - -global user.name ”name”• git config - -global user.email ”email”

Note: set for all repositories on your computer

• git config - -local user.email ”email” Note: can set differently for each repository

Page 11: Intro to git and git hub

A simple Git workflow1. Initialize a new project in a directory:

git init

2. Add a file using a text editor to the directory

3. Add every change that has been made to the directory:

git add <filename>.

4. Commit the change to the repo:

git commit –m “important message here”

Page 12: Intro to git and git hub

After initializing a new git repo…

add

commit

Make changes to files

Add local changes

Commit changes withmessage

Staging index

Working copy

Repository

Page 13: Intro to git and git hub

Creating a new repository• git init

• git add <filename>

• git commit –m “important message here”

Page 14: Intro to git and git hub

Commit messages• Tell what it does (present tense)

• Single line summary followed by blank space followed by more complete description

• Keep lines to <= 72 characters

• Ticket or bug number helps

Page 15: Intro to git and git hub

Commit Messages Structure

Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary.  Wrap it to about 72characters or so.  In some contexts, the first line is treated as thesubject of an email and the rest of the text as the body.  The blankline separating the summary from the body is critical (unless you omitthe body entirely); tools like rebase can get confused if you run thetwo together.

Further paragraphs come after blank lines.

  - Bullet points are okay, too

  - Typically a hyphen or asterisk is used for the bullet, preceded by a    single space, with blank lines in between, but conventions vary here

Source:http://git-scm.com/book/ch5-2.html

Page 16: Intro to git and git hub

Good and bad examplesBad: “Typo fix”Good: “Add missing / in CSS section”

Bad: “Fix syntax”Good: “Fix miscellaneous PEP8 issues”

Page 17: Intro to git and git hub

How to see what was done• git log

Page 18: Intro to git and git hub

Checksumsgenerated by SHA1 encryptionalgorithm

“SHAs”

Page 19: Intro to git and git hub

The HEAD pointer• points to a specific commit in repo

• as new commits are made, the pointer changes

• HEAD always points to the “tip” of the currently checked-out branch in the repo

Page 20: Intro to git and git hub

HEAD

Master

f30ab34ac298ca9

Page 21: Intro to git and git hub

Which files were changed and where do they sit in the three tree?

• git status – allows one to see where files are in the three tree scheme

Repo

Workingcopy

Stagingindex

add

commit

Page 22: Intro to git and git hub

Committing and adding message• git commit -a

• Allows one to add to staging index and commit at the same time

• Grabs everything in working directory

• Files not tracked or being deleted are not included

Page 23: Intro to git and git hub

• git diff – compares changes to files between repo and working directory

What changed in working directory?

Note: git diff --staged - compares staging index to repo

Note: git diff filename can be used as well

Line numbers in file

RemovedAdded

Repo

Workingcopy

Stagingindex

add

commit

Page 24: Intro to git and git hub

Difference between commits• git diff commit_1 commit_2

When using checksum of older commit, will show you all changes compared to those in your working directory

Page 25: Intro to git and git hub

Deleting files from the repo• git rm filename.txt

• moves deleted file change to staging area

• It is not enough to delete the file in your working directory. You must commit the change.

Repo

Workingcopy

Stagingindex

add

commit

Page 26: Intro to git and git hub

Moving (renaming) files• git mv filename1.txt filename2.txt

Note: File file1.txt was committed to repo earlier.

Repo

Workingcopy

Stagingindex

add

commit

Page 27: Intro to git and git hub

• git init• git status• git log• git add• git commit• git diff• git rm• git mv

Common workflowRepo

Workingcopy

Stagingindex

add

commit

Page 28: Intro to git and git hub

What if I want to undo changes made to working directory?

• git checkout something (where “something” is a file or an entire branch)

• git checkout will grab the file from the repo removing

all changes since last commit

• Example: git checkout -- file1.txt (“checkout file ‘file1.txt’ from the current branch”)

checkout

Repo

Workingcopy

Stagingindex

add

commit

Page 29: Intro to git and git hub

What if I want to undo changes added to staging area?

• git reset HEAD filename.txt

reset

Repo

Workingcopy

Stagingindex

add

commit

Page 30: Intro to git and git hub

Obtain older versions

• git checkout SHA1 -- filename.txt

Note: Checking out older commits places them into the staging area

checkout

Repo

Workingcopy

Stagingindex

add

commit

Page 31: Intro to git and git hub

Branches in Git• allows one to try new ideas

• If an idea doesn’t work, throw away the branch. Don’t have to undo many changes to master branch

• If it does work, merge ideas into master branch.

• Note: There is only one working directory

Page 32: Intro to git and git hub

Branching and merging example

HEAD

f30ab34ac298ca9

52ef3 S32d3

mN34i

HEAD

master

test_branchmerge

Page 33: Intro to git and git hub

Source: http://hades.github.io/2010/01/git-your-friend-not-foe-vol-2-branches/

Page 34: Intro to git and git hub

What branch am I on?• git branch

Page 35: Intro to git and git hub

How do I create a new branch?• git checkout -b new_branch_name

Note: At this point, both HEADs of the branches are pointing to the same commit (that of master)

Page 36: Intro to git and git hub

How do I switch branches?• git checkout branch_name

At this point, one can switch between branches, making commits, etc. in eitherbranch, while the two stay separate from one another.

Note: In order to switch to another branch, your current working directory must be clean (no conflicts, resulting in data loss).

Page 37: Intro to git and git hub

Comparing branches• git diff first_branch..second_branch

Page 38: Intro to git and git hub

How do I merge a branch?• git merge branch_to_merge

From the branch into which you want to merge another branch….

Note: Always have a clean working directory when merging

Page 39: Intro to git and git hub

“fast-forward” merge occurs when HEAD of master branch is seen when looking back

“recursive” merge occurs by looking back and combining ancestors to resolve merge

34ac298ca9

52ef3 S32d3

mN34i

f30ab34ac298ca9

52ef3 S32d3

mN34i

Page 40: Intro to git and git hub

Merge conflictsWhat if there are two changes to same line in two different commits?

apple

master new_feature

banana

file1.txt file1.txt

Page 41: Intro to git and git hub

Resolving merge conflictsGit will notate the conflict in the files!

Solutions:1. Abort the merge using git merge –abort2. Manually fix the conflict

Page 42: Intro to git and git hub

Graphing merge history• git log --graph --oneline --all --decorate

Page 43: Intro to git and git hub

Tips to reduce merge pain• merge often

• keep commits small/focused

• bring changes occurring to master into your branch frequently (“tracking”)

Page 44: Intro to git and git hub

Renaming and deleting branches• git branch –m/--move old_name new_name

• git branch –d branch_name Note: Must not be in branch_name Note: Must not have commits in branch_name unmerged in branch from which you are deleting

• git branch –D branch_name Note: If you are really sure that you want to delete branch with commits

Page 45: Intro to git and git hub

Fast context switching• git stash

Allows you to save your work during work.

Using git stash will allow you to maintain the state of your current workingdirectory and saves it to an temporary index that you can reapply later.

Repo

Workingcopy

Stagingindex

add

commit

stash

Stashingindex

Note: Stashing is used for switching quickly betweenbranches when you have to work on multiple different tasks. Don’t leave the code in stashfor a long period of time.

Page 46: Intro to git and git hub

Stashing your changes• git stash save message

Note: if you don’t use a message comment, git automatically creates it from the last commit message. This can be confusing and lead to errors.

• git stash list Note: shows your list of stashes, similar to git log

Page 47: Intro to git and git hub

Recovering your changes• git stash pop index

Note: throws away the stash index after applying changes to working directory

• git stash apply index Note: leaves stash in index list, but applies changes to working directory

Page 48: Intro to git and git hub

Branching from stash• git stash branch branch_name index

Note: If you plan on working on a new feature for a long time period, create a branch from stashed changes

Page 49: Intro to git and git hub

What is ?

Page 50: Intro to git and git hub

What is GitHub

Page 51: Intro to git and git hub

GitHub• a platform to host code repositories

• http://github.com

• launched in 2007

• most popular Git host

• allows users to collaborate on projects from anywhere

• GitHub makes git social!

• Free to start (can pay for private repositories and additional features)

Page 52: Intro to git and git hub

Source: https://medium.com/@abhishekj/an-intro-to-git-and-github-1a0e2c7e3a2f#.b9qbciy7e

Page 53: Intro to git and git hub
Page 54: Intro to git and git hub

Copying (cloning) files from remote repo to local machine

• git clone URL <new_dir_name>

• HTTPS

• SSH

Page 55: Intro to git and git hub

How do I link my local repo to a remote repo?

• git remote add <alias> <URL>

Note: This just establishes a connection…no files are copied/moved

Note: Yes! You may have more than one remote linked to your local directory!

Which remotes am I linked to?• git remote -v

Page 56: Intro to git and git hub

Pushing to a remote repo• git push remote_name branch_name

Page 57: Intro to git and git hub

Getting changes from a remote repo

• git fetch remote_repo_name

• Fetch in no way changes a your working dir or any commits that you’ve made.

• git merge must be done to merge fetched changes into local branch

• git pull remote_repo_name

• Fetch and merge changes to local branch and working directory

Page 58: Intro to git and git hub

Tagging• git tag

• Git has the ability to tag specific points in history as being important, such as releases versions

    (v.1.0, 2.0, …)

Page 59: Intro to git and git hub

Types of tagsTwo types of tags: • lightweight – a pointer to a specific comment – basically a SHA

stored in a file

git tag tag_name

• annotated – a full object stored in the Git database – SHA, tagger name, email, date, message and

can be signed and verified with GNU Privacy Guard (GPG)

git tag –a tag_name –m “message”

Page 60: Intro to git and git hub

Issue Tracking

Page 61: Intro to git and git hub

Good resources • Git from Git: https://git-scm.com/book/en/v2

• A number of easy-to-understand guides by the GitHub folks: https://guides.github.com

• Try Github: https://try.github.io/

• Bitbucket, alternative code hosting: https://bitbucket.org/