13 Advanced (but useful) Git Techniques and Shortcuts

Fireship
7 Sept 202108:06

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

00:00

πŸ“˜ 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.

05:02

πŸ›  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

Git is a version control system that tracks changes in source code during software development. It is essential for managing and coordinating code changes among multiple contributors. In the video, Git is the central theme, with various tips and tricks discussed to enhance productivity for software engineers.

πŸ’‘Pull Request

A pull request in Git is a method for proposing changes to a repository by comparing the target branch's code with the new contributions from a feature branch. The script mentions the fear of encountering failed checks and merge conflicts when submitting a pull request, which can be addressed by mastering Git commands.

πŸ’‘Merge Conflicts

Merge conflicts occur in Git when changes in different branches overlap in the same part of a file, and Git cannot automatically reconcile the differences. The video script discusses how to handle such conflicts as part of becoming proficient with Git.

πŸ’‘Stash

The 'stash' command in Git temporarily stores modified, tracked changes in a new stash stack, allowing the user to revert to a clean working directory. The script uses 'stash' as an example of a command that can help manage changes that are not yet ready to be committed.

πŸ’‘Amend

The 'amend' flag in Git allows a user to modify the most recent commit by adding new changes or altering the commit message. The script explains how to use 'amend' to correct typos in commit messages or to include additional files in the last commit.

πŸ’‘Revert

The 'revert' command in Git is used to create a new commit that undoes the changes made by a previous commit. The script mentions 'revert' as a way to undo a push that introduced problematic code without erasing the original commit from the history.

πŸ’‘Code Space

GitHub Code Spaces is a cloud-based development environment that allows developers to write, build, and test code using a browser or Visual Studio Code. The script suggests using Code Spaces for working on a repository without local machine access.

πŸ’‘Alias

In Git, an alias is a shortcut for a longer command or a sequence of commands. The script introduces the concept of creating aliases like 'ac' for 'add commit' to streamline the workflow and save time.

πŸ’‘Rebase

Git rebase is a command that allows the integration of changes from one branch into another interactively. The script discusses using 'rebase' to squash commits into a single commit for a cleaner merge into the main branch.

πŸ’‘Bisect

The 'bisect' command in Git is used to find the commit that introduced a bug by performing a binary search through the commit history. The script describes 'bisect' as a tool for identifying problematic commits when debugging.

πŸ’‘Hooks

Git hooks are scripts that run automatically before or after certain Git events, such as commit or push. The script mentions using hooks to automate tasks like code validation, which can improve code quality.

πŸ’‘Clean

The 'clean' command in Git is used to remove untracked files from the working directory. The script suggests using 'clean' to remove unwanted files after resetting the local code to match the remote repository.

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

play00:00

one of the best pieces of advice i ever

play00:01

got was get good at git few things are

play00:04

more scary for a software engineer than

play00:06

screwing something up with git after

play00:08

weeks of writing the perfect code you

play00:09

submit a pull request and end up with a

play00:11

bunch of embarrassing failed checks and

play00:13

merge conflicts next thing you know

play00:15

you're scrambling on stack overflow

play00:16

looking for a solution that will likely

play00:18

just add gasoline to the fire git was

play00:20

created by a guy much smarter than us he

play00:22

knew if he made it too easy it would

play00:23

make us weak instead he wanted to give

play00:25

us the glory of overcoming the challenge

play00:34

i'm pretty sure that's what he was

play00:35

thinking in today's video we'll look at

play00:36

a bunch of different tips and tricks to

play00:38

make you more productive with git but if

play00:40

you're new to git i just released a

play00:42

short course on fire fireship io that's

play00:44

designed as a modern learning path to

play00:45

get you started with git and github it

play00:47

contains a bunch of short videos along

play00:49

with exercises to transform you into a

play00:51

professional git user quickly this

play00:53

content is supported by viewers like you

play00:55

and i want to send a huge thank you to

play00:57

everybody out there who's already a pro

play00:58

member so to get started i'm assuming

play01:01

you know the basics of git like you know

play01:02

how to do a git ad followed by a git

play01:04

commit to save a snapshot of your code

play01:06

that's usually the first thing you learn

play01:08

in git but there's actually a better way

play01:10

to get the job done you can get rid of

play01:11

the git add and go straight to commit by

play01:14

using the am flag this will

play01:16

automatically add all the files in the

play01:18

current working directory that's a nice

play01:20

improvement but there's actually an even

play01:21

more concise way to get the job done

play01:23

your git config provides a way to create

play01:25

aliases which are commonly used to

play01:27

shorten existing commands or create your

play01:29

own new custom commands for example we

play01:31

might create an alias called ac that

play01:34

runs the add and commit command with

play01:35

just two letters that allows us to get

play01:37

things done faster but sometimes going

play01:39

fast leads to mistakes clearly mistakes

play01:42

were made what if you made a typo on

play01:43

your last commit message instead of

play01:45

resetting and creating a new commit the

play01:47

amend flag followed by a new message

play01:49

will simply update the latest commit or

play01:51

maybe you forgot to include or stage a

play01:53

couple files with your last commit you

play01:55

can also update the last commit with new

play01:57

files by using the amend flag and if you

play01:59

want to keep the same commit message add

play02:00

the no edit flag as well but keep in

play02:03

mind this only really works if you

play02:04

haven't already pushed your code to a

play02:06

remote repository unless you like to

play02:08

live dangerously in which case you can

play02:10

do a git push with the force flag this

play02:12

will overwrite the remote commit with

play02:14

the state of your local code however if

play02:16

there are commits on the remote branch

play02:18

that you don't have you will lose them

play02:19

forever and your co-workers might not be

play02:21

too happy about that don't make me swing

play02:23

on you bro but what happens if you push

play02:25

some code to a remote repository then

play02:27

realize it's complete garbage and never

play02:29

should have been there in the first

play02:29

place the git revert command allows you

play02:32

to take one commit and go back to the

play02:34

state that was there previously it's

play02:35

kind of like an undo but it doesn't

play02:37

remove the original commit from the

play02:38

history instead it just goes back to the

play02:40

original state and that's much easier

play02:42

than trying to put things back together

play02:44

like a trauma surgeon

play02:46

too much blood i can't see a thing in

play02:47

other cases you may need to work on a

play02:49

repo but not have access to your local

play02:51

machine if you're at your grandma's

play02:53

house without your laptop you can use

play02:54

any computer that has a web browser go

play02:56

to github and find the repo that you

play02:58

want to work on then hit the period key

play03:00

on your keyboard and like magic it pulls

play03:02

up a browser-based version of vs code

play03:04

where you can make edits submit pull

play03:05

requests and do almost anything else you

play03:07

could do locally well except for run the

play03:09

terminal if you do need to run terminal

play03:11

commands you can set up a github code

play03:13

space in the cloud which will give you

play03:15

the full power of vs code and is likely

play03:16

much faster than your grandma's computer

play03:18

but now let's switch gears to one of my

play03:20

favorite git commands stash have you

play03:22

ever spent time working on some changes

play03:24

that almost work but they can't really

play03:25

be committed yet because they break

play03:27

everything else or maybe it's just

play03:28

really sloppy and you don't want all

play03:30

your friends to see it yet git stash

play03:32

will remove the changes from your

play03:33

current working directory and save them

play03:35

for later use without committing them to

play03:37

the repo the simple way to use it is get

play03:39

stash and then get pop when you're ready

play03:41

to add those changes back into your code

play03:43

but if you use the command a lot you can

play03:45

use git stash save followed by a name to

play03:48

reference it later then when you're

play03:50

ready to work on it again use git stash

play03:52

list to find it then git stash apply

play03:54

with the corresponding index to use it

play03:56

now if you want to use a stash at

play03:58

grandma's house you're pretty much sol

play04:00

unless you use a github code space in

play04:02

which case your stashes would be saved

play04:04

in the cloud that's pretty cool but now

play04:06

i have a public service announcement for

play04:08

developers in the modern era

play04:09

historically the default branch in git

play04:12

is called the master branch but post

play04:14

2020 this term is no longer the

play04:16

preferred nomenclature refer to it as

play04:17

main mega or mucho to stay out of

play04:20

trouble to change it use get branch

play04:22

followed by the m flag and rename it to

play04:24

main or maybe get creative and invent

play04:26

your own name now another command you're

play04:28

probably familiar with is git log to

play04:30

view a history of commits the problem

play04:32

with this command is that the output is

play04:34

harder and harder to read as your

play04:36

project grows in complexity to make the

play04:38

output more readable add the options of

play04:40

graph one line and decorate you can now

play04:42

see a more concise breakdown and how

play04:44

different branches connect together but

play04:46

if you're looking at the git log there's

play04:48

likely a commit in there that's

play04:49

currently breaking your app get bisect

play04:52

allows you to start from a commit that

play04:54

is known to have a bug likely the most

play04:56

recent commit but you knew that the app

play04:57

was working a few hours ago you can

play04:59

point bisect to the last known working

play05:01

commit then it performs a binary search

play05:03

to walk you through each commit in

play05:05

between if the commit looks good type

play05:07

bisect good to move on to the next

play05:09

commit eventually you'll find the bad

play05:11

one and know exactly which code needs to

play05:13

be fixed another advanced git technique

play05:15

that every developer should know is how

play05:17

to squash their commits imagine you're

play05:19

working on a feature branch that has

play05:20

three different commits and you're ready

play05:22

to merge it into the master branch i

play05:24

mean main branch but all these commit

play05:26

messages are kind of pointless and it

play05:27

would be better if it was just one

play05:29

single commit we can do that from our

play05:30

feature branch by running git rebase

play05:33

with the interactive option for the main

play05:35

branch that'll pull up a file with a

play05:37

list of commits on this branch if we

play05:38

want to use a commit we just use the

play05:40

pick command we can also change a

play05:42

message using the reword command or we

play05:44

can combine or squash everything into

play05:46

the original commit using squash go

play05:49

ahead and save the file and close it git

play05:51

will pull up another file prompting you

play05:53

to update the commit message which by

play05:55

default will be a combination of all the

play05:57

messages that you just squashed and if

play05:58

you don't like all the messages to be

play06:00

combined you can use fix up instead of

play06:02

squash when doing the rebase to be even

play06:04

more productive you can also use fix up

play06:06

and squash flags when making commits on

play06:09

your branch when you do that it tells

play06:10

git in advance that you want to squash

play06:12

them so when you go to do a rebase with

play06:14

the auto squash flag it can handle all

play06:16

the squashing automatically now if you

play06:18

maintain a repo one tool that can be

play06:20

very useful is get hooks whenever you

play06:22

perform an operation with git like a

play06:24

commit for example it creates an event

play06:27

and hooks allow you to run some code

play06:29

either before or after that event

play06:31

happens if you look in the hidden git

play06:33

directory you'll see a directory there

play06:35

called hooks and inside of it you'll

play06:36

find a bunch of different scripts that

play06:38

can be configured to run when different

play06:40

events in git happen if you happen to be

play06:42

a javascript developer there's a package

play06:44

called husky that makes it much easier

play06:45

to configure git hooks for example you

play06:48

might install it with npm then create a

play06:50

script that will validate or link your

play06:52

code before each commit and that can

play06:54

help improve your overall code quality

play06:56

to wrap things up let's talk about

play06:58

deleting things let's imagine you have a

play07:00

remote repository on github than a local

play07:02

version on your machine that you've been

play07:04

making changes to but things haven't

play07:05

been going too well and you just want to

play07:07

go back to the original state and the

play07:08

remote repo first do a git fetch to grab

play07:11

the latest code in the remote repo then

play07:14

use reset with the hard flag to override

play07:16

your local code with the remote code but

play07:18

be careful your local changes will be

play07:20

lost forever but you might still be left

play07:23

with some random untracked files or

play07:25

build artifacts here and there use the

play07:27

get clean command to remove those files

play07:29

as well but if you're sick of get at

play07:30

this point and want to just get rid of

play07:32

it all together maybe you want to try

play07:34

out apache subversion to change things

play07:36

up a bit all you have to do is delete

play07:37

that hidden git folder and you're on

play07:39

your own again oh and there's one other

play07:41

tip i almost forgot about that comes in

play07:43

really handy if you recently switched

play07:44

out of a branch and forgot its name you

play07:46

can use git checkout followed by a dash

play07:49

to go directly back into the previous

play07:51

branch that you were working on i'm

play07:52

going to go ahead and wrap things up

play07:54

there if you have any additional git

play07:55

tips make sure to leave them in the

play07:57

comments and if you want to master the

play07:58

fundamentals of git step by step check

play08:00

out the full course on fire ship io

play08:02

thanks for watching and i will see you

play08:04

in the next one

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Git TipsSoftware EngineeringProductivityCode ManagementVersion ControlGitHubCommit StrategiesDevOpsProgrammingTech Tutorial