HomeVideos

Manage Branches easily using Git Worktree

Now Playing

Manage Branches easily using Git Worktree

Transcript

102 segments

0:00

so say you're working on a new feature

0:01

and as part of this feature you've made

0:04

some big changes to the code base you've

0:06

added some files you've deleted some

0:08

maybe added a couple of new dependencies

0:11

and at this point say a teammate wants

0:15

you to check out another branch on your

0:17

system or maybe you've noticed a small

0:21

regression while building your feature

0:23

and you want to see if your feature

0:25

introduced the regression or if it

0:27

already existed on the base branch and

0:29

since get allows you to only have one

0:31

active branch per directory you would

0:35

have to save your work by stashing it

0:38

and then you would jump to the branch

0:41

that maybe your teammates asking you to

0:43

check out and then you would jump back

0:47

to your branch and then you would do a

0:48

get stash pop so that your changes that

0:51

you were in the middle of making are not

0:54

lost

0:54

but take this as an example I've made a

1:01

change to the dependency list in this

1:04

branch so if I switch to the branch that

1:07

my team in wants me to check out I would

1:11

have to reinstall the dependencies after

1:14

checking out his branch do the testings

1:16

and then when I moved back to my feature

1:19

branch I would have to reinstall all

1:21

these dependencies again since the

1:23

dependency has changed here so one way

1:26

to solve this would be to have both the

1:27

branches available on your system so

1:29

that you don't have to keep installing

1:32

the dependencies when you switch these

1:33

branches and one way to do this would be

1:36

to reach lone your project in a

1:39

different folder with that branch

1:41

checked out so that you can just switch

1:42

between folders where different branches

1:45

are checked out maybe you one is your

1:47

teammates branch and the other one is

1:49

your feature branch and we could do this

1:51

manually but get provides a nifty little

1:54

command called get work tree there are

1:56

allows you to do this automatically so

1:58

how does get work tree work so let's say

2:01

I

2:02

and in the middle of making my changes

2:05

and someone's asking me to check out the

2:08

master branch for this for this

2:11

repository I in serve stashing now I can

2:15

use the gate

2:16

worked recommend to add a new directory

2:20

on my system let's call that master and

2:24

get here would re clone my repository in

2:28

this master folder and we also need to

2:31

specify the branch that you want to

2:33

check out automatically in the folder as

2:34

well so in this case gate would check

2:37

out the repository in the master folder

2:40

and would check out the master branch so

2:43

on running this we can see that the work

2:45

tree was created so now if I hop out of

2:48

my current project folder I can see that

2:53

I have my current project folder but I

2:55

also have the new project folder that we

2:57

just created using get work tree call

3:00

master so I can move into master and

3:03

then I can see that my project was

3:05

reclone and the master branch was

3:07

checked out automatically so now I can

3:10

do my testing maybe check out someone

3:13

something that my team mate is asking me

3:15

to check out and then I can go back to

3:18

my initial project folder and I have all

3:21

my changes untouched and I didn't have

3:24

to make any big changes in terms of

3:27

cleanup or reinstalling dependencies and

3:29

the reason why I calls a cleaner

3:31

approach is that the work tree command

3:34

lets you see all the actively present

3:37

work trees on your system for instance

3:39

it's showing me that my new features

3:42

checked out in this project folder and

3:44

my master the branch is checked out in

3:46

this project folder and maybe after some

3:50

time I figure that this is like

3:53

redundant now I've done my testing and I

3:56

can't remove it I can use the gate work

3:59

tree command again to remove a work tree

4:02

so in this case which is master and with

4:05

that the cleanup is done the folder that

4:10

was created by gate work tree has been

4:12

cleaned from the system and I don't have

4:15

to manage

4:15

on my own anymore so this is how you can

4:19

use get work tree to jump between

4:20

branches when you have a lot of unsaved

4:23

changes and jumping between branches is

4:26

going to be a hassle

Interactive Summary

This video explains the limitations of using standard Git branching when dealing with unsaved changes or heavy dependencies, and introduces 'git worktree' as a more efficient solution. By allowing multiple branches to be checked out simultaneously in separate directories, 'git worktree' avoids the need for stashing work or repeatedly reinstalling dependencies, offering a cleaner workflow for managing multiple tasks.

Suggested questions

3 ready-made prompts