Advanced GIT for Developers - Lorna Jane Mitchell - Laracon EU 2015

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and thanks for having me here at Lara con I am just going to confess right up front that I don't know very much about laravel I work with someone who is very keen so I suspect I'm about to know a lot more in the coming months at work but right now it don't have a lot of laravel context I do know a few things about git and you've shared so many ideas with me about the technology that you're excited well I've come to share some ideas with you I'm going to be talking about advanced git I have been assured that for the most part you are not beginners with git so I'm hoping to show you a series of things which will help you in your day-to-day work it is literally a series of things so if I lose you at any point relax and look out for me starting on a new topic they don't build on one another so if you're feeling lost the next thing may be more relevant to the work that you do before I dive in on the command line I want to say this about advanced get really advanced get users are advanced not because of the commands that they know but because of the way that they use the tool they are curating same commits putting together sensible units of work with a commit message that makes sense and will mean something to us when we need to go and dig into it in three months time there are people who branch and merge effectively in a way that reflects the bigger goals and the work that we're trying to do rather than the fact that you should commit your code before you leave the office so I wanted to start by just kind of show you around really we've get and some concepts that I think will help you to put these other pieces into a useful format the most observant amongst you will notice I'm now to command-line that means there are no slides so if you see anything you like you probably need to like write it down or something I could share my slides there are five of them I'm not sure how helpful that will be alright so let's just start by initializing a new git repository that's given me a git repo here in the my gate directory it's empty except it's got a dot git directory because that's how it works and inside this dot git directory we will see a bunch of things as branches be more stuff in there later config have a look at a config it's my config file it's human readable you can come here and edit your config file until the last few years you had to most of the stuff that people did with git they did by moving files and directories around in the docket directory if I start setting config variables I'm going to set this locally because I really frequently demo this and then completely ruin my settings for all of the other repos on my machine which is kind of annoying if I set some convict settings you start seeing them appearing in the config file it's a plain text file section dot setting just reflects in the git config like that everything in get is an object and the easiest way to show you this just going to split my screen for a second is to show you things as they appear in the objects directory so we have a look this is what's in the objects directory right now empty repo the info and pack files are always there if now use the other this is the other way for you this side and start doing a little bit of work with get will just get a file going here I'm pressing tab get doesn't know what word I'm thinking of come on get and as soon as I start to do ad things start appearing in the objects list and that's because we are staging changes get is starting to create some records that relate to this if I then commit then we see more objects appear the next time the watch commander refreshes and those objects if you're looking at the list you can see all of the things listed there in the right-hand pane are directories and that's because everything and get is an object they all have those long-winded sha-1 names that you're familiar with from commit references and they are sort of sharded in storage so there's a directory for each first two characters and then objects inside that because of the words on our large repo you would run out of eye notes if you look at my commit 75f you can see oh we've got a 7d directory the object literally exists on disk in the docket directory and that's really all this going on there let's just jump our t-works because otherwise I'm going to end up wedged in one window let's think also about branches if I create a branch let's actually switch to it Oh naming things is hard then we start to see things appearing in inside that git git directory so in refs heads you'll find all of your local branches and all these files are are a pointer to a commit there's master I just branch from the same place rights that branch one points to the same commit reference so think about this a branch is purely a text file that lives in your local docket directory and can pains in plain text the name of the commit it points to a branch is not a sequence of commits a branch points to a commit every commit has a parent parent parent and we sort of assemble branches by computing this you'll see this branch point to move around later I think it's a really important concept to understand okay some things I want to share with you that might happen in your day to day work what's really closely the script name might give you a clue so these are scripts which are aimed to be easier to watch than me typing but I just kind of turned on the output so not sure they're much easier to watch initializing an empty repository called branched and then copying a script in and committing it that we're going to need later for reference weird things happen if you're in a git repository which doesn't have a commit yet so you'll notice that every example starts with let's just quickly commit a thing and this is to stop me making us full of myself onstage I mean I might but that's one less thing that I'm likely to do wrong so we've got that first commit and it's a script that we might need later and then I'm bringing in a poem I don't know if you've know the poem the house that Jack built it's one of my favorites because it reminds me of open source why is this like this well we used to have a problem with the data format and so when we moved tried to fix the time zones then we added this intermediate thing which did right so the poem is about the cat that chased the rat that ate the malt that lay in the house that Jack built so it's like this story just gets longer and longer just like the history of open source projects have all of these stories so I am committing this poem creating a branch not checking out the branch and making some local modifications to my local version of the poem so if we look at house that's my poem if we run git diff this will show me the changes I have local on my current working directory note that if I check if I switch branches my working directory comes with me so you can develop on the wrong branch as much as you like as long as you're on the right branch when you commit so git diff shows me this is what's here and gets data shows me I have house txt modified someone commented when I was teaching it recently about how often I run get status when I'm teaching and I had to say no that's an average day in the office git status what's happening git status what's happening do some more git status what's happening it's a nervous tic but one that helps me to remember where I am and what's happening here's my favorite git add feature so if you do get add with a file name well that's fair enough it's it's a normal way to work if you do get out with dots I'm going to controversially say that this is for losers and you can do enormous ly better try this get out with - P this allows you to review each of the changes that you made and you can review each one in turn and you can choose to add or not add each one I find it also means that I make eye contact with the VAR dumped statement which is lurking somewhere in a file so I find that incredibly helpful that the var dump statement kind of gives me a wave on the way past well I'm doing it ads with dots so I will say there's a bunch of things you can do here right yes no and quit - the first three they're basically the ones you want press question mark for all other options I will stage this change and let's not stage that change have a look at what good status shows us that file shows as both modified as are sorry this file shows us both staged and unstaged because part of it staged and part of is on stage you'll see this also if you add a file and then edit it some more so the staging area allows you to pick just the changes so you can create really good commits without having to like do your development thinking that way you're the craftspeople you make it make it awesome and then get will allow you to communicate that any way that you want if you stay something you don't want to get reset we'll unstaged your changes but doesn't lose them so git diff is still there we can add with a file name we can also get reset with Dash P so it'll allow me to unstaged individual changes so if you kind of want everything except not this bug fix that you did in a long file you can just add the whole thing and then reset with dash p you can check out with dash p as well if that's helpful I am staged awesome let's stop fiddling about with that okay so what I was going to show you was a merge conflict so to do that I am going to switch over to that branch that I made earlier so we did a commit then I created a branch you've just seen me make changes on the master branch now we're going to make changes on the feature branch but I've called awkward to give me a clue that something might happen I have brought in some changes and I have committed those changes so git status just shows me everything's cool now if we switch over to the master branch come on then let me show you my favorite favorite git command like it's it's a tough call this is my favorite get command with all of the switches and everything this is harder than you would think decorate all graph those four switches git log one-line decorate all graph I'm typing at once you'll see me pulling it out of my history with command R for the rest of the session and what this does the one line shows you the compressed view so you just get the little bit of commit reference and the first line of the commit message or the first piece of the commit message often we don't care about dates or authors or whatever it allows you to see more than four things rather than try look through a letterbox you can see a long history the decorate is what produces the branch and head and tag labels so you get those green red home got any remotes red and the head label - - all means show me things that are in my repository but which are not necessarily ancestors of my current commit so usually get log will just walk back up the tree for you whereas here if you've branched it will show you all of the branches with - - all and - - graft as the ASCII art you can get GUI representations of all of this I am a command-line person and the command line will work on all of your systems so I quite like to use it as an example so we made this awkward branch it's a feature it's fabulous so we're going to merge it now git merge awkward uh-oh there's a conflict surprise you have a couple of options when you see this or three actually but the first one is panic and the second one I mean you can see here that my prompt says I am merging so I have that plugged into my prompt git status will tell you you have unmerged paths so that tells you your repository is halfway through emerge you can't do anything else until you get out of this situation so one of your options is get merge - - abort pretend never happened what this means is that now you can go ahead and try and merge any branch in the world ever and if something goes wrong you can just walk away repo inconsistent state pretend that never happened it's also useful for situations like the colleague whose branch this is is in a meeting or in a different time zone or currently at a conference so we can't ask her what we should do there's all sorts of reasons you might want to abort you might want to know how to actually resolve it as well so let's go for that git merge awkward oh there's a conflict okay git status shows me which files conflicted obviously this is a trivial example so we have a whole one file and it's conflicted but if you have files which are okay they will show is stay aged for commit and only the file or files which of course the conflicts will be listed here under unmerged paths and all you need to do is edit the file now I always do this the same way and I'm going to share my process with you I search for four equals signs if I'm working with code doesn't always work so well on various like rst uses it as a title thing but uncoded I use four equal signs and I search for that okay in this case the conflict is on line two so we could probably find it without the use of search but bear with me so we fix this by doing something that makes the code read correctly and we're done right resolve the conflict save the file ready to move on before you leave this file press find again cuz look there's another conflict in this file and it was below the fold and git will let you add and commit those conflict markers it'll be just like uh-huh git is not on your side always so yeah there's another conflict to this file and this can happen if you have long utility files or you know there's just a couple of a bit of separation between two things that got changed it's yeah I'm trying to do something specific with this merge there we go search again can't find anything so my file is fixed now git status is totally your friend conflicts can be like old as a conflict that ought to do git status tells you what to do you see the conflict notification you run git status you understand what has conflicted you mend the conflicts you save the file you run git status it says you should use git add Oh get add then you run git status and get status says you should use git commit okay good commit and there we go we've resolved the conflict this is the commit message that you would have got if there had been no conflicts and it's also got information about which files were conflicted you can edit this as in accordance with whatever your branching strategy is if you do fixes hash 42 then vine do whatever you would normally do here it's you know how to do a commit so there we go so that's a merge conflict I have a very clear process look at what come if in doubt back it out that's connect waited a billion times isn't it oh well and if I always get Oh H done stayed whatever if there is a conflict you can resolve it the conflict is just get saying excuse me I need some help from a grown-up right get come write code that's your job so you need to just pop in and help it resolve where where there's been two changes that overlap add the files commit the files all good and I think this stuff comes up all the time I wouldn't be particularly concerned that you're seeing conflicts unless you're seeing a lot of conflicts all the time in which case you need to talk more to the people that you're working with or in my case you need to keep up better with your pull request queue because once you've got enough open pull requests they are going to conflict at some point because they've diverged a long way one neat trick if you are seeing the same conflicts coming past again and again is to do what's called reuse recorded resolutions this tends to happen where you are building a staging branch or some kind of integration branch bunch of features available you put four of them on the server client once three of them you reset the branch you put three of them on the server two of these branches conflict and you're resolving that same conflict over and over it's maddening there's a feature for that and get so to do that let me just show you my graph so I have commits on each branch then emerge commit I'm just going to undo this so that I can reuse that conflict again to redo the commit so to undo a merge I'm going to do git reset with - - hard and my top advice get reset - - heart is really really useful for losing all of the uncommitted work that is currently in your working directory it's really hard to lose work and get but if that's your aim get reset - - hard we'll do it every time if you've committed we can retrieve it if you haven't you just blew it away so before you do that get status nothing to commit working directory clean okay now run git reset - - hard git reset - - hard and I'm just going to reset the master branch back to where it was before I did the merge which is that - 8 - 3 yeah whatever awesome so now my graph looks like it did as if I had never done the merge remember that the branch is just a pointer to a commit so when it seems like a branch is a long thing and we might be tangling it by going back on itself we're not we just move the pointer that's fine right I need to strategically create another branch at this point I'm going to call it another ok so the re re re record reuse recorded resolution it's a conflict setting so we're going to turn it on re re re dot and enabled true got it then we do the merge git merge awkward you know what happens there's one more line in this output and it's the third one down it says recorded pre image so git is understanding the shape of the conflict and kind of storing it for later use or reuse as the name suggests okay so we are now resolving a git conflict you just watch me do this we run git status we say oh look it's house txt we edit house txt we find the equal sign yeah whatever good and we say that's perfect and then we commit it and that's brilliant Oh lovely so we've done that and now I'm going to show you what happens when we do the same merge again so I created the another branch and just show you that web command created the another branch which is pointing to the commit that the master branch was pointing to so when I merge awkward in we're going to see exactly the same conflicts exactly as if we were just merging it again git check out another git merge awkward guess what there's a conflict but instead of saying recorded resolution it says resolved house txt using previous resolution so it's remembered what I did last time I saw this conflict and it's just put it into place get status shows me it's there but it's not staged so I still need to inspect this I can edit it it looks like you'd expect it to look it used the changes that I made last time and all I need to do is add it as I would and then commit so that's that's using the riri resetting if you get it resolving something that you don't want you can always abort the merge and turn off the setting it's not on by default just because it's extra overhead and extra storage and it's only if you have that workflow where you're integrating the same branches repeatedly usually to build a selection and test it build another selection so it's putting together a release branch with a bunch of features in it something like that I see this all the time it is maddening when you have to keep doing the same thing over and over you sort of think the computer should be able to help it can ok I'm going to talk about rebasing and I'm going to talk about it by first of all showing you a picture this is why I have slides because otherwise I try and do this with the art of mine it doesn't work as well so imagine this situation we've got the master Brown develop or whatever your main line is and we're happily developing along and then somebody branches and does some work great sometimes we'll want to rebase we might rebase because there's a conflict we might rebase because we like to rewrite history and make our history look like a happy story where nothing ever goes wrong either way this is what happens let me do that again if I go back one your brand you branched off from here and you did some work and rebase makes it look as if you branched from there we just roll up your branch run to the tip of another branch and then unroll it one commit at a time if your rebasing because you're you have a branch that won't cleanly merge ie there's a conflict you're going to see a conflict when you do the rebase the good news you know how to resolve conflicts a rebase conflict sounds pretty horrifying right but somebody's nodding violently at me from the audience a rebase conflict sounds horrifying you know how to resolve conflicts get status will show you what conflicted you will inspect it you will fix it you will add it the only difference insert is that instead of running it commit at the end you run git rebase - - continue and get status tells you that's what you should run next so don't be freaked out with that because it's super easy ok so git rebase here's a script I made earlier initializing an empty repository here I'm creating a directory changing into it and doing a bear initialization you'll sometimes see this as a bear clone or a bear initialization like this one and all that means and git is that it's a repository with no working directory so typically these are server repositories they usually have this dot git thing in their name you've seen this on github and so on where your URLs have dot get on the end and it's a server repo so you can push to it you can pull from it but if you go and try and like look at it on disk it only has the docket directory there isn't anywhere for you to check out a branch and work on it so that's really useful for hooks and things if you want to push to a repository which then has hooks you can't push to a branch that's currently checked out so you want a you want a bear repo for that so all I'm doing is initializing a bear repo cloning the bear repo get very intelligently points out it's empty just get we do our first commit so that I don't get too tangled and I've pushed it so we've pushed that commit back up to the remote notice that my remotes here are just local on the file system it doesn't actually care whether it's a file or a URL or whatever that's not it's not that unusual for me to clone like to initialize a git repo and then clone it and work on it on my own machine I do it for these talks so I have like a set of scripts and I'll clone it and then if I've made any changes to the scripts I'll add it as a remote and pull them back doesn't matter that they're on disk also me it doesn't matter that I've turned off my network notifications so you can't you can tweet at me if you like but it's not going to come up on the big screen while I'm talking that was a massive tangent okay so I've committed and pushed excellent everybody is still with me I've created a new branch on my repo and I'm using a quick script here that you'll see a bunch of times called generate random changes which does create a new file puts random content in the file and commits it there's four of those and then created a branch so I've got where I origin/master so where I did the initial commit and pushed that's in sync with the origin I've got a branch with some commits and I've got the master branch with some more commits and what I want to do now is rebase the branch I was working on branch one because I'm also net awesome at naming things and rebase that onto the tip of master when you merge often you're merging feature branch into the master branch when you're doing that you check out master because that's the victim that you will be operating on when you rebase it's the branch that you're making a change to so you need to check out the branch it's the victim I should probably say target victim sounds terrible but I find it much easier to remember so I am going to rebase branch one on to master so I check out branch one I type git rebase master and this feedback message is quite a good reflection of what happens it rewinds the work and then replays each of your commits so this is where if there would be a conflict along the way you'll see that as it does each one in turn so it will conflict on one ask you to fix it and then the git rebase - - continue is to get it to pick up and carry on again let's grab that log command here it is so that looks quite different you can just see the tail end above where the branch went off and then there was master and now it looks really linear all the commits our master happen and then it looks like all of the commits on the branch happened so that's rebase it just lets you go from one thing to another did you just lose that I lost it there we go so what's the point of that well sometimes yes so that you resolve the conflict and then I can accept your pull request into my project sometimes it's done for clarity so it might be that you rebase so that when you merge you achieve a fast-forward merge we just talk about that for a minute if I was to ready to merge this branch in now ivory based it check out master git merge branch one I can't type anymore it's a long way to go you go it gives you this fast-forward merge and what that means is it literally fast forwarded the master branch pointer to the tip of branch one because it was obvious that it could you that it was linear in our log so all that's different with our log now is that all the pointers all the branch pointers are sitting at the top so that's a fast-forward merge sometimes the fast-forward merge isn't what you want and if that's the case then you can just do a git merge - - no - FF no fast-forward and that will always show you there was a branch and you get a merge commit on there pictures so master feature if it looks like this and there are no commits on master in the meantime either because you rebased or because you're doing what I call a hotfix so you've branched to make a change and merged it back in but it was really small change nothing happened on master in the meantime then all that happens when you do the merge is git will try to fast-forward the merge and it will just move the pointer along that are if you do the git merge command with the dash - no - FF and you get the merge commit and so your tree will look like this instead and that's all that is is that we get an extra commit and it's a merge commit with two parents as you would normally expect so you see the fast-forward and you didn't want it then you can just do the git reset - - hard as I showed you earlier and then redo it I do this all the time this is my number one reason for git reset - - hard is I merged somebodies branch and it fast forward merges and the branching strategies that I use I don't like that I prefer to see the history of that we did branch and it did happen one of the clients that I'm consulting for at the moment said to me we've got a problem we've got a contractor who is committing directly onto our staging branch only we sometimes throw the branch away and it turned out that she wasn't but she was getting these fast forward merges and they're branching strategy didn't particularly say to not do that she was committing on a branch it just looked like a direct commit because it was doing the fast-forward so have you read the git log it looked like direct commits on that staging brush so you do see that once in a while let's push this there's another kind of rebase that I wish had a different name as it's a radically different thing git rebase interactive allows you to rewrite history I'm not sure if I should be encouraging to do this but it's quite fun so I'm going to show you anyway git rebase - I for interactive this is going to work on the branch that I'm on so I'm just going to rebase the last few commits like eight something like that so what you get is this text file and this is your scratch pad you can move things around as you like here if you just save and quit this file then it will replay all of those commits with a current timestamp so all the commit identifiers will change but nothing about the actual commits will change number one thing to mention about this because it confuses me every time you run git log and it shows you the newest commits first and the one before that one before that that's the time goes this way this is the other way around this is telling you a story from the beginning to the end so you do get log you decide what you want to rebase interactively and then it flips it upside down your brain goes wait what so look out for that the instructions for this are all in the comments so your choices are pick which means just keep it you can reorder them though so you can pick move the lines around reword for I should have sorted that is I missed the ticket number out of the commit message or something else equally crucial you can edit I wouldn't recommend that one you can and squash and fix up do the same thing so I use fix up usually and I use it mostly in the following scenario in my commit history you'll see a commit that's sad Lorna fix the bug and then the nimac next commit says Lorna really fixed it this time so if you haven't pushed yet you can do git rebase interactive for London like the last two commits and do fix up which will just squash that second commit into the one it should have been in in the first place also useful for Laura made an awesome feature next commit Laura now has remembered to include the new template files this happens to me all the time kind of depends like at work I wouldn't worry about it too much if I'm doing really well-paid consultancy probably gonna fix my commit history it just it depends so we can fix some things up we can remove things we can move things around so let's do some work on this feature one is awesome but it should have gone in much earlier so I'm going to make that happen first in history by just putting the line at the top of the file that said feature three really wasn't a good idea if I remove that line that commit never happened in the new version of my branch these app changes were cute but it's coming in four commits and it just doesn't need to at all so I'm going to squash that down at three is okay but I'm going to combine at four at five and Apps six by using the let's use squash you can also abbreviate just with a single character which is what that's showing you there so the instructions tell you which prefixes you can change and you should know also that you can reorder these lines and remove them so now I'm going to have feature one happening first app three a combination of other app commits and then feature to go feature three and feature for all I have to do is save and close this file and get will then wash off and do all this for me because I chose to squash I have the opportunity to rewrite the commit messages so it's prompting me now and showing me what the previous commit messages were so we'll just put something really meaningful in here because I am fabulous at this and there we go so look at my log now let's just steal that come on there we go so now you can see that I've got Oh a couple of interesting things going on let's just get log on one line for a moment how confused of you there we go so this is what I've just done look at where I rebased feature one ended up much earlier in the history app one is still the app 3 sorry I can't read this from here at free is still there but 4 5 & 6 are now in a single commit which is really useful for when it just takes you a few commits to really nail something and then I kept feature 2 and feature 4 but not feature 3 so that's git rebase interactive I have rewritten history the reason that my combined log looks very interesting is because I had already pushed my branch to master and by rewriting history I have diverged my master and origin master so if I want to push this now I can't alright lately you can push with dash F and you're all getting about C off in say Laura Jane said we could push with dash F um do you know what you can push with dash F if you understand what's going on if you already pushed that doesn't matter if you already pushed and somebody else pulled it that probably does matter um in the situation where you are the victim if you like of push dash F so you were perfectly following the rules and someone else has force crushed to a branch that you were using well you can either counter push dot F dash F but we probably you should like sit down over a cup of tea or something try and sort that one out um I believe I'm giving you this information all right next but what you actually probably need to do if somebody has pushed with force and it makes your branch look like this it's diverged you do another it's a reset heart again you need to get fetched to find out what the remote branch is up to and then do get reset - - hard origin/master or whatever the remote and branch name is to bring yours into sync with theirs so if there's a reason on your project why you need to push with force on to say the master branch then everybody else can just get their master branches back in sync by doing get reset - - hard origin master brings everybody back to where they need to be okay how we're doing the time horribly actually oh well let me pick right I'll talk about git diff this is interesting because you probably think you know how to diff if a couple of things I do use that I want to share with you so this is an empty repository called diff a couple of commits and then editing both those files so I have committed files with local changes gets status one of them is staged one of them is not get diff let's just try and get this so you can all see git diff by itself only shows me the unstaged files so git diff on its own gives me things that you haven't remembered to include in your next commit also remember that git diff won't show you new files so you need to spot those in git status if you want to see what is in your next commit which I find immensely more useful then you need to do get diff - - staged and this will show you the things that are going to be in your next commit so if you've added a few things maybe you got interrupted then you can do get diff - - staged what am I about to inflict on the world and remember I already said you can do the get reset with - P so you need to just pull a few things out and add something you can very much shuffle those things around if you want to see everything that's happened since the last commit then you can get diff against head head is a revision number which always means the thing I last committed and therefore the thing that will become the parent of my incoming commit my next commit so that shows me both changes get diff with head another way of looking at diff other than this kind of inline ASCII kind of view most of you probably have some kind of GUI tools my hair source tree is very good I use meld you're about to see it there are other ways that we can look at this everywhere that you can do diff you can do diff tool so get diff all this is a bit hard to read get diff tool and it will pick a different thing which you can configure mine's currently configured to VIN diff so it shows me the two files side-by-side can be super helpful you can also configure this so another option for me and something that I use a lot of I'm dealing with really complicated changes is gits setting diff tool defines what diff tool runs and setting that to meld so if I now do that git diff tool head again this time it opens it in a bit more of a GUI editor and again same as vim do if you can move changes from one side to the other and inspect and compare then meld also has a mode where you can get it to show you the whole diff and it opens like a tree with just the changed files so you can see there's a database patch there's a controller change and there's also two template changes and it shows you just those files and lets you open them in side-by-side view as well all of you should have access to tools that do this your IDE s will do this source tree will do this meld is cross-platform be really certain because especially when you get beyond doing a bit or working on your own and you start looking at reviewing big incoming branches other people's work really collaborating those diff tools will make all of the difference in your confidence also in how quickly you can get things done which is nice and important right one more different reek and it's I want to really show you what get can do with file types and death it's called diff tool this does a bunch of things so first of all it's moving the agate attributes file over into my dot git attributes file dot git attributes is a bit of a weird one it just affects your local repository so it's not included and committed when you share and it can dictate how all kinds of things work in this case I'm saying I'm outputting the file they're all star dot ODT files that's open document text right emilynics user its libre office all ODT file should be dipped as in the ODT category then I put an extra entry into my git config saying ODT files are binary if you have a file type that is like the minified CSS stuff tends to do this that kind of looks like text except makes no sense when difft you can just categorize it as binary and git will just tell you these files have changed and stop trying to show you the whole diff and making it impossible to read so that's really useful I've also used this text conf setting which is saying here is a program that knows how to render files in this category as text so it's going to allow me to diff by converting them to a textual representation and here is a document that I made earlier so I've got the document and I've committed it has to be committed for me to diff it right so it's in here it is so we'll edit this document it is a document it says this is a document it has some content in the document and I will now edit the document this is so exciting I probably then give you a get a vim command to close it because I find this incredibly complicated there we go git status shows us the file has changed git diff gives me the text rendering including the diff of the changes made to that document this is sometimes helpful if you do have like a PDF handbook or some kind of rich text thing it doesn't come up loads more often you're likely to need the binary setting because although git has extensive knowledge of a load of file formats specialized projects are very likely to have some custom ones so you can mark it as binary just to stop it showing up in your diff it's irritating and if you want to if there's a way if it's text ish find a text conversion Handler and set it up like this super helpful and I do have some repose which are sort of mixed so I find that very good some of you are still awake I'm determined not to run long because I'm between you and your lunch so I want to tell you all of the things I don't want to make you miss out on your food so just trying to balance that out over here let me show you a trick this trick is called git bisect and it's almost an implicit implementation of a guess than number gained right see I guess the number between one and a hundred and I'll tell you higher or lower so you're all immediately going to start 50 in an effort to reduce the search space that's git bisect you're going to use in a situation where you know something's broken you know what is broken we have no idea when it broke you need to kind of find that route commit and fix it so here is a hypothetical situation we've got a bunch of commits and at some point in history I have no idea when it's a massive history we have accidentally deleted a file or we've only just noticed that's a problem and I can't possibly guess from this history of commits when that would would ever have happened so I'm going to use git bisect to help me to find it the way this works is we start by saying git bisect start so we kind of get into bisecting mode and my prompt reflects that I'm going to say I need I need that log command again no I want all of it right git bisect it so it was fine somewhere like really early git bisect good let's just go over the second commit there to be 4d yeah whatever and it's currently broken so I'm going to say git bisect bad if I omit the commit reference it seems that I mean head so it's currently broken off it goes so it tells you how many revisions are left to test how many steps it thinks it's needs to narrow that down and which commits currently on so this is basically doing what you would do which would be like okay well let's try three weeks ago and you sort of check out this revision and maybe poke at it refresh the page whatever it is you need to do to verify if that bug exists in my case I'm looking for does the file stuff one exists okay the bug is we deleted stuff one right so I can test for that oh let me show you this give you a sense of where you are but get bisect visualize shows you where we are so this that top pane shows you these are all the commits and you can see we're currently in the middle and then it will show you I've eliminated these one now I'm working on this section so you can run visualize at any point to kind of see how it's doing where it's up to I think this is yeah this is get K so you'll have this on most systems if you have something prettier and get knows about it it'll use that instead okay so I have I can just test for this right this is super straightforward and if you look closely there's this test SH file which is mysteriously appeared in my directory which just tests for the existence of tests of stuff one txt and it uses the exit status of the script to say this is good this is bad you could do this by wrapping a PHP unit and call because it's exit statuses will always be correct you could use any of your other testing tools again they'll return successful live it's cool and not if it's not so you just need a tap you need a script that says yes or no depending if the problem is there or not crucial thing to know this script should not be under source control because if you put it in the nearest commit right and then you check out some historical committed it's not there I can't run it so so if you need to like copy your test or something if you haven't had if you didn't have your test at the time that you broke this thing then either don't commit it or just yeah it can't be in source control because you lose it when you go back in history this is just hanging about in my working directory so I can use my script by doing git bisect run test SH and it goes through trying a bunch of different commits and if you were to look at these on my history they literally does okay it was working here here oh no I was broken yeah found it and it comes back and says oh it's the commits with the message deleted a file you would never have guessed right I just needed something really obvious so that I would know I'd shown you the right thing as well so that's good bisect I don't always use it with the script do a lot of web stuff so sometimes I'm just refreshing the user interface but it does the efficient which commit should I try next how to reduce the search space thing for me so that can be wouldn't say use it every day I do work on some quiet legacy code but it's not that bad but my current colleagues are fine it's just some of these projects have a difficult history but sometimes it's allowed us to very quickly pick up on what went wrong and fix it which is nice all right let's go with sub-modules oh let me just stop bisecting there we go awesome I want to talk about sub modules now sub modules don't have a good reputation I think it's because there are quite a few moving parts so especially if you work with people who are less experienced in git I find that I normally then need to write a cheat sheet on how to make sure that everything is pushed and committed that should be I love sub modules think they're a fabulous feature and this is coming from someone who was a subversion power user and liked externals and kind of hated some modules for a while I love them I love them I love them and there's a bunch of things you can do instead you guys are big into modern tools so you have Composer so there are lots of reasons why you can just like rely on your own Composer branch and that's also awesome and I do that as well but sometimes I just need to work on this code in the context of this project and make the code available elsewhere to be reused into other things but the hacking is happening here and in that case it's probably this project that's going to use the sub module and others may pull in the branch dependency with composer using this quite a bit at the moment with Hugh bot doing a bunch of Hugh bot things and I want those plugins that I write to be available to other people but they're kind of like the Hugh bot plugins are just not as stable and mature as you would think so I constantly have to hack them and I think I always want them available to other people and I use them in a bunch of different Hugh BOTS and so that's all done with sub modules so I think it's awesome and I want to show you how to do it I'm just going to cheat and set this up quickly so I'm initializing a a repository called sub mod making some changes in it and cloning into a bear repository so I've got a working repository in sub mod and then I've got main mod which is where my module lives so that's like the server thing we're going to pull from now I've got modules that's the name of my parentage echt I definitely feel like naming things is hard right now okay so that's the name of my parent project and I've got some commits in my parent project nothing else has happened yet at which point I add a sub-module get sub module commands are one of the few places where it matters where in the hierarchy you are like where in the directory structure you are so you need to run these from the top level and I am adding get sub module called sub mod on the repository and again it's a file repo slash main mod get that could be a github URL it could be pointing to your work get lab whatever wherever you keep your code in this case mines all local so that if I go places without working Wi-Fi it's not a problem and get status shows me first of all there's a new file called sub mod you know you would expect that to be a directory it is a directory it's just showing it you as a file and there's also a new entry and dot git modules and if you have a look in up come on get modules is that describes my sub module so it's called sub mod it's in the sub directory and it links to that URL or otherwise remote okay so we'll just add this that's stage so we'll commit it awesome okay so if we're actually in the sub module everything looks quite different here I've got some files I can edit a file it's status shows me the file is modified but if I go up into the main project so out of my sub module into them the module itself gets status shows me something's happened over there it's just like that thing did something happened and it keeps this really kind of isolated way of working that the parent project kind of knows that something's going on over there but it can't show you what's happening inside that directory it's very black box so I think works really well so we go back in here I've got my edited file I can add it yeah nice I'm so good at this right and we can push this up to our repository so we created a sub module all the repository we pulled it into a project we made changes inside that we pushed it back to its remote now in my parent project we're still kind of seeing something happened over there and all we need to do here is get sub module update and it pulls the newest because we're tracking the tip right so it pulls the newest and accepts those changes so I can just make a change in one project update that sub module fix something maybe fix a bug and then make that available to all the other projects that use that same sub module by just doing get sub module update so feel like there should be more to say about sub modules it should be big and complicated and hard and it isn't you need to pay attention and make sure you are pushing from your sub directory as well as any changes in the parent project and that is usually where things go wrong but otherwise when you clone get sub module in it and then all the time gets a module update and that will pretty much keep you in sync and working together okay well I am pretty much out of time they're looking at me okay so I am pretty much out of time but I am really happy kind of to chat with you through the day I am here all day I enjoy talking about this stuff are too much so please please like I don't know very many laravel people but I'm excited about it you're lovely so talk to me anytime I'd really appreciate your feedback on joined in regarding this talk except I'm the maintainer of that project so you can talk to me about joined in itself if you would like to I teach extensively on git so you can get my lean pub get workbook which is many small chapters with practical exercises that you can quickly learn a thing from and it's also a video course with O'Reilly and if you need any like more serious help than you should use gets SCM comm because it's the official reference and with that thanks very much enjoy your lunch you
Info
Channel: Laracon EU
Views: 108,478
Rating: 4.8738737 out of 5
Keywords: laracon, laravel, amsterdam, php
Id: duqBHik7nRo
Channel Id: undefined
Length: 60min 46sec (3646 seconds)
Published: Wed Sep 02 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.