13 Advanced (but useful) Git Techniques and Shortcuts
Summary
TLDRThis video script offers valuable Git tips for software engineers, emphasizing the importance of mastering Git to avoid common pitfalls like merge conflicts. It introduces shortcuts like the 'ac' alias for commit, explains how to amend commits, and use 'git revert' for undoing mistakes. The script covers advanced techniques such as stashing changes, utilizing GitHub Codespaces, and renaming default branches to avoid controversial terms. It also touches on 'git bisect' for bug tracking, commit squashing, and using git hooks to maintain code quality. Finally, it provides guidance on resetting to a remote state and cleaning untracked files, concluding with a reminder of Git's flexibility and the option to switch to other version control systems if desired.
Takeaways
- 📚 Git is an essential tool for software engineers and mastering it can prevent common mistakes and save time.
- 🔧 The 'git commit -am' flag allows you to add and commit changes in one step, streamlining your workflow.
- 🛠 Creating aliases in your git config can help you to shorten commands and work more efficiently.
- 🔄 The 'git commit --amend' flag is useful for correcting typos in commit messages or adding forgotten files to the last commit.
- ⚠️ Using 'git push --force' can overwrite remote commits but should be used with caution as it can lead to data loss.
- 🔙 The 'git revert' command can undo a commit by creating a new commit that reverses the changes, without erasing history.
- 🌐 GitHub's web-based version of VS Code allows you to work on code repositories from any browser.
- 🛡 The 'git stash' command is useful for temporarily saving changes that aren't ready to be committed.
- 🔍 'git bisect' can help you find the commit that introduced a bug by performing a binary search through the commit history.
- 🔧 'git rebase -i' allows you to squash commits together, creating a cleaner commit history for merges.
- 🔑 Git hooks can automate tasks before or after git events, like running tests before commits, to maintain code quality.
- 🗑️ 'git clean' can remove untracked files and build artifacts, helping to clean up your working directory.
- 🔄 Renaming the default 'master' branch to 'main' or another term is a modern practice to avoid outdated terminology.
- 🔍 If you forget the name of a branch, 'git checkout -' can help you return to the previous branch you were working on.
Q & A
What is the main theme of the video script?
-The main theme of the video script is to provide tips and tricks to make viewers more productive with Git, a version control system for software development.
What advice does the speaker suggest for new Git users?
-The speaker suggests that new Git users should start with a short course on fireship.io, which is designed as a modern learning path to get them started with Git and GitHub.
What is the purpose of using the 'am' flag in Git?
-The 'am' flag in Git allows users to commit changes without using 'git add' first, by automatically adding all the files in the current working directory to the commit.
Why might someone use the 'amend' flag in Git?
-The 'amend' flag is used to update the latest commit with a new message or additional files that were missed in the previous commit, without creating a new commit.
What does the speaker mean by 'git push with the force flag'?
-Using 'git push with the force flag' means overwriting the remote commit with the state of your local code. This should be used with caution as it can permanently delete commits from the remote repository.
What is the 'git revert' command used for?
-The 'git revert' command is used to undo a commit by going back to the state that was there previously, without removing the original commit from the history.
How can one work on a GitHub repository without local machine access?
-One can work on a GitHub repository using any computer with a web browser by visiting GitHub, finding the repository, and using the browser-based version of VS Code that appears when the period key is pressed.
What is the 'git stash' command used for?
-The 'git stash' command is used to save changes that are not ready to be committed yet, by removing them from the current working directory and storing them for later use.
Why should developers avoid using the term 'master' for the default branch in Git?
-The term 'master' is no longer the preferred nomenclature post-2020 due to its association with slavery. Developers should use terms like 'main', 'mega', or 'mucho' to avoid controversy.
What does the 'git bisect' command do?
-The 'git bisect' command performs a binary search to help find a specific commit that introduced a bug by comparing a known buggy commit with a previously known working commit.
What is the purpose of 'git rebase' with the interactive option?
-The purpose of 'git rebase' with the interactive option is to allow developers to edit, combine, or squash commits from a feature branch into a single commit before merging it into the main branch.
What are 'git hooks' and how can they be useful for repository maintenance?
-Git hooks are scripts that run before or after certain Git events, such as commits. They can be configured to run code that validates or links code, improving overall code quality and helping maintain the repository.
How can one reset their local changes to match the remote repository?
-To reset local changes to match the remote repository, one should first 'git fetch' to get the latest code, then use 'git reset --hard' to override local changes with the remote code.
What command can be used to remove all untracked files and build artifacts in a Git repository?
-The 'git clean' command can be used to remove untracked files and build artifacts in a Git repository.
How can one quickly switch back to the previous branch in Git?
-To quickly switch back to the previous branch, use 'git checkout -' which takes you directly to the last branch you were working on.
Outlines
📘 Git Mastery Tips and Tricks
This paragraph introduces the importance of mastering Git for software engineers, highlighting the potential chaos that can ensue from improper use. The speaker suggests that Git's complexity is by design, to challenge and strengthen users. The video promises to share various tips to enhance productivity with Git, and mentions a recently released course on Fireship.io designed to quickly transform beginners into proficient Git users. Basic Git commands are assumed knowledge, and the paragraph ends with a more efficient way to commit changes using the 'am' flag and aliases in Git configuration.
🛠 Advanced Git Techniques and Tools
The second paragraph delves into advanced Git techniques, starting with the 'amend' flag for correcting commit messages and including forgotten files. It discusses the implications of force-pushing and the use of 'git revert' for undoing commits. The paragraph also covers the use of GitHub's browser-based code editor and GitHub Codespaces for remote work. It introduces 'git stash' for saving and applying incomplete changes, and the importance of updating the default branch name from 'master' to 'main' or another term to avoid controversy. The speaker then explains how to use 'git log' with additional options for clarity, and introduces 'git bisect' for identifying the introduction of bugs. The paragraph concludes with a discussion on 'git rebase' for squashing commits and the use of Git hooks to automate tasks, suggesting 'husky' for JavaScript developers.
Mindmap
Keywords
💡Git
💡Pull Request
💡Merge Conflicts
💡Stash
💡Amend
💡Revert
💡Code Space
💡Alias
💡Rebase
💡Bisect
💡Hooks
💡Clean
Highlights
Emphasizing the importance of mastering Git for software engineers to avoid common mistakes and challenges.
Introduction of a short course on Fireship.io for modern learning path to Git and GitHub.
Using the 'am' flag to commit changes without 'git add' for a faster workflow.
Creating aliases in Git config to shorten commands, like 'ac' for 'add commit'.
Using the 'amend' flag to correct the last commit message or include forgotten files.
Warning against using 'git push --force' due to the risk of overwriting remote commits.
The 'git revert' command to undo a commit by reverting to a previous state.
Using GitHub's browser-based version of VS Code for code editing without local machine access.
Introducing 'git stash' to save and remove uncommitted changes for later use.
Using 'git stash save' with a name for referencing stashes and 'git stash apply' to retrieve them.
The transition from 'master' to 'main' or other terms for the default branch name in Git.
Improving 'git log' readability with options like 'graph', 'oneline', and 'decorate'.
Using 'git bisect' for binary search to find the commit that introduced a bug.
Squashing commits into a single commit using 'git rebase -i' for cleaner merge history.
Using 'fix up' and 'squash' flags during commits to streamline future rebase operations.
Utilizing Git hooks to automate tasks before or after events like commits.
The 'husky' package for easier Git hook configuration in JavaScript projects.
Resetting local changes to match a remote repository using 'git fetch' and 'reset --hard'.
Using 'git clean' to remove untracked files and build artifacts.
The option to switch version control systems by deleting the '.git' folder.
Tip for recovering the name of a recently switched-out branch using 'git checkout -'.
Invitation for viewers to share additional Git tips in the comments.
Transcripts
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
Weitere ähnliche Videos ansehen
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git and GitHub - 0 Experience to Professional in 1 Tutorial (Part 1)
Day-11 | Git Interview Q&A and Commands for DevOps | Real World Example |#devops #github #git #2023
Git Tutorial For Dummies
What is Git and Github?
Day-9 | Git and GitHub | What is GIT ? | What is Version Control ? | #devops #2023 #github #gitlab
5.0 / 5 (0 votes)