Fetching & Pulling
Last updated
Last updated
What happens when clone git repositorires ?
When we clone a repository from a git repository , we will have a branch more , which provide by cloud github,gitlab etc ... it's origin/master
Discription : origin/master
like a regular branch referench,We can move this around myself and we can also call it's Remote tracking branch
. It's a reference to the state of the master branch on the remote . I can't move this my self.It's like a bookmar poingting to the last known commit on the master branch on origin .
"At the time you last commuicated with this remote repository, here is when x branch was pointing" .
They follow this pattern <remote> / <branch>
: - origin/master
references the state of the master branch on the remote repopo named origin . - upstream/logoRedesign
references the state of the the logoRedesign branch on the remote named upstream (a common remote name) .
Run git branch -r to view to view the remote branches our repository knows about .
git branch -r origin/master
When i run git status
it push notifications . On branch master , Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
git checkout origin/master
we can recive a notifications :
Note : switching to origin/master
.
your are in detached head
state . you can look around , make exper..
Once you've cloned a repository, we have all date and git history for the project at the moment in time. However,that does not mean it's all in my workspace ! .
The github repo has a branch call puppies, but when i run git branch
I don't see it on my machine! All i see is the master branch . what's going on ?
`By default, my master branch is already trackiing origin/master'
I didn't connect these my my self
But i want to work on the puppies branch locally
I could checkout origin/puppies
,but that puts me in detached HEAD .
I want my own local branch call puppies, and i want it to be connected to origin/puppies,just like my local master branch is connected to origin/master
.
It's super easy
Run git switch <remote-branch-name>
to create a new local branch form the remote branch of the same name
git switch puppies make me a local pupies branch AND sets it up to track the remote branch origin/puppies
Anthor way
git checkout --track origin/puppies
The new command git switch makes this super easy to do .! it used to slightly more complicate using git checkout
Fetching allows us to dowload changes from a remote repository,BUT those changes will not be automatically integrated into our working files
It let you see what others have been working on,without having to merge thouse change your local repo .
Think of it as "please go and get the lastest information from github . but don't not screw up working .
The git fetch <remote>
command fetches branches and history from a specific repmote repository. It only updates remote tracking branches .
git fetch origin
would fetch all changes from the origin remote repository .
We can also fetch a specific branch form a remote using
git fetch <Remote> <Branch>
For example, git fetch origin master
would retrive the lastest information from the master branch o nthe origin remote repository.
git pull
is another command we can use to retrieve changese from a remote repository. Unlike fetch, pull actually update our HEAD branch with whatever changes are retrieved from the remote .
"go and dowload data form Github And immediately update my local repo with thouse changes" .
git pull = git fetch + git merge
To pull, we specify the particular remote and branch we want to pull using git pull <remote> <branch>
. Just like with git merge, it matter branch we run it from is where the changes will be merged into
git pull origin master
would fetch the lastest information from the origin's master branch and merge those changes into our current branch .