Git Set UpStream – What Does It Do & How To Use It?

Git Set Upstream

Git –set-upstream is now used as –set-upstream-to.

Previously it was used as git branch --set-upstream <remote-branch> (may still work for many)

Version Changes Set Upstream Depreciated

The basic git –set-upstream-to command is demonstrated below with an example.

git branch --set-upstream-to <remote-branch>

The use of –set-upstream along with the git branch command was ambiguous, so maybe that’s why they have changed the option name from –set-upstream to –set-upstream-to.

Why the git –set-upstream command is used?

As the name specifies itself It is used with the git branch command option that lets you set the remote branch for the current branch on which you are working locally. In other words, with this command, you’re setting the remote location of your current local branch.

Once you set the <remote branch> for the current local branch with the git --set-upstream or --set-upstream-to command, any future git pull command, when run after checking out to the local branch, will also try to get the commits from the set remote branch as well.

When you set the remote branch for the local branch, now the git knows whenever now you’re going to use commands including git pull, git push or git fetch, which remote branch it has to refer. This really helps a lot when you want to keep the local branch of git as well as the remote branch in sync. For such a syncing, the use of git –set-upstream command is appropriate.

The git checkout --track command can also help you achieve the same thing.

The following command simply helps you to avoid setting the remote branch explicitly for the current local branch explicitly with the –set-upstream option. You just have to use the -u shorthand flag with the git push command. Make sure, this should be your very first git push command.

git push -u origin local-branch

origin here refers to the remote git repository for the current local repository. There could be multiple remote repositories as well. In case, there are multiple remote repositories, you need to replace the origin with the exact name of the remote repository for which you want to run this command.

You can find the remote repositories for your current local git repository, by using the following command.

git remote -v

Git Command To Check Remote Repositories

Suggested Reading.

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *