DEV Community

shun
shun

Posted on

Git Upstream Commands Guide

Git's upstream refers to a default remote branch for a given branch. Setting an upstream makes it easier to synchronize and compare changes between the branch and its corresponding remote branch.

Setting Up Upstream

git remote add upstream <original-repository-url>
Enter fullscreen mode Exit fullscreen mode

This command configures the original repository as a new remote called "upstream".

Configuring an Upstream for a Branch

To set the upstream for a branch:

git branch --set-upstream-to=<remote>/<branch> <local-branch>
Enter fullscreen mode Exit fullscreen mode

For instance, to set the main branch of the origin remote as the upstream for the local feature branch:

git branch --set-upstream-to=origin/main feature
Enter fullscreen mode Exit fullscreen mode

Checking Upstreams

To view all local branches and their respective upstreams:

git branch -vv
Enter fullscreen mode Exit fullscreen mode

Pushing to Upstream

To push changes from the current branch to its upstream:

git push
Enter fullscreen mode Exit fullscreen mode

Pulling from Upstream

To pull changes from the upstream to the current branch:

git pull
Enter fullscreen mode Exit fullscreen mode

By setting up upstreams, you no longer need to specify the remote name and branch name each time you run git push or git pull. This enhances the efficiency of your workflow.

Top comments (0)