Resetting a GitHub Personal Access Token on MacOS

Personal Access Token’s are used to grant access to one of your repos with specific permissions. They have an expiry, so when one expires you need to recreate a new one, details are here.

If you’re trying to push and your token has expired, you’ll see an error like this:

> git push
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/youraccount/yourrepo

The first time accessing a repo requiring authentication is simpler because it will prompt you for your id and password (or token). Once you’ve set one up though, it’s not as obvious how to reset a new token value, since the git cli only gives you the above error, it doesn’t prompt you to enter a new password/token.

To replace your cached credentials, use these steps (from here):

git config --global --unset credential.helper

git credential-osxkeychain erase
host=github.com
protocol=https

Press Return twice on the second step. Close your Terminal, open a new one, then:

git config --global user.name "userid"
git config --global user.email you@example.com

From your GitHub account, go to Settings from the top right icon, then Developer Settings, then Personal Access Tokens.

Next time you do a push, use the Personal Access Token value when prompted for your password.

Re-writing previous git commits to change committer author/email

If you’ve committed a number of git commits using a wrong user.name or user.email value, you can re-write previous commits with:

git filter-branch --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "Old Name" ];
        then
                GIT_COMMITTER_NAME="new-name";
                GIT_AUTHOR_NAME="new-name";
                GIT_COMMITTER_EMAIL="new-email";
                GIT_AUTHOR_EMAIL="new-email";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

Note that this will re-write all commits on the current branch matching the if condition which may or may not be what you’re looking for, so be careful. push the changes back to your remote origin as needed.

This is based on answers to this question here.

Simplest customization of MacOS zsh prompt with git branch name without escape codes

In my previous zsh configuration that I combined from multiple sources there’s an issue with backspace when moving back through a longer line. The cursor jumps up or down a line and behaves weirdly.

From some searches online, it appears I have an issue with the escape codes not starting and ending correctly. Rather than spending time debugging what’s going on, I simplified it to avoid using the escape codes using suggestions in this question:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

setopt PROMPT_SUBST
export PROMPT='%n %~ %F{green}$(parse_git_branch)%F{reset_color} > '

This approaches uses the built in color codes, e.g. $F{green} instead of using the escape codes that need to be correctly terminated.

Moving a git ‘master’ branch to ‘main’

By default, the git install on my MacOS 11.5 creates new projects with a ‘master’ branch, but GitHub which I push to for my remote projects has been creating new projects with a default branch of ‘main’ for some time now.

To move a master branch to main locally, set it as the default branch and then push to GitHub I use the following steps (taken from multiple places but mainly from this article):

Create a new main branch from master locally:

git branch -m master main

Add remote GitHub repo:

git remote add github https://your-repo-url

Pull remote changes from remote main branch

git pull github main

Set upstream branch to track main

git branch  --set-upstream-to github/main

Push local to github main

git push github main

If you have changes in the new remote project (like a readme) that don’t exist locally yet and you get the error: “fatal: refusing to merge unrelated histories” then pull with the allow unrelated histories option:

git pull github master --allow-unrelated-histories

Now:’git push’ and you should be good.

For my local development these steps are enough for what I need, but if you have a master in your remote repo too and want to delete it there are additional steps in the article linked above.