Git sets the upstream branch
This article will introduce how to establish the relationship between local branches and remote branches.
Git calls set upstream
to establish this relationship.
A local branch is called a tracking branch, and the branch it tracks - a remote branch - is called 上游
a branch.
The purpose of setting up upstream is to make git push
and git pull
easier.
Imagine you have a long branch name like this, feature/a-long-long-branch-for-feature-A
.
Without setting the upstream branch, you need to explicitly execute git commit with the branch name git push
.
For example:
git push origin feature/a-long-long-branch-for-feature-A
It's shorter and you can get rid of the branch name you're working on after setting the upstream branch.
Just execute git push
, clear and concise.
To set the upstream when the remote branch has not yet been created, use --set-upstream-to
the -r option with git push
the -p command.
git push --set-upstream-to origin <branch_name>
or its shorter version,
git push -u origin <branch_name>
$ git push -u origin master
Enumerating objects: 17, done.
...
remote: Create a pull request for 'master' on GitHub by visiting:
...
To github.com:username/repo_name.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
To set the upstream when the remote branch already exists, use the following command.
git branch --set-upstream-to origin/<branch_name>
or,
git branch -u origin/<branch_name>
For example,
$ git branch -u origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Another benefit of setting upstream is to indicate unsynchronized commits between local and remote branches.
$ git branch -u origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.
$ touch file.txt
$ git add file.txt && git commit -m 'Add file.txt'
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
To cancel the upstream setting, use git branch --unset-upstream
; then, git status
no extra information will be displayed.
$ git branch --unset-upstream
$ git status
On branch master
nothing to commit, working tree clean
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing drives in Git Bash
Publish Date:2025/03/30 Views:56 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:105 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se
Differences between Git Reset, Revert and Checkout commands
Publish Date:2025/03/30 Views:185 Category:Git
-
This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with
Git revert local commits
Publish Date:2025/03/30 Views:84 Category:Git
-
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from
Deleting local and remote Git branches
Publish Date:2025/03/30 Views:146 Category:Git
-
Git comes into play in version control systems when you are working with a team and the entire team is making changes to the same code repository. Creating a new branch in Git is relatively easy than other version control systems and deleti