How Cloning Works
- After cloning there will be two branch references:
- A regular branch reference that I can move around myself.(the default branch of the Github repo)
- origin/master. This is a "Remote Tracking Branch". It's a reference to the state of the master branch on the remote. I can't move this myself. It's like a bookmark pointing to the last known commit on the master branch on origin remote.
Remote Tracking Branches
- "At the time you last communicated with this remote repository, here is where x branch was pointing"
- They follow this pattern remote/branch.
- origin/master references the state of the master branch on the remote repo named origin.
- upstream/logoRedesign references the state of the logoRedesign branch on the remote named upstream (a common remote name)
- Run
git branch -r
to view the remote branches our local repository knows about. - After committing, the master branch will move but the remote tracking branch will remain unchanged
-
When you run
git status
, it will show that the master branch is ahead of ‘origin/master’ by 1 commit. To sync the remote branch with the local branch ,rungit push
git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
- To go back to the original cloned state, run
git checkout origin/master
Remote Branches
- Once you've cloned a repository, we have all the data and Git history for the project at that moment in time. However, that does not mean it's all in our workspace!
- The github repo has a branch called puppies, but when I run
git branch
I don't see it on my machine! All I see is the master branch. So what's going on? - Run
git branch -r
to view the remote branches our local repository knows about. - By default, my master branch is already tracking origin/master but there are no branches to track other remote branches. I want my own local branch called puppies, and I want it to be connected to origin/puppies, just like my local master branch is connected to origin/master.
- Run
git switch <remote-branch-name>
to create a new local branch from the remote branch of the same name and connect it to the remote branch. For example,git switch puppies
makes me a local puppies branch AND sets it up to track the remote branch origin/puppies.
Git Fetch
- Suppose that while you are working on the Local repository, the Github Repo has changed. How would you get those changes?
- Fetching allows us to download changes from a remote repository, BUT those changes will not be automatically integrated into our working files.
- It lets you see what others have been working on, without having to merge those changes into your local repo.
- Think of it as "Please go and get the latest information from Github, but don't screw up my working directory."
- The
git fetch <remote>
command fetches branches and history from a specific remote repository. It only updates remote tracking branches. For example,git fetch origin
would fetch all changes from the origin remote repository. -
We can also fetch a specific branch from a remote using
git fetch <remote> <branch>
. For example,git fetch origin master
would retrieve the latest information from the master branch on the origin remote repository.
git fetch <remote> # If not specified, <remote> defaults to origin
- I now have those changes on my machine, but if I want to see them I have to checkout origin/master. My master branch is untouched!
Git Pull
-
git pull
is another command we can use to retrieve changes from a remote repository. Unlike fetch, pull actually updates our HEAD branch with whatever changes are retrieved from the remote. - "go and download data from Github AND immediately update my local repo with those changes"
- To pull, we specify the particular remote and branch we want to pull using
git pull <remote> <branch>
. Just like with git merge, it matters WHERE we run this command from. Whatever branch we run it from is where the changes will be merged into. -
git pull origin master
would fetch the latest information from the origin's master branch and merge those changes into our current branch. - If we run
git pull
without specifying a particular remote or branch to pull from, git assumes the following:- remote will default to origin
- branch will default to whatever tracking connection is configured for your current branch which is already setup.
- For example, if I am on my local master branch, then
git pull
would pull from origin/master automatically. - Pulls can result in conflicts hence we should always pull first, then merge conflicts and then push.
Git Fetch | Git Pull |
---|---|
Gets changes from remote branch(es) | Gets changes from remote branch(es) |
Updates the remote-tracking branches with the new changes | Updates the current branch with the new changes, merging them in |
Does not merge changes onto your current HEAD branch | Can result in merge conflicts |
Safe to do at anytime | Not recommended if you have uncommitted changes! |
Top comments (0)