fetch

Learn Git from Scratch - Fetch and Pull from Origin



Sharing buttons:

hey what's up guys Alex Sierra thank you

for checking this video and welcome to

another episode about get

[Music]

in this tutorial we're going to take a

look about the differences between and

get pull and get such why we need to use

pull before pushing and why we need to

use fetch when we have a new branch on

another origin on a remote origin of our

git repository so right now we are

currently on our git repo on bitbucket

and we only have the master branch that

is updated was a bit in a month ago the

last time I recorded a tutorial so I was

slacking a lot about this but no worries

I'm recording new stuff right now anyway

if we open our terminal and we access

our get repositories in our local

environment and we check the git status

we'll see that based on my previous

tutorial or something that I edited I

have five commits in my current branch

in my current stage that haven't been

pushed yet so I could simply just write

get push and push all these commits to

the origin to the github repository but

what if we have some edits that another

developer did from another computer and

pushed in our git repositories so that's

what I'm going to simulate right now for

example if I from inside github and

something that usually never happens but

bear with me it's just an example I

create a new file and I'm gonna name

this file for example about dot HTML I'm

going to write just simple HTML like an

h1 this is my about write about me and

then just a simple paragraph these is my

about page just like that nothing

special nothing particular and that sit

automatically github is going to create

a commit for us and it's going to commit

to the master branch or I can create a

new branch we're going to take a look at

the same thing but for now let's create

just a new commit create about HTML to

the master branch so commit new file and

that's it that's perfect now we have

these new commits just now that we

implemented we pushed kind of pushed

directly in the origin the about HTML

and I don't have that about dot HTML in

my current repository because if I

access the repository on sublime you see

I just have index licensing style so if

I try to get push to the origin that

it's on github I will have an error

message or a warning it's failed to push

because updates were just rejected

because the remote repository contains

work that you don't have locally so that

means that something was updated in the

origin in the remote repository and I

don't have my local repository updated

currently so get cannot push to another

repository if the code is ahead before

doing that anything else I have to get

pull and that means I'm pulling all the

new editions all the new stuff that I

don't currently have on my local

environment and most likely when you

pull something on from a remote

repository something that someone else

did you will have probably an auto

merging conflict or merging issue that

you have to solve it

most likely these happens when you have

compiled code like compiled CSS minified

CSS minified javascript and stuff like

that so that usually happens right now

I'm dealing just with a single file that

is the about dot HTML then now I have

currently on my repo and that's perfect

so now I can safely push something that

I wasn't allowed to do before so I push

the new stuff that it are all these

things inside the index of the HTML that

I did in the previous lesson so if now I

go and check my git repository and i

refresh my page you will notice that the

index.html was updated based on the

latest commits that I did on my local

Chane that's perfect let's create

another example for example let's create

a new file and I'm going to create now

this new file I'm going to call it

contact dot HTML and here I'm going to

write exactly the same h1 and US and

then close h1 and then another paragraph

page and now let's close the paragraph

perfect

now instead of committing these new file

to the master branch I'm going to create

a new branch then I'm going to simply

call contact and you can call this

branch however you want are usually like

as I said this is not something that you

should do you should not create a new

branch convene new files directly on

github or directly on your origin you

should do it on local and then push it

remotely but this is just an example for

example if someone else another

developer on another machine just

commits this file so propose the new

file and here we're going to have a pull

request confirmation because we are

creating directly on github so the

system needs to have a confirmation if

we want to review it if you want to

leave a comment you want to edit the

code if we want to reject it because

that could happen that a user decides to

help us directly on our git repository

and we can decide what to do with this

code let's simply create a pull request

and let's confirm that everything is ok

so if we go back to code and we open the

branches now we're going to have the

contact branch that if we open it we'll

see we have this contact HTML file that

we currently don't have on our master

branch so what is accessing back my

local repository what if I want to

switch to this check out contact branch

or like uppercase contact branch the

contact doesn't match any file doesn't

match any branch so it get check out

contact there it doesn't exist a branch

like this but I cannot create a branch

called

contact

and then pull and push from my remote

repository because that branch already

exists

I cannot absolutely do that I need to

fetch this branch before being able to

work on it so in order to fetch the

branch so let's simply write get fetch

origin and then get checkout contact

perfect so this was really easy because

with the fetch origin we specify to the

origin that is our git repository on

github to fetch all the branches that we

currently gonna have on our local

environment if we wanted to fetch just a

specific branch you can just specify get

fetch origin and then the contact name

of the branch and then call on contact

name of the branch so it's going to

create the new branch by pulling just

the branch for the origin but if you

just write get fetch origin is going to

get is going to recognize automatically

all the branches that you currently

don't have on your local git repository

but our presence they exist on our

remote repository so now we are on the

get content and if we check our code

editor of course we're going to have the

contest HTML page that we don't have in

our master branch so it's pretty much it

for this video thank you so much for

checking it if you liked it please give

it a thumbs up or subscribe to my

channel and until the next lesson as

usual happy coding

you