About
An upstream is a configuration of a local branch that set the remote branch that it's tracking.
By default, when cloning, each local branch gets as upstream the remote branch of the cloned remote repository.
Name Shorthand
@{u} or @{upstream} means the upstream branch of the current branch
- @{upstream}
- or @{u}
Usage example with the merge command
git merge @{u}
Management
Get
With Git - rev-parse
git rev-parse --abbrev-ref branch@{u}
Example for the current local branch:
echo $(git rev-parse --abbrev-ref "@{u}")
origin/main
Tip: you can also build it from the config info:
remote="$(git config "branch.${branch}.remote")"
remote_branch="$(git config "branch.${branch}.merge" | cut -d/ -f3-)"
echo "$remote/$remote_branch";
List
git branch -vv
- Output: the upstream remote branch is between []
* main 876e695 [origin/master: gone] Error in travis.yml
Set / Update
You update/set an upstream with the branch command
git branch -u origin/branchName
# or
git branch --set-upstream-to origin/branchName
Branch branchName set up to track remote branch branchName from origin.
You can also set it with a push
git push -u origin master
git push --set-upstream origin master
Add
Add the upstream of your fork as remote
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Control.
git remote -v
origin https://github.com/gerardnico/forked (fetch)
origin https://github.com/gerardnico/forked (push)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY (fetch)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY (push)
You got the origin and the upstream (the forked repo)
Fetch (Sync)
git fetch remoteName
Output example
remote: Enumerating objects: 56, done.
remote: Counting objects: 100% (56/56), done.
remote: Compressing objects: 100% (3/3), done.
Receiving objects: 73% (134/183)sed 54 (delta 53), pack-reused 127 eceiving objects: 67% (123/183)
Receiving objects: 100% (183/183), 40.48 KiB | 1010.00 KiB/s, done.
Resolving deltas: 100% (103/103), completed with 23 local objects.
From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
* [new tag] 2018-04-30 -> 2018-04-30
Merge
- merge command
# Go to the main branch (default)
git checkout main
# merge the upstream main branch in the checked out branch
git merge upstream/main
git merge upstream/master
# Shorthand
git merge @{u}
Diff
Local Branch vs upstream
- Description
git log HEAD..@{u} --oneline
71bfc15 (origin/master) Added ansible-config cli
c4c7124 Added ansible-inventory
- Count
git rev-list HEAD...@{u} --count
2
Upstream vs local branch
git log @{u}..HEAD --oneline
afc0e4b (HEAD -> master) private key enhancement
Support
fatal: no upstream configured for branch 'master'
Example on the master branch
git branch --set-upstream-to origin/master