{"id":898,"date":"2021-02-23T19:41:25","date_gmt":"2021-02-23T19:41:25","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=898"},"modified":"2021-02-23T19:41:25","modified_gmt":"2021-02-23T19:41:25","slug":"set-git-upstream-respository-branch","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=898","title":{"rendered":"How to set Git Upstream For a Respository and a Branch"},"content":{"rendered":"<p>In this blog, you will learn everything about <strong>setting up an upstream<\/strong> for a <a href=\"https:\/\/devopscube.com\/git-basics-every-developer-and-administrator-should-know\/\" rel=\"noreferrer noopener\">git<\/a> repository, a branch and the <code>--set-upstream-to<\/code> command usage<\/p>\n<p>You will also learn about different scenarios where git upstream is used and how to apply it in your git development workflow.<\/p>\n<p>Have you wondered how to keep the <strong>forked repo in sync with the main repo<\/strong>? It&#8217;s done by adding the main repo as the upstream. It is explained in the below sections.<\/p>\n<h2 id=\"what-is-upstream-in-git\">What is Upstream in Git?<\/h2>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note: <\/strong><\/b>We are following the name &#8220;main&#8221; instead of &#8220;master&#8221; considering the removal of terms like &#8220;master&#8221; and &#8220;slave&#8221; in the IT community.<\/div>\n<\/div>\n<p>Like the literal meaning, upstream refers to something which points to where it originated from.<\/p>\n<p>In the git world, upstream refers to the original repo or a branch. For example, when you clone from Github, the remote Github repo is upstream for the cloned local copy.<\/p>\n<p>Let&#8217;s take a look at different types of git upstreams.<\/p>\n<h3 id=\"git-repository-upstream\">Git Repository Upstream<\/h3>\n<p>Whenever you clone a git repository, you get a local copy in your system. So, for your local copy, the actual repository is upstream.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/remote-upstream-min-1.png\" class=\"kg-image\" alt=\"git remote upstream\" loading=\"lazy\" width=\"720\" height=\"567\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/remote-upstream-min-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/remote-upstream-min-1.png 720w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h3 id=\"git-forked-repository-upstream\">Git Forked repository Upstream<\/h3>\n<p>When you clone a Forked repository to your local, the forked repository is considered as the remote origin, and the repository you forked from is upstream.<\/p>\n<p>This workflow is common in open-source development and even in normal project developments.<\/p>\n<p>For example, If you wanted to contribute to an open-source project, this is what you do.<\/p>\n<ol>\n<li>You would fork the actual project repository.<\/li>\n<li>Then you clone from the forked repository.<\/li>\n<li>Meanwhile, to keep up with the main open-source repository, you pull the changes from it through your git upstream config.<\/li>\n<li>You push your changes to a branch in the forked repository.<\/li>\n<li>Then you would raise a PR to the actual project repository from your forked repository. <\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-forked-upstream-min-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"720\" height=\"567\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/git-forked-upstream-min-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-forked-upstream-min-1.png 720w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Tip:<\/strong><\/b> With <a href=\"https:\/\/devopscube.com\/jenkins-multibranch-pipeline-tutorial\/\" rel=\"noreferrer noopener\">Jenkins multibranch pipelines, you can easily set up<\/a> the hooks for pull requests from a Forked branch.<\/div>\n<\/div>\n<h2 id=\"setup-git-upstream-for-a-repository\">Setup Git Upstream For a Repository<\/h2>\n<p>Let&#8217;s say you are working on a forked project and you want to sync changes from the main project repo.<\/p>\n<p>You can add the actual repo as an upstream to your local copy. This way you can pull all the changes happening in the main project repo.<\/p>\n<h3 id=\"add-an-upstream-repo\">Add an Upstream Repo<\/h3>\n<p>Before adding upstream, you can take a look at your <code>.git\/config<\/code> file and you will see something like below with a remote origin. Which is a clone of the forked repo.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-remote-origin-min-1.png\" class=\"kg-image\" alt=\"git forked repo without upstream repo\" loading=\"lazy\" width=\"580\" height=\"231\"><\/figure>\n<p>Now lets the add the upstream to our repo with the main project repo URL using the <code>add upstream<\/code> command.<\/p>\n<pre><code>git remote add upstream https:\/\/github.com\/devopscube\/vagrant-examples.git<\/code><\/pre>\n<p>Now if you check the .<code>git\/config<\/code>, you will see an upstream entry as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-upstream-example-min-1.png\" class=\"kg-image\" alt=\"git repo with upstream repo\" loading=\"lazy\" width=\"584\" height=\"284\"><\/figure>\n<p>You can also check out the remote origin and upstream branches using the following command.<\/p>\n<pre><code>git branch --remotes<\/code><\/pre>\n<p>Here is a sample output.<\/p>\n<pre><code>vagrant@dcubelab:~\/vagrant-examples$ git branch --remotes\n  origin\/HEAD -&gt; origin\/main\n  origin\/dev\n  origin\/main\n  upstream\/dev\n  upstream\/main<\/code><\/pre>\n<h3 id=\"sync-changes-from-the-upstream-repo\">Sync Changes From The Upstream repo<\/h3>\n<p>To sync an upstream repo, first, you need to fetch the upstream changes.<\/p>\n<pre><code>git fetch upstream<\/code><\/pre>\n<p>Then, merge the changes from the upstream branch to the local branch. In this example, it&#8217;s the main upstream branch.<\/p>\n<pre><code>git merge upstream\/main<\/code><\/pre>\n<h2 id=\"setup-git-upstream-for-a-branch\">Setup Git Upstream For a Branch<\/h2>\n<p>But when we talk about an upstream branch, it refers to the specific upstream of a branch in the remote respository.<\/p>\n<p>For example, let&#8217;s say you clone a git repository with two branches, <strong>main and dev<\/strong>.<\/p>\n<p>When you clone the repository both the branches, main, and dev will have the respective upstream branches (remote branches) because these branches are present in both the local copy as well the remote or upstream repository. Also, the tracking is enabled by default.<\/p>\n<p>Now let&#8217;s look at scenarios where you want to set up git upstream using <code>--set-upstream-to<\/code> command.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Important Note:<\/strong><\/b> In git version update, 1.8.0 the <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">--set-upstream<\/code> the command is changed to <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">--set-upstream-to<\/code><\/div>\n<\/div>\n<h3 id=\"add-git-upstream-usingset-upstream-to\">Add Git Upstream Using &#8211;set-upstream-to<\/h3>\n<p>When you create a branch (named foo)  in your local repository and want to add an upstream branch for tracking,  you can use the following command.<\/p>\n<pre><code>git push -u origin foo<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Important Note:<\/strong><\/b> Here <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">-u<\/code> is the shorthand for <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">--set-upstream-to<\/code><\/div>\n<\/div>\n<p>When you push a local branch with the upstream command, it automatically creates the remote branch and adds tracking to your local branch.<\/p>\n<h3 id=\"add-git-upstream-to-existing-remote-branch\">Add Git Upstream To Existing Remote Branch<\/h3>\n<p>Sometimes, when you create a local branch, you might push the remote repository changes without adding the upstream tag. For example,<\/p>\n<pre><code>git push origin foo<\/code><\/pre>\n<p>It will create the remote branch without tracking. Meaning, when someone pushes any change to the branch foo in the remote branch, and when you try to pull the changes, it will throw the following error.<\/p>\n<pre><code>There is no tracking information for the current branch.\nPlease specify which branch you want to merge with.\nSee git-pull(1) for details\n\n    git pull &lt;remote&gt; &lt;branch&gt;\n\nIf you wish to set tracking information for this branch you can do so with:\n\n    git branch --set-upstream-to=origin\/&lt;branch&gt; testing<\/code><\/pre>\n<p>To avoid this, you can set git upstream using the following command. It will enable tracking, and you will be able to pull the changes.<\/p>\n<pre><code>git branch --set-upstream-to origin\/foo<\/code><\/pre>\n<h3 id=\"add-git-upstream-using-git-head\">Add Git Upstream Using Git HEAD<\/h3>\n<p>In git, HEAD refers to the currently active branch.<\/p>\n<p>For example, if you have checked out <code>foo<\/code> branch, your HEAD now is <code>foo<\/code>.<\/p>\n<p>So when you create a new local branch named foo, you can create the upstream using the HEAD as shown below.<\/p>\n<pre><code>git push -u origin HEAD<\/code><\/pre>\n<h2 id=\"git-set-upstream-faqs\">Git set-upstream FAQ&#8217;s<\/h2>\n<p>The following are the frequently asked questions.<\/p>\n<h3 id=\"what-is-the-need-to-set-upstream-in-git\">What is the need to set upstream in git?<\/h3>\n<p>By setting upstream in git, the local branches can track the remote branches. This makes pulling changes from the remote repository very easy.<\/p>\n<h3 id=\"how-to-keep-the-original-and-forked-repo-in-sync\">How to keep the original and forked repo in sync?<\/h3>\n<p>By setting the original repo as upstream on your local copy, you can fetch the original repo changes and push them to the forked repo to keep it in sync.<\/p>\n<h3 id=\"what-is-an-upstream-branch-in-git\"><strong>What is an upstream branch in Git?<\/strong><\/h3>\n<p>When you clone a git repository, the parent or remote repository branches are called the upstream branches for the local branches.<\/p>\n<h3 id=\"how-to-check-the-git-upstream-branches\">How to check the git upstream branches?<\/h3>\n<p>You can check all the upstream branches using the git branch -vv command. It will show all the upstream tracking mapped to the local branches.<\/p>\n<p>If you want to clone or checkout a specific commit id, checkout my blog on <a href=\"https:\/\/devopscube.com\/checkout-clone-specific-git-commit-id-sha\/\" rel=\"noreferrer noopener\">git checkout specific commit id.<\/a><\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/set-git-upstream-respository-branch\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to set Git Upstream For a Respository and a Branch \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/set-git-upstream-respository-branch\/<\/p>\n","protected":false},"author":1,"featured_media":899,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=898"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/898\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/899"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}