Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I push to GitHub under a different username?

Tags:

git

github

People also ask

Why are my commits linked to the wrong user?

GitHub uses the email address in the commit header to link the commit to a GitHub user. If your commits are being linked to another user, or not linked to a user at all, you may need to change your local Git configuration settings, add an email address to your account email settings, or do both.

How do I switch between users in git?

gitconfig file into the . git folder (and rename it to "config") and just change the lines you want to change (probably github. user and github. token) or you create a new file with just the two lines in it.

Should git username be same as GitHub?

No, your user.name does not matter.


This worked for me, it will prompt for username and password

git config --local credential.helper ""
git push origin master

If you use different windows user, your SSH key and git settings will be independent.

If this is not an option for you, then your friend should add your SSH key to her Github account.

Although, previous solution will keep you pushing as yourself, but it will allow you to push into her repo. If you don't want this and work in different folder on the same pc, you can setup username and email locally inside a folder with git by removing -g flag of the config command:

git config user.name her_username
git config user.email her_email

Alternatively, if you push over https protocol, Github will prompt for username/password every time (unless you use a password manager).


You can push with using different account. For example, if your account is A which is stored in .gitconfig and you want to use account B which is the owner of the repo you want to push.

Account B: B_user_name, B_password
Example of SSH link: https://github.com/B_user_name/project.git

The push with B account is:

$ git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git

To see the account in .gitconfig

  1. $git config --global --list
  2. $git config --global -e (to change account also)

I setup an ssh alias using a custom IdentityFile and rewrote the origin to use my custom me-github hostname.

#when prompted enter `id_rsa_user1` as filename
ssh-keygen -t rsa

# ~/.ssh/config
Host user1-github
HostName github.com
Port 22
User git
IdentityFile ~/.ssh/id_rsa_user1

#check original remote origin url
git remote -v
origin [email protected]:user1/my-repo.git

#change it to use your custom `user1-github` hostname
git remote rm origin
git remote add origin git@user1-github:user1/my-repo.git

Follow the following steps:

  1. You must understand that, you define author before commiting! the commits are already frozen: they have whatever name is set for their author and committer, and these cannot be changed.
# you can check what's currently:
git config user.name
git config user.email

git config user.name "your_github_username"
git config user.email "your_github_email"

# Again check what's currently:
git config user.name
git config user.email
  1. Check to whom your commit is tagged to?
git log
# once you're confirmed that it's tagged to you, then you should move to step 3

In case, the author is wrong then you can easily undo last commit without losing changes

Also, before moving to step3, don't forget to follow step one for sanity check.!

  1. give yourself a prompt to enter github_username and github_password
git config --local credential.helper ""
git push
# it will ask you to enter your github_username and github_password