Git basics a starter on git and its ecosystem

Preview:

Citation preview

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

WHAT IS GIT ?DISTRIBUTED VERSION CONTROL

SpeedSimpleNon Linear DevelopmentFully DistributedLarge Projects

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

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 "fdagosti@nds.com"

git config --global color.ui always

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

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

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

WORKING DIRECTORY, INDEXES ANDCOMMITS

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

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"

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

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 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

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

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 repo

warning: 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 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

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

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

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 THECOMPANY

Different 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

FORKING

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

PRIVATE REPOS

Develop your own projectscan show or hide repos to others

JIRA LINK

Allows 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 ?

WE NEED BEST PRACTICES !!

GIT FLOW

Standard 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

Recommended