Git basics a starter on git and its ecosystemPresentation Transcript
GIT BASICSHow to quickly use Git for day to day developments...AND STASHLeverage Git on the EnterpriseCreated by /Original presentation onFrançois DAgostini @DagoFrancescoGitHub
WHAT IS GIT ?DISTRIBUTED VERSION CONTROLSpeedSimpleNon Linear DevelopmentFully DistributedLarge Projects
SNAPSHOT BASED, NOT DELTASDIFFERENT FROMCVS,SVN...
GIT CONFIGURATION--system: /etc/gitconfig--global: ~/.gitconfigdefault: repo/.git/configCOLORS IN LINUX:gitconfig--globaluser.name"FrançoisDAgostini"gitconfig--globaluser.email"firstname.lastname@example.org"gitconfig--globalcolor.uialways
CREATION OF A REPONo need for networkCan do a lot locally on PCBest for testingGit init: repo + working directorygit init --bare: Repo onlygit clone:From an existing repo
EXAMPLE: FIRST COMMITmkdirgit-testsgitinitjohn.gitgitconfiguser.name"johnDoe"gitconfiguser.email"email@example.com"echo"firstlinefromJohn">file1gitaddfile1gitstatusgitcommit-m"initialcommitfromJohn"
WORKING DIRECTORY, INDEXES ANDCOMMITS
VIEWING COMMITS HISTORY...lots of other options !!git blame: one files onlygitk: for graphic visualizationsame options as git loggitlog--decorate--graph--stat--oneline-n
COMMITS HISTORY (2)git show: details about a commit by default, shows the detailof the most recent commitgit show-branch: show details about the current branchHANDY ALIASgitconfig--globalalias.graph"log--decorate--graph--oneline"
VIEWING COMMIT DIFFERENCESgit diff: differences between the working directory and theindex--cached: between the index and the most recent commitHEAD: between the working directory and the most recentcommit
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
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
BRANCH MANAGEMENT (2)git branch: list branches-r: remote branches as well-a all branchesgit branch dev: create branch "dev"Does not change the current branchgit checkout dev: move the current branch to the "dev"branch-b: create the branch while switching to it
MERGINGgit merge dev:merges "dev" branch into current branchdoes not destroy "dev"git branch -d dev:deletes the "dev" branchonly 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
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
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 repowarning: the remote repo must already have the samebranch, else use git push repo branch
REMOTES: FETCHING AND PULLINGThere is a separate copy of the remote commits separatedfrom the local branchCan 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 repos branchthis one can lead to conflicts
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 commitallows 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
STASH: SAVE YOUR WORK WHILE SWITCHINGCONTEXTAllows 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
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
GIT HOSTING IN THECOMPANYDifferent from git stash seen previously !!STASH
WHY NEED A GIT HOSTING TOOL?Git alone is not sufficientTo improve knowledge sharingto improve code visibilityto get the code out of the darkness !!
STASH CARACTERISTICSStructuring projects, repos and rolesBrowse files, branches, commits and tagsCode Reviews concepts: Pull requestsRepos forkingprivate reposJira linking
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
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
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
FORKINGfork a repo into another repo, but keep historyallows for later merges
PRIVATE REPOSDevelop your own projectscan show or hide repos to others
JIRA LINKAllows to link Git commit to Jira issuesfrom a Jira issue, see all related commitswith Git hooks, you can force it
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 ?WENEED BEST PRACTICES !!
GIT FLOWStandard usages of branches when working with GitUsed in Big projects, with many developers working on it
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
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
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