71
Managing Files with GIT Akshay Mathur

Working with GIT

Embed Size (px)

DESCRIPTION

Content prepared for Hands-on workshop on GIT

Citation preview

Page 1: Working with GIT

Managing Files with GIT

Akshay Mathur

Page 2: Working with GIT

@akshaymathu 2

Ground Rules• Post on FB and Twitter now• Disturb Everyone during the

session– Not by phone rings– Not by local talks– By more information and questions

Page 3: Working with GIT

@akshaymathu 3

Let’s Know Each Other

• Who does not code?• How do you store files?• Have you used VCS?– Which one?– What do you store?– Repository size?– What happens when you are not on network?

• Why are you attending?

Page 4: Working with GIT

@akshaymathu 4

Akshay Mathur

• Founding Team Member of– ShopSocially (Enabling “social” for retailers)– AirTight Neworks (Global leader of WIPS)

• 15+ years in IT industry– Currently Principal Architect at ShopSocially– Mostly worked with Startups• From Conceptualization to Stabilization• At different functions i.e. development, testing, release• With multiple technologies

Page 5: Working with GIT

What is the issue

What if I don’t care?

Page 6: Working with GIT

@akshaymathu 6

Keeping Track of Changes

• Hope you get it right first time• Hope you can remember the changes

• You will actually end up rewriting

Page 7: Working with GIT

@akshaymathu 7

Organizing Backups

• Save the files/directories appending some context (usually dates)– Totally ad-hoc– Only the owner knows where is what– Hard to pick a version to go back to– Prone to error– No tools to help

Page 8: Working with GIT

@akshaymathu 8

Sharing Work

• When many people need to work on same file– Work sequentially– Overwrite other’s changes– Keep track via emails

Page 9: Working with GIT

The Solution

Version Control System (VCS)

Page 10: Working with GIT

@akshaymathu 10

How VCS Help?

• Keeps track of changes– Tells who change what and when

• Merges changes done by different people– Detects and alerts if same thing is changed by

different people• Allows comparing two versions– Shows how information has grown

• Allows going back to previous version

Page 11: Working with GIT

@akshaymathu 11

VCS Terminology

• Repository– Place where VCS stores files and its metadata

• Sandbox– Place where files are available for editing

• Checkout– Process of getting files from repository to sandbox

• Commit– Process of putting files from sandbox to repository

Page 12: Working with GIT

@akshaymathu 12

Storage System

• Non-intrusive– Everything is stored in

different folders/files• Compact– Incremental changes are

stored– Different storage techniques• RCS, BerkleyDB etc.

Page 13: Working with GIT

@akshaymathu 13

Versioning Scheme

• File Versioning– Version number of only changed file is changed

• Repository Versioning– Version number of entire repository changes every

time• Version numbers can be– A serial number– Hash of the content1.1 1.2 1.3 1.4 1.5

Page 14: Working with GIT

@akshaymathu 14

Page 15: Working with GIT

How VCS work

Managing Concurrency Optimistically

Page 16: Working with GIT

@akshaymathu 16

Page 17: Working with GIT

@akshaymathu 17

Page 18: Working with GIT

@akshaymathu 18

Page 19: Working with GIT

@akshaymathu 19

Page 20: Working with GIT

@akshaymathu 20

Page 21: Working with GIT

@akshaymathu 21

Page 22: Working with GIT

Types of VCS

Where is the repository?

Page 23: Working with GIT

@akshaymathu 23

Centralized

• CVS, SVN etc.• Repository at only one central location– Sandbox on every working machine

• Network is needed for every operation– Can not work offline

• The repository has to be protected and backed up– Work stops if repository goes down

Page 24: Working with GIT

@akshaymathu 24

Centralized VCS

Page 25: Working with GIT

@akshaymathu 25

Centralized VCS Operations

Repository

Server Client

Sandbox

Checkout

Commit

Page 26: Working with GIT

@akshaymathu 26

Distributed

• Mercurial, Bitkeeper etc• Repository on every machine– Sandbox and repository are always together

• Network is needed only for repository sync– Can work offline

• Backups are trivial• Election of central repository by convention

Page 27: Working with GIT

@akshaymathu 27

Distributed VCS

Page 28: Working with GIT

@akshaymathu 28

Distributed VCS Operations

Repository

Remote Local

Sandbox

Pull

Push

Sandbox

Repository

Commit Checkout

Page 29: Working with GIT

@akshaymathu 29

Page 30: Working with GIT

Working with GIT

Page 31: Working with GIT

@akshaymathu 31

GIT

• Free• Open Source• Created by Linus Torvalds (Mr. Linux)• First used for Linux Kernel

Page 32: Working with GIT

@akshaymathu 32

General Info

• Distributed– All repositories hold same data/info

• Compact– Low footprint– Compact Repository

• Fast– Quick operations– Low Data Transfer

• Feature rich yet simple• Intelligent

Page 33: Working with GIT

@akshaymathu 33

Hands on Practice

Page 34: Working with GIT

@akshaymathu 34

Creating Repository

• Init– Initialize a new repository on local machine• Creates a .git directory with subdirectories for objects,

refs/heads, refs/tags, and template files• An initial HEAD file that references the HEAD of the

master branch is also created

git init <repository_name>

Page 35: Working with GIT

@akshaymathu 35

Adding New File

• Add– Adds a new file into the list of tracked files– A list of files (or wildcard) is accepted– Commit is not automatic

git add <file_1> <file_2>git add <file_*>

Page 36: Working with GIT

@akshaymathu 36

Editing the File

• No restriction from GIT• Use your favorite editor– Text files• Vim, Notepad, Textpad, Eclipse, Visual Studio,

Dreamviewer etc.

– Binary Files• Word, Excel, Powerpoint, Photoshop etc.

Page 37: Working with GIT

@akshaymathu 37

Removing Files

• Rm– Removes file from the sandbox– Remember to commit the change

git rm <file_name>

Page 38: Working with GIT

@akshaymathu 38

From Repository to Sandbox

• Checkout– Gets file(s) from repository to sandbox– Local changes of the files go away

git checkout <file_name>

Page 39: Working with GIT

@akshaymathu 39

Switching to Old Version

• Checkout– Gets file(s) from repository to sandbox• Commit ID of old version needs to be provided

– Sandbox gets detached and does not allow commits

– Command fails if local changes are there in the file

git checkout <commitID>

Page 40: Working with GIT

@akshaymathu 40

Checking Current State

• Status– Displays information about current state of sandbox

• Branch name• List of added, removed, modified and conflicted tracked

files• List of untracked files• Ignores files listed in .gitignore

git status

Page 41: Working with GIT

@akshaymathu 41

.gitignore

• A gitignore file specifies intentionally untracked files that git should ignore– Files already tracked by git are not affected

• Each line in a gitignore file specifies a pattern• Wildcard can be used

Page 42: Working with GIT

@akshaymathu 42

Reviewing Changes

• Diff– Shows difference between working copy of a file and the

copy in the repository

git diff• Difftool– Allows to use an external tool for viewing diff

git difftool –y –t xxdiff• Log– Lists all commit logs

git log

Page 43: Working with GIT

@akshaymathu 43

Saving into Repository

• Commit– Stores current content of tracked files from

sandbox into repository• A log message is required• All files can be committed with –a option• Selected files can be committed by providing list of files

git commit –am “<commit message>”git commit –m “<commit message>” <file1>

<file2>

Page 44: Working with GIT

@akshaymathu 44

Commit Object

• Commit creates a commit object with the current state of repository– Author info and timestamp is recorded– A new checkpoint is created– New version number (commit ID) is assigned to

the checkpoint• Commit ID is hash of the content in the commit

Page 45: Working with GIT

@akshaymathu 45

Marking a State of Repository

• Happens automatically with every commit• Name of the state, the commit ID, is tough to

remember– A simple name can be assigned

git tag <tag_name>git tag <tag_name> <commitID>

Page 46: Working with GIT

@akshaymathu 46

Page 47: Working with GIT

@akshaymathu 47

Handling Emergencies

Page 48: Working with GIT

@akshaymathu 48

Saving Local Changes

• Stash– Saves all local changes of sandbox– Sandbox goes clean for coding and pushing a

hotfix– Multiple stash can be created

git stash

Page 49: Working with GIT

@akshaymathu 49

Applying Saved Changes

• Stash apply– Applies stashed changes back to the sandbox– You can choose from multiple stashes

git stash apply

Page 50: Working with GIT

Sharing the Work

Page 51: Working with GIT

@akshaymathu 51

Distributed VCS Operations

Repository

Remote Local

Sandbox

Pull

Push

Sandbox

Repository

Commit Checkout

Clone

Page 52: Working with GIT

@akshaymathu 52

Getting Repository

• Clone– Clones an existing remote repository, on local

machine, into a newly created directory• Creates remote-tracking branches for each branch in

the cloned repository• Checks out an initial branch into sandbox• Default name of remote repository is ‘origin’

git clone <path_to_repository>

Page 53: Working with GIT

@akshaymathu 53

Getting Changes

• Pull– Downloads objects and refs from remote

repository– Merges with local repository– Commits automatically

git pullgit pull origin master

Page 54: Working with GIT

@akshaymathu 54

Sending Changes

• Push– Update remote repository with changes in local

repository• Use --tags option for pushing tags• Use –u option to push a new branch

– Uncommitted changes are not updated

git push origin master

Page 55: Working with GIT

55@akshaymathu

Page 56: Working with GIT

Diverging Streams

Page 57: Working with GIT

@akshaymathu 57

Why Diverging

• For parallel development– A feature requiring long development time

• For experimenting new stuff– Something we are not sure of

• Custom development– For multiple clients from same codebase

Page 58: Working with GIT

@akshaymathu 58

Creating a Branch

• Branch– Creates a new code stream reference in local

repository from specified point• Checkout– Checkout with –b option also creates a new

branch• Latest checkpoint is used for branching by

default• Any older commit ID can be specified

Page 59: Working with GIT

@akshaymathu 59

Branching

git branch <branch-name>git checkout –b <branch_name>

git branch <branch-name> <start_point>

Page 60: Working with GIT

@akshaymathu 60

Branch Facts

• A branch named ‘master’ is always present in the repository

• Name of a branch refers to a moving checkpoint that is the latest commit on the branch– Branch name can be used in place of commit ID in

any command

Page 61: Working with GIT

@akshaymathu 61

Switching Branches

• Checkout– Gets file(s) from repository to sandbox• Branch name needs to be provided

– Command fails if local changes are there in the file that are going to be overwritten• Otherwise changed files remain there as is in sandbox

git checkout <branch_name>

Page 62: Working with GIT

@akshaymathu 62

Folding Back

• Merge– Merges a branch into current branch– Automatically commits, if no conflict– Runs in local repository• Requires push to update changes to remote

git merge <other_branch_name>

Page 63: Working with GIT

@akshaymathu 63

Merging

git merge <other_branch_name>

Page 64: Working with GIT

@akshaymathu 64

Conflicts

• If, at two places, changes are made in same portion of a file– GIT is not able to understand what to keep– Puts both changes in the file• Boundaries are marked in the file• File is marked as conflicted

– Authors need to collaborate and fix• Remember to commit after done

Page 65: Working with GIT

@akshaymathu 65

Moving Branch Point

• Rebase– Forward-port local commits to the updated

upstream head• All local commits in the current branch are saved to a

temporary area• The current branch is reset to upstream• The saved commits are then re-applied to the current

branch, one by one, in order• Rebase stops in the middle in case of conflicts• Rebase can be continued after resolving conflicts

Page 66: Working with GIT

@akshaymathu 66

Rebase

git rebase <upstream_branch_name>

Page 67: Working with GIT

@akshaymathu 67

Page 68: Working with GIT

@akshaymathu 68

Best Practices

• Communicate with peers• Write good commit log• Never directly modify files repository directory• Never share files using external means e.g.

email, ftp, ssh etc.– Get the difference as needed

• Do not commit generated files

Page 69: Working with GIT

@akshaymathu 69

Branching Best Practices

• Merge a branch only into its upstream• Keep ‘master’ untouched for hot fixes• Have a branch (say ‘develop’) for out of

‘master’ for system and regression testing• Create feature branches out of ‘develop’

Page 70: Working with GIT

@akshaymathu 70

Working with GUI

• Gui– Launches a Graphical User Interface (GUI) for GIT– Common operations can be performed

Page 71: Working with GIT

@akshaymathu 71

Thanks

@akshaymathu