Git push New Branch
Often I need to push a new branch to remote here is a helpful flag to know.
Git will not let you push changes if they exist on a local-only branch.
If you do, you will get an error.
fatal: The current branch <Branch Name> has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin <Branch Name>
To avoid copy-pasting the command from the error each time to push your changes, create you can create a function.
To get the current branch you can use the command git rev-parse --abbrev-ref HEAD
function gitpush() {
cur_branch="$(git rev-parse --abbrev-ref HEAD)"
remote=$(git remote)
git push --set-upstream "${remote}" "${cur_branch}"
}
Command
git-push - Update remote refs along with associated objects
SYNOPSIS
git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
[--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
[-u | --set-upstream] [-o <string> | --push-option=<string>]
[--[no-]signed|--signed=(true|false|if-asked)]
[--force-with-lease[=<refname>[:<expect>]] [--force-if-includes]]
[--no-verify] [<repository> [<refspec>...]]
-u, --set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).