Skip to content

Keeping your repository up to date

Aine Riordan edited this page Aug 21, 2023 · 1 revision

Keeping your repository up to date

When you are working with a team on a repository, it is important to keep your work in sync with the upstream repository.

Rebasing local tracked branches with upstream branches

  1. Navigate to the branch that you want to sync with upstream.

    git checkout <tracked_branch>
  2. Fetch all commits from upstream.

    git fetch upstream
  3. Run git rebase to incorporate the commits from upstream/<tracked_branch> into your local branch. Do not use git pull, as it adds extra commits.

    git rebase upstream/<tracked-branch>
  4. Push your local branch to your forked repo.

    git push origin <tracked-branch>

The following example syncs your local main branch with the upstream/main branch:

$ git checkout main
$ git fetch upstream
$ git rebase upstream/main
$ git push origin main

Rebasing a topic branch with its upstream parent branch

As you work on your topic branch, it is important to incorporate new commits from the upstream parent branch.

Before you begin:

  • Run git status to check whether you have uncommitted changes on the topic branch; run git commit to save them.

  • Optional steps:

    • To get an idea of how much work the rebase will entail, run git log --oneline -n 5 <topic_branch> and git log --oneline -n 5 <upstream/parent_branch>, or view the parent branch on GitHub. If your topic branch and new parent branch commits both involve changes to the same files, you may need to resolve a conflict.

    • Back up your topic branch by pushing it to your forked repo. This is useful if you have trouble resolving a snarly conflict.

Identify the parent branch

  • If you created your topic branch by branching from main, rebase upstream changes to main with your topic branch.

  • If you created your topic branch by branching from a release branch, rebase changes to the upstream release branch with your topic branch.

  • If you created your topic branch by branching from an upstream feature branch, you must rebase changes in the upstream feature branch with your topic branch.

Rebase commits from the parent branch onto the topic branch

  1. Checkout your local topic branch

    git checkout <topic_branch>
  2. Retrieve new changes from upstream

    git fetch upstream
  3. Rebase upstream changes to the parent branch with your local branch

    git rebase upstream/<parent_branch>
    • If there are no conflicts, git reports the status of the topic branch after the successful rebase.

      $ git rebase upstream/main
      First, rewinding head to replay your work on top of it...
      Applying: Update attribute values for operator components
    • If there are conflicts, the message from git includes CONFLICT. Refer to Resolving conflicts for details.

      $ git rebase upstream/main
      First, rewinding head to replay your work on top of it...
      Applying: Update operator planning procedure
      CONFLICT (content): Merge conflict in downstream/modules/operator-planning.adoc
      Failed to merge in the changes.
      Patch failed at 0035 Update operator planning procedure
  4. Push the changes to to your forked repo.

    git push origin <topic_branch>
Clone this wiki locally