Skip to main content

2 posts tagged with "git"

View All Tags

Git push New Branch

· 2 min read
Aurelian Shuttleworth
Aurelian Shuttleworth
Site Reliability Engineer

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.

note

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).

Rebasing is boring

· 2 min read
Aurelian Shuttleworth
Aurelian Shuttleworth
Site Reliability Engineer

The problem

When working in a team, you must ensure you sync code reviews with your default branch. Making sure you are up to date is definitely a chore and will take some time out of your day, but it will be worth it.

For me, the primary time sink is having to run the same commands and wait for the outputs each time.

  1. git checkout <default branch>
  2. git pull
  3. git pull again because I have not logged in today.
  4. git checkout <branch I was working on>
  5. git rebase <default branch> and hope there are no new merge conflicts.
  6. git push -f
  7. Check that my pipelines still work.

As you can imagine, this is a tedious process and can easily result in you messing up.

Solution

An easy fix is adding a new function to your cli. In my case, I use zsh, so I will add it to my ~/.zshrc file.

Code

tip

If you want to understand what is going on I recommend you use Explain Shell

update-branch() {
current_branch="$(git rev-parse --abbrev-ref HEAD)"
remote=$(git remote)
REPLY="Y"
if default_branch="$(git remote show "${remote}" | sed -n '/HEAD branch/s/.*: //p')"; then
vared -p "Rebase branch(${current_branch}) onto branch(${default_branch}) (Y/N)?: " -c REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
REPLY=""
git checkout "${default_branch}"
if git pull; then
git checkout "${current_branch}"
if git rebase "${default_branch}"; then
echo
else
vared -p "Unable to rebase without errors, abort (Y/N)?: " -c REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git rebase --abort
fi
fi
fi
fi
else
echo "Unable to pull latest changes check your auth probably."
fi
}
note

Thanks to making this script I learnt that vared exists for editing variables and prompting users.

Bonus code

Here is a quick script to allow quick switching and creation of branches.

checkout-branch() {
branch="$1"
source_branch="${2:-main}"
REPLY="Y"
if git rev-parse --verify "${branch}" > /dev/null 2>&1; then
git checkout "${branch}"
else
vared -p "Can't find Branch(${branch}) new branch (Y/N)?: " -c REPLY
if [[ $REPLY =~ ^[Yy]$ ]]; then
vared -p "What source branch (Y/N)?: " -c source_branch
git checkout "${source_branch}" && git checkout -b "${branch}"
fi
fi
}