Pulling and Pushing

Overview

Teaching: 10 min
Exercises: 5 min
Questions
  • How do I keep my local branches in sync with the remote branches?

  • What is the difference between forking and branching?

Objectives
  • Push changes to a remote repository.

  • Pull changes from a remote repository.

Multiple remote branches

Just as you can have multiple local branches, you can also have multiple remote branches. These may or may not be upstreams for your local branches.

As a reminder, remote and local repositories are not automatically synchronised, but rather it is a manual process done via git pull and git push commands. This synchronisation needs to be done branch by branch with all of those you want to keep in sync.

Pushing

Let’s try to push changes to the main branch. First make sure you are on the main branch.

git switch main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 6 commits.
  (use "git push" to publish your local commits)

Notice that git is suggesting you to use git push to publish your local commits. Let’s do that:

git push
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 8 threads
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 1.83 KiB | 938.00 KiB/s, done.
Total 18 (delta 4), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (4/4), completed with 1 local object.
To https://github.com/username/recipe.git
   57d4505..d10e1e9  main -> main

Now you try

You should now be on the main branch. Try switching to the spicy branch and pushing changes to it.

  1. Check the current branch using git branch.
  2. If the current branch is main, then switch to spicy using git switch spicy.
  3. Push changes to the spicy branch using git push.

Solution

git branch
git switch spicy
git push

This should give an error that the current branch spicy has no upstream branch:

fatal: The current branch spicy has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin spicy

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

Push again by setting the upstream:

git push -u origin spicy
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'spicy' on GitHub by visiting:
remote:      https://github.com/username/recipe/pull/new/spicy
remote:
To https://github.com/username/recipe.git
 * [new branch]      spicy -> spicy
branch 'spicy' set up to track 'origin/spicy'.

Pulling

Git collaborative

Key Points

  • With git push you can push any committed changes in your current branch to its upstream branch.

  • If the current branch has no upstream yet, you can configure one by doing git push -u origin BRANCH_NAME.

  • Using git pull will bring changes in the upstream branch to the local branch.

  • If the local and upstream branches have diverged (have different commit history), then git pull will attempt to merge both. If there are conflicts, you will have to resolve them.