Eternity Eternity - 3 months ago 20
Git Question

Share project on GitHub using alternate account

I'm trying to share a project on GitHub and want to use IntelliJ IDEA's built-in "Share project on GitHub" option. When I click on it, I am asked to log in to GitHub, which I do successfully, and then choose the files I want to commit so that they can be pushed to the repository.

The problem is that, while this does indeed create an empty repository on GitHub, it doesn't push the initial commit, and instead throws an error at me:

Successfully created project 'project' on GitHub, but initial push failed: unable to access 'https://github.com/alt/project.git/': The requested URL returned error: 403


I know the reason it does this is because I set up git to use my main account, and I'm trying to push to the repository which is on my alt account. I set up an alt account for my own reasons. After this failed, I tried to do it using git bash; I generated a new ssh keypair for the alt account and added it to the SSH keys in the alt account on GitHub. I also created a config in my
~/.ssh/
folder:

#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

#Alt GitHub
Host github-alt
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_alt


And also set the user.email and user.name to the email and name of my alt account. After doing all of this, I then tried to add a new remote and push it using git bash:

git remote add origin git@github-alt:alt/project.git
git push -u origin master


Which does actually push the commit to the repository... as my main account, not my alt account.

What am I doing wrong here? I want to share a project on GitHub on my alt account, and push commits to it using the same alt account.

Answer

The command git remote add origin git@github-alt:alt/project.git && git push -u origin master pushes code to repository associated with alt account.

But this doesn't modify author of existing commits. The commits made so far has main username associated. Changing username in entire commit history requires git history rewrite.

Example script:

#!/bin/sh
git filter-branch --env-filter '

OLD_EMAIL="main@sample.com"
NEW_NAME="alt"
NEW_EMAIL="alt@sample.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Do a force push after verifying git history. git push --force --tags origin 'refs/heads/*'

Comments