13 Advanced (but useful) Git Techniques and Shortcuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the best pieces of advice i ever got was get good at git few things are more scary for a software engineer than screwing something up with git after weeks of writing the perfect code you submit a pull request and end up with a bunch of embarrassing failed checks and merge conflicts next thing you know you're scrambling on stack overflow looking for a solution that will likely just add gasoline to the fire git was created by a guy much smarter than us he knew if he made it too easy it would make us weak instead he wanted to give us the glory of overcoming the challenge i'm pretty sure that's what he was thinking in today's video we'll look at a bunch of different tips and tricks to make you more productive with git but if you're new to git i just released a short course on fire fireship io that's designed as a modern learning path to get you started with git and github it contains a bunch of short videos along with exercises to transform you into a professional git user quickly this content is supported by viewers like you and i want to send a huge thank you to everybody out there who's already a pro member so to get started i'm assuming you know the basics of git like you know how to do a git ad followed by a git commit to save a snapshot of your code that's usually the first thing you learn in git but there's actually a better way to get the job done you can get rid of the git add and go straight to commit by using the am flag this will automatically add all the files in the current working directory that's a nice improvement but there's actually an even more concise way to get the job done your git config provides a way to create aliases which are commonly used to shorten existing commands or create your own new custom commands for example we might create an alias called ac that runs the add and commit command with just two letters that allows us to get things done faster but sometimes going fast leads to mistakes clearly mistakes were made what if you made a typo on your last commit message instead of resetting and creating a new commit the amend flag followed by a new message will simply update the latest commit or maybe you forgot to include or stage a couple files with your last commit you can also update the last commit with new files by using the amend flag and if you want to keep the same commit message add the no edit flag as well but keep in mind this only really works if you haven't already pushed your code to a remote repository unless you like to live dangerously in which case you can do a git push with the force flag this will overwrite the remote commit with the state of your local code however if there are commits on the remote branch that you don't have you will lose them forever and your co-workers might not be too happy about that don't make me swing on you bro but what happens if you push some code to a remote repository then realize it's complete garbage and never should have been there in the first place the git revert command allows you to take one commit and go back to the state that was there previously it's kind of like an undo but it doesn't remove the original commit from the history instead it just goes back to the original state and that's much easier than trying to put things back together like a trauma surgeon too much blood i can't see a thing in other cases you may need to work on a repo but not have access to your local machine if you're at your grandma's house without your laptop you can use any computer that has a web browser go to github and find the repo that you want to work on then hit the period key on your keyboard and like magic it pulls up a browser-based version of vs code where you can make edits submit pull requests and do almost anything else you could do locally well except for run the terminal if you do need to run terminal commands you can set up a github code space in the cloud which will give you the full power of vs code and is likely much faster than your grandma's computer but now let's switch gears to one of my favorite git commands stash have you ever spent time working on some changes that almost work but they can't really be committed yet because they break everything else or maybe it's just really sloppy and you don't want all your friends to see it yet git stash will remove the changes from your current working directory and save them for later use without committing them to the repo the simple way to use it is get stash and then get pop when you're ready to add those changes back into your code but if you use the command a lot you can use git stash save followed by a name to reference it later then when you're ready to work on it again use git stash list to find it then git stash apply with the corresponding index to use it now if you want to use a stash at grandma's house you're pretty much sol unless you use a github code space in which case your stashes would be saved in the cloud that's pretty cool but now i have a public service announcement for developers in the modern era historically the default branch in git is called the master branch but post 2020 this term is no longer the preferred nomenclature refer to it as main mega or mucho to stay out of trouble to change it use get branch followed by the m flag and rename it to main or maybe get creative and invent your own name now another command you're probably familiar with is git log to view a history of commits the problem with this command is that the output is harder and harder to read as your project grows in complexity to make the output more readable add the options of graph one line and decorate you can now see a more concise breakdown and how different branches connect together but if you're looking at the git log there's likely a commit in there that's currently breaking your app get bisect allows you to start from a commit that is known to have a bug likely the most recent commit but you knew that the app was working a few hours ago you can point bisect to the last known working commit then it performs a binary search to walk you through each commit in between if the commit looks good type bisect good to move on to the next commit eventually you'll find the bad one and know exactly which code needs to be fixed another advanced git technique that every developer should know is how to squash their commits imagine you're working on a feature branch that has three different commits and you're ready to merge it into the master branch i mean main branch but all these commit messages are kind of pointless and it would be better if it was just one single commit we can do that from our feature branch by running git rebase with the interactive option for the main branch that'll pull up a file with a list of commits on this branch if we want to use a commit we just use the pick command we can also change a message using the reword command or we can combine or squash everything into the original commit using squash go ahead and save the file and close it git will pull up another file prompting you to update the commit message which by default will be a combination of all the messages that you just squashed and if you don't like all the messages to be combined you can use fix up instead of squash when doing the rebase to be even more productive you can also use fix up and squash flags when making commits on your branch when you do that it tells git in advance that you want to squash them so when you go to do a rebase with the auto squash flag it can handle all the squashing automatically now if you maintain a repo one tool that can be very useful is get hooks whenever you perform an operation with git like a commit for example it creates an event and hooks allow you to run some code either before or after that event happens if you look in the hidden git directory you'll see a directory there called hooks and inside of it you'll find a bunch of different scripts that can be configured to run when different events in git happen if you happen to be a javascript developer there's a package called husky that makes it much easier to configure git hooks for example you might install it with npm then create a script that will validate or link your code before each commit and that can help improve your overall code quality to wrap things up let's talk about deleting things let's imagine you have a remote repository on github than a local version on your machine that you've been making changes to but things haven't been going too well and you just want to go back to the original state and the remote repo first do a git fetch to grab the latest code in the remote repo then use reset with the hard flag to override your local code with the remote code but be careful your local changes will be lost forever but you might still be left with some random untracked files or build artifacts here and there use the get clean command to remove those files as well but if you're sick of get at this point and want to just get rid of it all together maybe you want to try out apache subversion to change things up a bit all you have to do is delete that hidden git folder and you're on your own again oh and there's one other tip i almost forgot about that comes in really handy if you recently switched out of a branch and forgot its name you can use git checkout followed by a dash to go directly back into the previous branch that you were working on i'm going to go ahead and wrap things up there if you have any additional git tips make sure to leave them in the comments and if you want to master the fundamentals of git step by step check out the full course on fire ship io thanks for watching and i will see you in the next one
Info
Channel: Fireship
Views: 199,033
Rating: 4.9603806 out of 5
Keywords: webdev, app development, lesson, tutorial
Id: ecK3EnyGD8o
Channel Id: undefined
Length: 8min 6sec (486 seconds)
Published: Tue Sep 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.