35
GIT BASICS How to quickly use Git for day to day developments ... AND STASH Leverage Git on the Enterprise Created by / Original presentation on François D'Agostini @DagoFrancesco GitHub

Git basics a starter on git and its ecosystem

Embed Size (px)

Citation preview

Page 1: Git basics  a starter on git and its ecosystem

GIT BASICSHow to quickly use Git for day to day developments

...

AND STASHLeverage Git on the EnterpriseCreated by /

Original presentation on

François D'Agostini @DagoFrancesco

GitHub

Page 2: Git basics  a starter on git and its ecosystem

WHAT IS GIT ?DISTRIBUTED VERSION CONTROL

SpeedSimpleNon Linear DevelopmentFully DistributedLarge Projects

Page 3: Git basics  a starter on git and its ecosystem

SNAPSHOT BASED, NOT DELTASDIFFERENT FROM CVS, SVN...

Page 4: Git basics  a starter on git and its ecosystem

GIT CONFIGURATION

--system: /etc/gitconfig--global: ~/.gitconfigdefault: repo/.git/config

COLORS IN LINUX:

git config --global user.name "François D'Agostini" git config --global user.email "[email protected]"

git config --global color.ui always

Page 5: Git basics  a starter on git and its ecosystem

CREATION OF A REPONo need for networkCan do a lot locally on PCBest for testing

Git init: repo + working directorygit init --bare: Repo only git clone:From an existing repo

Page 6: Git basics  a starter on git and its ecosystem

EXAMPLE: FIRST COMMITmkdir git-testsgit init john.gitgit config user.name "john Doe"git config user.email "[email protected]"echo "first line from John" > file1git add file1git statusgit commit -m "initial commit from John"

Page 7: Git basics  a starter on git and its ecosystem

ANOTHER COMMITcat >> file1Second Commit from Johngit commit -a -m "another commit "

Page 8: Git basics  a starter on git and its ecosystem

WORKING DIRECTORY, INDEXES ANDCOMMITS

Page 9: Git basics  a starter on git and its ecosystem

VIEWING COMMITS HISTORY

...lots of other options !!git blame: one files only gitk: for graphic visualizationsame options as git log

git log --decorate --graph --stat --oneline -n

Page 10: Git basics  a starter on git and its ecosystem

COMMITS HISTORY (2)git show: details about a commit by default, shows the detailof the most recent commit git show-branch: show details about the current branch

HANDY ALIASgit config --global alias.graph "log --decorate --graph --oneline"

Page 11: Git basics  a starter on git and its ecosystem

VIEWING COMMIT DIFFERENCESgit diff: differences between the working directory and theindex--cached: between the index and the most recent commit HEAD: between the working directory and the most recentcommit

Page 12: Git basics  a starter on git and its ecosystem

CANCELLATION, RESTORATION, CLEANINGgit reset: cancels changes about to be commited in theindex

--hard: changes also the working directorygit checkout: cancels changes made to working directorycompared to indexgit clean: deletes files not followed by Git.

-f option to really delete them-n to simulate

.gitignore file: to avoid Git to track useless files

Page 13: Git basics  a starter on git and its ecosystem

BRANCH MANAGEMENTA Branch is just a pointer to a commitnothing fancyvery lightweightvery similar to "tags"stored in .git/refs/[heads|remotes|tags]Default branch name: master

Page 14: Git basics  a starter on git and its ecosystem

BRANCH MANAGEMENT (2)git branch: list branches

-r: remote branches as well-a all branches

git branch dev: create branch "dev"Does not change the current branch

git checkout dev: move the current branch to the "dev"branch

-b: create the branch while switching to it

Page 15: Git basics  a starter on git and its ecosystem

MERGINGgit merge dev:merges "dev" branch into current branch

does not destroy "dev"git branch -d dev:deletes the "dev" branch

only deletes the pointercan be deleted only if the branch is accessible from thecurrent branchThis is usually what is needed after merging. The old"dev" pointer is no longer usefulin case of future branching, a new branch can becreated

Page 16: Git basics  a starter on git and its ecosystem

CONFLICT RESOLUTIONgit merge can lead to conflicts if the same file has beenmodified in two branchesmanual conflict resolution is needed on this filesconflict markers are added by git and need to be removedby the developpergit add on the conflict filesgit commit to end the mergeand of course, delete the merged branchif conflict resolution is too complex:

git merge --abort: restores the working directory to thestate it was prior to merge

Page 17: Git basics  a starter on git and its ecosystem

REMOTE REPOSITORIESgit remote: lists all remote repos linked to the currentlocal repogit remote add name url: adds the specified url as aremote repo to the local repoNo need in case repo has been created with git clonegit push repo: push local commits on the current branch tothe same branch on the remote repo

warning: the remote repo must already have the samebranch, else use git push repo branch

Page 18: Git basics  a starter on git and its ecosystem

REMOTES: FETCHING AND PULLINGThere is a separate copy of the remote commits separatedfrom the local branch

Can be seen using git branch -athis means that the remote and the local copy can easilybe comparedgit fetch repo: updates the local copy of the remote repogit pull: like git fetch, but also merges the remote commitinto this repo's branch

this one can lead to conflicts

Page 19: Git basics  a starter on git and its ecosystem

GIT REBASEWhen fetching commits from remote repos, there are twoways to merge them:

regular merge: it will create a new commit every timerebase: it will not create a new commit

allows to change the commits that were not published tohave new parentsvery handy when you need to integrate other peoplechanges but continu your workuse: git rebase branchgit pull --rebase: will use the reboase algorithm in case ofconflicts instead of merge

Page 20: Git basics  a starter on git and its ecosystem

STASH: SAVE YOUR WORK WHILE SWITCHINGCONTEXT

Allows to save your current context and switch workThen, you can restore the exact state backgit stash save messages: stores the working director andthe index into a stackgit stash list: lists all the saved stacksgit stash show: shows the details of a stack itemgit stash pop: pops a state and applies it on the currentworking directorygit stash drop: removes an item on the stack

Page 21: Git basics  a starter on git and its ecosystem

STASH (2)warning, stashing does not store the branch stateThis means that you can recover your state on any branchpopping a state can create conflicts that needs to bemergedif a pop failed because of a conflict, it will need to beremoved manually

Page 22: Git basics  a starter on git and its ecosystem

GIT HOSTING IN THECOMPANY

Different from git stash seen previously !!

STASH

Page 23: Git basics  a starter on git and its ecosystem

WHY NEED A GIT HOSTING TOOL?Git alone is not sufficientTo improve knowledge sharingto improve code visibilityto get the code out of the darkness !!

Page 24: Git basics  a starter on git and its ecosystem

STASH CARACTERISTICSStructuring projects, repos and rolesBrowse files, branches, commits and tagsCode Reviews concepts: Pull requestsRepos forkingprivate reposJira linking

Page 25: Git basics  a starter on git and its ecosystem

PROJECT STRUCTURESStash organized by projects that handles multiple repospermissions are based on projects and repos. Allows fordecentralized adminusers have roles: project creators, admins, system admins,writerSoon, anonymous mode

Page 26: Git basics  a starter on git and its ecosystem

FILE BROWSINGCan browser any file and see source codeAllows to change branches and tagsCan have details of each commitbrowse list of commits and logmarkdown support to explain source code organization

Page 27: Git basics  a starter on git and its ecosystem

PULL REQUESTSImplements best practices with respect to code reviewAllows anyone to create a branch, make some code andask for its mergeBranch permissions allows to control commitsPull requests allow to review, make comments, ammendnew commits, see diffs...Anyone can watch a pull request and be notifiedPull requests can be declined or accepted

Page 28: Git basics  a starter on git and its ecosystem

FORKING

fork a repo into another repo, but keep historyallows for later merges

Page 29: Git basics  a starter on git and its ecosystem

PRIVATE REPOS

Develop your own projectscan show or hide repos to others

Page 30: Git basics  a starter on git and its ecosystem

JIRA LINK

Allows to link Git commit to Jira issuesfrom a Jira issue, see all related commitswith Git hooks, you can force it

Page 31: Git basics  a starter on git and its ecosystem

BRANCHING STRATEGIES WITH GITGit is small tool that does not come with a wholeenvironment and rules (think Clearcase...)Git is versatile and can be used in a dozen of differentwaysGit itself does not enforce how to use branches and howto work as a team. It keeps this openBut how a serious company is supposed to use git for all itsprojects without going messy ?

WE NEED BEST PRACTICES !!

Page 32: Git basics  a starter on git and its ecosystem

GIT FLOW

Standard usages of branches when working with GitUsed in Big projects, with many developers working on it

Page 33: Git basics  a starter on git and its ecosystem

MAIN CARACTERISTICS2 long lived branches : Develop and master3 short lived branches: Feature, Release and HotFixThe master branch is restricted to commit by one or twopeopleThe Develop branch is restricted to senior developersNew code is added through Feature branches only

Page 34: Git basics  a starter on git and its ecosystem

STEPS TO FOLLOWCreate a Feature branch, name it to the story/feature youare working ononce you are happy with your features, you create a pullrequest to merge it

Page 35: Git basics  a starter on git and its ecosystem

STEPS TO FOLLOW (2)When a release must be done, use a temporary releasebranchThe Master branch is used only for "stable" versioncommits. Any commits on master should have a TagIf a bug is found on Master, a "HotFix branch is created tocorrect it and merge it back on master