Category: Technology

  • How to Set Up SSH Keys for GitHub on a Mac (Fix “Authentication Failed” on git push)

    Life involves a lot of problem solving. Today, my first one was this: I had forgotten the username and password for one of my GitHub repos, and my current Mac had no SSH keys set up. I don’t regularly work on this repo (an Android app), and I’ve changed devices since I was last active on this project.

    Tested on: a Mac running a macOS version Tahoe 26.6.2 using an ed25519 key.

    Quick answer: how to fix “Authentication failed” on git push by using SSH keys

    A TL;DR for fellow impatient people. 🤪

    If git push fails with “Authentication failed” and your Mac has no SSH keys, create an ed25519 key with ssh-keygen, add it to the SSH agent, and add the public key to your GitHub account under Settings → SSH and GPG keys. Then, remember to change your repo’s remote URL from HTTPS to SSH with git remote set-url!

    Step-by-step quick guide:

    1. Check for existing SSH keys with ls -al ~/.ssh.
    2. Create a new key with ssh-keygen -t ed25519 -C "[email protected]".
    3. Start the SSH agent with eval "$(ssh-agent -s)".
    4. Create ~/.ssh/config so macOS loads your key automatically.
    5. Add the key to the agent with ssh-add --apple-use-keychain ~/.ssh/id_ed25519.
    6. Copy your public key with pbcopy < ~/.ssh/id_ed25519.pub.
    7. Paste the public key into GitHub (Settings → SSH and GPG keys).
    8. Test the connection with ssh -T [email protected].
    9. Switch your repo’s remote to the SSH URL with git remote set-url origin [email protected]:USERNAME/REPO.git.

    And for those who want to dive in to more details, read on~

    The problem: git push says “Repository not found” and “Authentication failed”

    I tried to push to my repo, and Git asked for a username and password I no longer remembered. Entering anything failed with the error below.

    me@my-MacBook-Pro My-App % git push
    Username for 'https://github.com':
    Password for 'https://github.com':
    remote: Repository not found.
    fatal: Authentication failed for 'https://github.com/catzie/My-App.git/'

    Since I had no SSH keys on this device, it was time to set them up.

    Step-by-step: set up SSH keys for GitHub on a Mac

    Step 1: Check for existing SSH keys

    First, I checked whether any keys already existed on my Mac:

    ls -al ~/.ssh

    It found nothing, so I went ahead and created one.

    Step 2: Generate a new ed25519 SSH key

    I created a new SSH key with this command (replace the email with your own):

    ssh-keygen -t ed25519 -C "[email protected]"

    Here is the response I got from the terminal. It asks where to save the key and for an optional passphrase (you can leave it empty for no passphrase).

    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/Users/me/.ssh/id_ed25519):
    Created directory '/Users/me/.ssh'.
    Enter passphrase for "/Users/me/.ssh/id_ed25519" (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/me/.ssh/id_ed25519
    Your public key has been saved in /Users/me/.ssh/id_ed25519.pub
    The key fingerprint is:
    SHA256:gca01y4Nhsp3ZhFrUnY8ty6VPQiNjBEIt7IVbnFqdx4 [email protected]
    The key's randomart image is:
    +--[ED25519 256]--+
    |    ..=.B*.o     |
    |     =.%.=* o    |
    |    . # X E+ =   |
    |   . O * O .= o  |
    |    + . S +o   . |
    |     . + .. .    |
    |           .     |
    |                 |
    |                 |
    +----[SHA256]-----+

    Step 3: Start the SSH agent

    Next, I started the SSH agent:

    eval "$(ssh-agent -s)"

    This showed me Agent pid 1234. The number at the end will vary.

    Step 4: Create the SSH config file (macOS newer than Sierra 10.12.2)

    Since I use a macOS version newer than Sierra 10.12.2, I had to edit an SSH config file. I tried to open it first:

    open ~/.ssh/config
    The file /Users/me/.ssh/config does not exist.

    As you can see, the terminal told me the file doesn’t exist yet. I habitually use vim to quickly edit text files in the terminal, and to create nonexistent ones:

    vim ~/.ssh/config

    This opens a blank text file named “config” located at ~/.ssh/.

    Terminal showing vim opened on a new, blank ~/.ssh/config file

    I pressed the i key to enter vim’s INSERT mode, and then pasted the following text:

    Host github.com
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519
    vim in INSERT mode with the GitHub SSH config pasted into ~/.ssh/config
    Trivia: what does the ~ in ~/.ssh/id_ed25519 mean?

    In Mac and Linux terminals, the ~ symbol automatically translates to your user’s home directory, which on a Mac is usually /Users/YourUsername. If your Mac username is me, the terminal reads ~/.ssh/id_ed25519 and expands it to /Users/me/.ssh/id_ed25519 behind the scenes.

    Note: if you chose not to use a passphrase, you may omit the UseKeychain line from the config.

    If you see a “Bad configuration option: usekeychain” error, add an IgnoreUnknown UseKeychain line to the same Host github.com section of the config, above the UseKeychain line:

    Host github.com
    IgnoreUnknown UseKeychain
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519

    To save the config file and exit vim, I pressed the Escape key, typed :wq!, and pressed Enter.

    Step 5: Add your SSH key to the agent

    Then I ran this command:

    ssh-add --apple-use-keychain ~/.ssh/id_ed25519

    Note: omit --apple-use-keychain if you didn’t set a passphrase.

    The terminal responded with:

    Identity added: /Users/me/.ssh/id_ed25519 ([email protected])

    Step 6: Copy your public key

    Now it’s time to add the key to my GitHub account! I copied the contents of my public key file (~/.ssh/id_ed25519.pub) to my clipboard with this command:

    pbcopy < ~/.ssh/id_ed25519.pub

    Step 7: Add the public key to your GitHub account

    In my web browser, I went to github.com and navigated to Settings → SSH and GPG keys. I entered the title I wanted to label this key with, and selected Authentication Key as the key type. For the key itself, paste the contents of the local ~/.ssh/id_ed25519.pub file from your machine.

    GitHub Settings page for adding a new SSH key, with title, key type, and key fields

    Step 8: Test your SSH connection to GitHub

    After setting up the key with GitHub, I tested the SSH connection:

    ssh -T [email protected]

    It worked! The screenshot below shows the response I got. Note that every user connecting to GitHub.com via SSH using ED25519 sees that exact same fingerprint: SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.

    Terminal output of ssh -T git@github.com showing the GitHub ED25519 fingerprint

    Step 9: Switch your Git remote from HTTPS to SSH

    Time to test on the real thing: pushing my new Android app commits. But Git still prompted me for a username and password!

    This is because my Git remotes were still pointing to HTTPS URLs instead of SSH. I confirmed it with:

    git remote -v
    origin  https://github.com/catzie/My-App.git (fetch)
    origin  https://github.com/catzie/My-App.git (push)

    I visited my repo page on GitHub to get the SSH URL, which looks like [email protected]:catzie/My-App.git, and then updated the remote URL:

    git remote set-url origin [email protected]:catzie/My-App.git

    Running git remote -v again showed the remotes now use SSH:

    git remote -v
    origin  [email protected]:catzie/My-App.git (fetch)
    origin  [email protected]:catzie/My-App.git (push)

    Finally, I ran git push again within my Android Studio project and it successfully did as told. 😎 This is because the git remote is now pointing to the SSH URL, and should authenticate with the SSH key instead of prompting me again for username and password (which I already remembered! But not willing to type over and over again!)

    Common problems and fixes

    ProblemCauseFix
    git push shows “Repository not found” and “Authentication failed”Forgotten GitHub credentials and no SSH keys set up on this deviceFollow steps 1 to 9 above to set up an SSH key
    git push still asks for a username and password after adding the SSH keyThe Git remote still points to an HTTPS URLRun git remote set-url origin [email protected]:USERNAME/REPO.git (step 9)
    “Bad configuration option: usekeychain”The UseKeychain option isn’t recognizedAdd IgnoreUnknown UseKeychain to the Host github.com section, above the UseKeychain line (step 4)
    open ~/.ssh/config says the file does not existThe config file hasn’t been created yetCreate it with vim ~/.ssh/config (step 4)
    No passphrase set on the keyThe keychain options are only needed for passphrasesOmit the UseKeychain config line and the --apple-use-keychain flag

    Frequently asked questions

    Why does git push still ask for my username and password after I set up an SSH key?

    Your Git remote is probably still using an HTTPS URL. Run git remote -v to check. If it shows https://github.com/..., switch it to the SSH URL with git remote set-url origin [email protected]:USERNAME/REPO.git.

    How do I check if I already have SSH keys on my Mac?

    Run ls -al ~/.ssh in the terminal. On my Mac it found nothing, which is how I knew I needed to create a new key.

    Do I need a passphrase for my GitHub SSH key?

    The ssh-keygen prompt lets you leave the passphrase empty. If you choose no passphrase, you can omit the UseKeychain line from your ~/.ssh/config and the --apple-use-keychain flag from ssh-add.

    How do I test my SSH connection to GitHub?

    Run ssh -T [email protected] after adding your public key to your GitHub account. Every user connecting to GitHub.com over SSH with ED25519 sees the same fingerprint, SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.

    How do I fix the “Bad configuration option: usekeychain” error?

    Add the line IgnoreUnknown UseKeychain to the Host github.com section of ~/.ssh/config, above the UseKeychain line.

    Which macOS versions need the SSH config file?

    I needed it because I use a macOS version newer than Sierra 10.12.2. The config file tells SSH to add your key to the agent automatically, to use the keychain, and which identity file to use for github.com.

    Command cheat sheet

    All the commands from this guide, in order. Replace the email, username, and repo name with your own.

    # 1. Check for existing keys
    ls -al ~/.ssh
    
    # 2. Generate a new ed25519 key
    ssh-keygen -t ed25519 -C "[email protected]"
    
    # 3. Start the SSH agent
    eval "$(ssh-agent -s)"
    
    # 4. Create the SSH config file
    vim ~/.ssh/config
    
    # 5. Add the key to the agent (omit --apple-use-keychain if no passphrase)
    ssh-add --apple-use-keychain ~/.ssh/id_ed25519
    
    # 6. Copy the public key, then paste it into GitHub
    pbcopy < ~/.ssh/id_ed25519.pub
    
    # 7. Test the connection
    ssh -T [email protected]
    
    # 8. Switch the remote from HTTPS to SSH
    git remote -v
    git remote set-url origin [email protected]:USERNAME/REPO.git
  • Freshened up this site…

    In response to the recent PHP vulnerability issue, I spent some time here in this blog site to upgrade my WordPress theme, plugins, WordPress installation itself, and finally, the PHP version.

    One minor issue was encountered after upgrading plugins — Head, Footer and Page Injection plugin began showing parts of some code that I inserted, as plain text in my blog site!! I noticed that the plugin had a notice stating that “php is not allowed in your code (disabled by your theme or a plugin)”. The fix that worked was to head over to the plugin’s settings page (Settings -> Head and Footer) and then go to the ‘Advanced’ tab, and then toggle on the setting labeled as “Enable PHP execution”.

    The PHP version I was using was quite outdated T_T and so upgrading to version 8.0 broke my blog site with the error “there has been a critical error on this website.” Fortunately, the thing that I deactivated first was the exact cause of error: my old, outdated theme! It was “Sparkling” theme, which I hadn’t touched for years. I changed into the Twenty Twenty-Five theme for a fresh look and updated WP code. Thank you, Colorlib for the Sparkling theme — it served me well for several years!

    I feel good after taking the time to update this site. I’ve had this for over a decade now.

  • Installing git on a macbook

    As I was setting up a new Android Studio installation in a reformatted Macbook, I ran into this issue:
    Git is not installed
    xcode-select: note: No developer tools were found, requesting install. If developer tools are located at a non-default location on disk, use sudo xcode-select --switch path/to/Xcode.app to specify the Xcode that you wish to use for command line developer tools, and cancel the installation dialog. See man xcode-select for more details.

    <!–more–>

    To verify the lack of git in the machine, I ran git --version command for which I got a xcode-select: note: No developer tools were found, requesting install.

    To install git, I ran the following command in my Terminal:
    xcode-select --install

    and got a response:
    xcode-select: note: install requested for command line developer tools

    I initially thought that the git installation wasn’t working, until I finally noticed that there was a pop-up asking for confirmation! I confirmed the installation and right now the pop-up window looks like this as it installs git on my machine.

    As I waited for the installation to complete I asked myself why I write about this. Will it be helpful to anyone? Will anyone even read this page in the future, at the age when AI is most people’s first source of information instead of visiting web pages one by one?

    The answer I had was: Yes, this will surely be helpful to at least one person — me! The reason why I blog about challenges I encounter to begin with (since over a decade ago) is to help myself remember in the future. But, why? It’s because it supports my terrible memory.

    After a few minutes, “the software was installed”:

    I ran git --version again and finally got this:
    git version 2.50.1 (Apple Git-155)

  • Spoofing Battery and Forcing Sleep (Android Development)

    I wanted to do something with my Android phone using ADB commands, while keeping it plugged in to run the said commands. However, the thing I wanted to test was related to the idle (dozing or asleep) mode of Android devices. The workaround was to spoof the battery state to bypass Android’s charging restrictions.

    Spoofing Battery Charging Status and Forcing Android Device to Sleep (Idle Mode)

    • Turn off the device screen using the physical power button.
    • Trick Android into thinking the device is running on battery power: adb shell dumpsys battery unplug
    • Force the device to step into deep Doze mode: adb shell dumpsys deviceidle force-idle
    • Verify the device is successfully spoofed and asleep: adb shell dumpsys deviceidle get deep (This should return IDLE).

    Can also do:
    adb shell dumpsys battery unplug && adb shell dumpsys deviceidle force-idle && adb shell dumpsys deviceidle get deep

    Device Cleanup for Spoofed Battery and Forced Sleep (Crucial!)

    1. Once testing is done, you must tell the OS to start reading the actual battery state again, otherwise it won’t charge properly: adb shell dumpsys battery reset
    2. Bring the device out of idle: adb shell dumpsys deviceidle unforce

    Can also do:
    adb shell dumpsys battery reset && adb shell dumpsys deviceidle unforce && adb shell dumpsys deviceidle get deep

  • Shorter ADB commands for Android Studio Emulator Device targeting

    To target my specific emulator devices on Android Studio with my ADB (Android Debug Bridge) commands, I usually need to type long ones, like in the commands below where i wanna list the contents of a directory:

    adb -s emulator-5554 ls /sdcard/Android/sub/directory/
    adb -s emulator-5556 ls /sdcard/Android/sub/directory/

    When I am working on a new feature or a bug-fix ticket, I try to avoid typing long commands and args over and over again, especially args that are as error-prone as emulator-5554.

    How to shorten the ADB commands that target emulators (Mac OS):

    Open your Zsh config file with an editor. Mine by habit is Vim. Like so:
    vim ~/.zshrc

    Use your arrow keys to go to the very bottom of the file and copy-paste below exact code:

    adbs() {
      port=$1
      shift
      adb -s "emulator-55$port" "$@"
    }


    What it does is creates a new command adbs. I think the addition of s at the end is appropriate because the flag we are trying to work with is the -s flag which stands for “Serial”, for identifying specific emulators.

    The way the new command works is,

    • the $1 captures the first argument you type after adbs and saves it — for example, the 54 in adbs 54.
    • The shift bit removes the first argument from the front so that the actual command to be run will NOT be adb -s "emulator-5554" 54 logcat (wrong!)
    • Finally, the last line adb -s "emulator-55$port" "$@" runs the correct command, inserting the first argument (54 in our example) where $port is, followed by "$@" which grabs the rest of the command after the 54

    Save the changes to the config file and then exit the editor. In Vim I press Esc key, then type :wq! and hit Enter. IIRC my partner questions something about my :wq! usage, but what he points out, I can’t remember.

    After saving the config file, run the following command to make the new shortcut work immediately:

    source ~/.zshrc

    Usage Example:

    Instead of typing the long version:
    adb -s emulator-5554 ls /sdcard/Android/sub/directory/

    We can just type:
    adb -s 54 ls /sdcard/Android/sub/directory/

    Extra: to know what serial numbers of running devices you got, run below command

    adb devices

    Random thoughts: I am grateful to my very first team lead as a software developer (Peng) because he’s the one who inspired me to become comfortable with command line interfaces. Remembering how much I grew as a dev in that team due to high-traffic production databases that we were managing and debugging in realtime. Oh, the source command on the other hand, was something I learned from my current team lead, Maru. I like how much I am growing in this current team, not only as a dev, but also as a person.

    Credit goes to Google Gemini for the above adbs custom command. I had no idea how to write that until today. hehe

  • A random dev brain dump

    I don’t need a reason to randomly post a brain dump on my blog, no?

    • If an Android dev wishes to implement a precise “notification ID” that they want to be able to precisely cancel later (imagine that it plays an annoying alarm tone!) they can go about it either by
      • Using a unique ID tied to that notification’s base entity (e.g., an app’s incoming message) so that NOTIFS WILL STACK, or
      • Using a unique, dedicated ID that is defined as a constant and is defined without ties to the notification’s base entity so that ONLY THE LAST NOTIF WILL BE SHOWN
    • I gotta update my site’s, something..
    • If an Android dev’s soft keyboard covers a text input field when it’s displayed, particularly in Android 15 devices, they may wanna try to add an imePadding modifier to the root element (e.g., Box, Column) because this modifier will take care of spacings whenever the keyboard appears, avoiding issues such as covering the text input field.
  • Fix “Error loading build artifacts” error (Android Studio after upgrade)

    Quick Guide Summary:

    • Fixes the “Error loading build artifacts” (redirect.txt) error when running an app on an emulator after upgrading Android Studio from Ladybug to Otter.
    • Run the AGP Upgrade Assistant to bump Gradle 8.10.2 → 8.13 and AGP 8.8.1 → 8.13.2 in gradle-wrapper.properties and libs.versions.toml.
    • Remove and re-add “Gradle-aware Make” under Edit Configurations → Before Launch, leaving the task field empty.

    Problem:

    After upgrading Android Studio from “Ladybug” to “Otter” I started getting the following error message when I run my app on my emulated test device:

    "Error loading build artifacts from: .../myProjectRootDir/app/build/intermediates/apk_ide_redirect_file/productionDebug/createProductionDebugApkListingFileRedirect/redirect.txt"

    Solution:

    First, I ran the AGP Upgrade Assistant, which updated 2 files:

    • .../myProjectRootDir/gradle/wrapper/gradle-wrapper.properties
      Distribution URL has been upgraded from distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip to distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
    • .../myProjectRootDir/gradle/libs.versions.toml
      AGP version updated from agp = '8.8.1' to agp = '8.13.2'

    And then, applied a “Run Configuration” Fix:

    1. Click the Edit Configurations dropdown (next to the “Play” button).
    2. Select your app configuration on the left.
    3. Scroll to the “Before Launch” section at the bottom.
    4. Remove “Gradle-aware Make” (select it and click the minus – button).
    5. Re-add it: Click +, select Gradle-aware Make, and leave the task field empty. Click OK.
    6. Click Apply -> OK.
    7. Run the app.

    Additional things to try if above suggestions weren’t sufficient to fix the issue:

    • Invalidate caches
    • Clean the build before re-running
    • Sync Gradle files