270

A friend and myself are sharing my computer. I've made pushes to GitHub using the git bash shell on Windows 7. Now we're in a different project on that computer and I need her to push to her account. But it keeps trying to use my username and saying I don't have access to her repository:

$ git push her_github_repository our_branch
ERROR: Permission to her_username/repository.git denied to my_username.
fatal: The remote end hung up unexpectedly

IMPORTANT: Support for password authentication was removed on August 13, 2021.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
at.
  • 50,922
  • 104
  • 292
  • 461
  • Does this help https://help.github.com/articles/setting-your-email-in-git (use `git config --global user.name her_username`) before pushing – Prinzhorn Oct 27 '12 at 18:49
  • I see you switched to HTTPS urls so github always prompts for credentials. Just remember that the commits will still be recorded as done by the user in git config user.name and git config user.email regardless of who actually does the push. p.s. I don't think there's a history of who pushed what. – Jorge Orpinel Pérez Sep 15 '14 at 16:30
  • 46
    `git config --local credential.helper ""` may do the trick. – SOFe Oct 02 '17 at 15:15
  • I could not follow any of the solutions given here on my windows system. The steps given in this answer worked finally https://stackoverflow.com/a/15382950/351903 – Sandeepan Nath Dec 23 '17 at 18:53
  • @SOFe that's what I was looking for! Does this command effectively wipe some cached credentials? – CCJ Jun 24 '19 at 22:05
  • I don't think so. It just stops calling the global credential store. – SOFe Jun 25 '19 at 02:11
  • @SOFe awesome, thanks! Any way to access and alter the global credential store? it keeps asking me the password now.. – huskygrad Apr 15 '20 at 04:41
  • @Sanket I have been using SSH remotes for the past two years, and I am no longer using Windows, so I have no idea about those things anymore. – SOFe Apr 15 '20 at 04:47
  • 1
    Nothing above will work after August 2021 update – gaurav kumar May 14 '22 at 18:43

29 Answers29

368

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

git config --local credential.helper ""
git push origin master
s-hunter
  • 24,172
  • 16
  • 88
  • 130
  • 1
    Thanks, it worked for me i used bitbucket and i'd configure my username, but this solved the migration to aws code-commit – Alejandro Ruiz Oct 09 '18 at 23:20
  • I had `helper = cache --timeout=999999` set on my `.gitignore` and this worked. But any way to reset that without changing a local config I wonder? – Ciro Santilli OurBigBook.com Jan 07 '19 at 14:24
  • 6
    Better explain what it does here: It configures your local repo to ignore the (likely globally) configured `credential.helper`, e.g. the Windows credential store. This also means it asks for a username/password *each time* it needs it, as nothing is configured, So maybe you can also configure the `credential.helper` to cache instead (see @Ciro Santilli's answer) or use a different answer here. – rugk Jul 19 '20 at 18:14
  • 2
    Github these days responds with: "remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead." – Tails Aug 25 '21 at 10:39
132

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

RedBassett
  • 3,469
  • 3
  • 32
  • 56
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • 13
    As far as I remember, SSH keys have to be unique on Github, you can't have the same key attached to multiple accounts – Gareth Oct 27 '12 at 18:56
  • @Gareth Maybe, but you can set multiple SSH key for the same user in windows (and everywhere I know). – Simon Boudrias Oct 27 '12 at 18:57
  • can't have two keys, see my answer below. – chovy Feb 11 '15 at 07:46
  • 1
    For every projects : `git config --global user.name "Mona Lisa"` – w3spi Mar 30 '20 at 17:49
  • 4
    This just changes the username displayed. It does not change the user name logged be into. As stated in the other answers, a good way is to alter the remote url: `git push https://your_other_username_at_github@github.com/username/provided path.git/` where everything after the `@` is the default https url of the repo. – Cutton Eye Oct 20 '20 at 07:40
  • Unfortunately, not relevant anymore: `remote: Support for password authentication was removed on August 13, 2021.` – valk Jan 29 '23 at 08:50
111

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)
thotruong
  • 1,171
  • 1
  • 7
  • 5
  • 1
    Wow this is the answer!! I have set `user.name`, `user.email` in local config, I have cloned with user name, and not working; now the last answer works without touching keychain/whatever... in `Git 2.9.0 in Windows`. "at last but not least".. this is what it is called. Thank you. This answer deserves more upvote. – WesternGun Dec 04 '17 at 19:12
  • 4
    The only side effect, is that your password/username is recorded in the bash history.. But it works for me. – WesternGun Dec 04 '17 at 19:14
  • Thank you for this, this is exactly what I wanted to do. This pushes to master, though. How would I push to a branch besides master? – Steven Anderson Mar 27 '20 at 22:51
  • 18
    @WesternGun Do not provide password in the url if you want to prevent it from being recorded in bash history. Use `https://'B_user_name'@github.com/B_user_name/project.git`. This should prompt for the password of `B_user_name`. – subtleseeker Jul 08 '20 at 10:39
  • This is the best answer when you don't want to change your local git credentials. – Kellen Apr 06 '21 at 15:10
  • 5
    this answer no longer works: https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ – Selcuk Oct 01 '21 at 20:37
  • 4
    Support for password authentication was removed on August 13, 2021. :( – Jose R. Zapata Sep 20 '22 at 19:15
  • 1
    `remote: Support for password authentication was removed on August 13, 2021.` – Fisher Coder Oct 01 '22 at 23:51
36

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 git@github.com: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
chovy
  • 72,281
  • 52
  • 227
  • 295
  • 9
    Instead of removing then add, we can change the remote url by: `git remote set-url origin git@user1-github:user1/my-repo.git ` – ninhjs.dev May 16 '17 at 13:18
  • Nice! thanks, this finally worked :) when trying to set it up via git config, it did not, simply updating the remote with the alias did the job! – laka Aug 18 '23 at 11:40
31

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
  • When I ran `git config --local credential.helper ""` and then `git push` came to know VSCode had the credentials stored for a default push. – invinciblemuffi Jan 03 '21 at 17:19
29

If you use an SSH URL (git@github.com:username/projectname.git) rather than an HTTPS URL, you can temporarily specify a different SSH key using the $GIT_SSH_COMMAND environment variable:

$ GIT_SSH_COMMAND="ssh -i different_private_key" git push

As far as I can tell, with SSH URLs, GitHub doesn't care about user names, only about keys: if a user account has access to a repository, and that account has an SSH key (see the SSH keys page in the account settings), then if you use that SSH key to push to that repository, the push will be treated as coming from that user.

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • 1
    this did not work for me, interestingly, changing the `user.name` and `user.email` in `git config` will change the account that a commit is related to – Kevin Zhao Mar 26 '17 at 23:22
  • 2
    It worked for me. Just a small comment to make the answer more clear. "different_private_key" is a file on the hostA that contains the private key of the userB on behalf of which we want to perform "git push" on the hostA being userA. You have to create this file on the hostA (for example, as ~/.ssh/id_rsa_userB) and put there the contents of the file ~/.ssh/id_rsa from the hostB where userB exists. It even does not matter if the userB exists or does not exist on the hostA. – Alexander Samoylov Nov 30 '18 at 11:54
17

If after running git push Git asks for a password of user, but you would like to push as new_user, you may want to use git config remote.origin.url:

$ git push
user@my.host.com:either/branch/or/path's password:

At this point use ^C to quit git push and use following to push as new_user.

$ git config remote.origin.url
user@my.host.com:either/branch/or/path

$ git config remote.origin.url new_user@my.host.com:either/branch/or/path

$ git push
new_user@my.host.com:either/branch/or/path's password:
foobar
  • 133
  • 1
  • 7
Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
  • Hello, I tried this solution to push to atlassian stash. I get the error "/info/refs not found: did you run git update-server-info on the server?" I don´t think I can run commands on Stash. Do you have an Idea how to solve this? Thanks! – Zsolt Szilagyi Jul 16 '15 at 06:26
  • For GitHub, this would look like: `git config remote.origin.url https://different_user@github.com/different_user/repo.git` – NumesSanguis Jun 19 '20 at 08:03
15

It's simple while cloning please take the git URL with your username.While committing it will ask your new user password.

Eg:

git clone https://username@github.com/username/reponame.git

git clone https://username@bitbucket.org/username/reponame.git

  • 4
    This answer should have more upvotes at least. If you don't do as @Krishna Kavoor says, you will always have the issue, because although globally you have username1 and locally you have username2, git will try to use the global name. – Markon Dec 03 '17 at 15:04
  • 1
    It does not work. I used it and it say: `fatal: repo not found`. – WesternGun Dec 04 '17 at 19:06
  • 1
    It works always Since I have 3 different projects from 3 different clients.It works like a miracle.Please check your repo path is correct. – Krishnamoorthy Acharya Dec 05 '17 at 06:50
13

if this is your problem

remote: Permission to username1/repo.git denied to username2.
fatal: unable to access 'https://github.com/username1/repo.git/':
The requested URL returned error: 403

In addition to changing username and email from terminal using git config:

$ git config --global user.name "Bob"
$ git config --global user.email "bob@example.com"

you'll need to remove authorization info from Keychain. This solution took me several hours to figure out.I found that I also had certificate in my Keychain.

Open up Keychain access, click on All Items and search for git. Delete all keychain

Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23
11

As mentioned before, you can use

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

to manually set username and email for single repo, but you have to add this command:

git commit --amend --reset-author

if you have already tried to push before the change. Otherwise the changes doesn't appear in config file.

Martin819
  • 412
  • 2
  • 7
  • 26
  • 2
    This is not true. The `--reset-author` (with `--`...) will put the latest configured name/email on the last commit, and also the latest timestamp. It doesn't affect the changes in the config file. – Ramon Snir Feb 17 '17 at 22:42
  • @RamonSnir this is true but if you authored the commit before you changed the config you'll still get rejected so resetting the author may be part of fix – Braden Holt Jan 22 '21 at 20:14
  • I needed to include --not-edit to mine: `git commit --amend --reset-author --no-edit` – Phillip Havea Mar 11 '21 at 00:04
11

I have been using one machine to push code to two different GitHub accounts with different username. Assuming you already set up one account and want to add a new one:

  1. Generate new SSH key ssh-keygen -t rsa -C "myaddress@example.com"
  2. Save it, but remember not to override the existent one id_rsa. Give it a different name, e.g. id_rsa_another
  3. Copy the contents of the key to your GitHub account:

Settings -> SSH and GPG keys -> New SSH key -> Give a label and paste the key -> Add SSH key

  1. Add the key to the ssh agent: ssh-add ~/.ssh/id_rsa_another
  2. Setup a GitHub host: Create a config file with touch ~/.ssh/config and edit the file by providing configurations to your accounts:
#first account
Host github.com-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#another account
Host github.com-another
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_another

Now you should be able to push from different accounts depending on what key you add to the ssh agent, i.e. to use your first account, do ssh-add ~/.ssh/id_rsa.

You might also want to change your user email:

git config --global user.email "myaddress@example.com"

or clean out ssh keys in case of permission error when pusing code to one of the accounts:

ssh-add -D

Tomasz Bartkowiak
  • 12,154
  • 4
  • 57
  • 62
8

I like to do it like this the best.

git push https://username@github.com/username/repo

This will prompt for password, so you don't have to write it in plain text in the shell.

The Fool
  • 16,715
  • 5
  • 52
  • 86
7

I couldn't figure out how to have a 2nd github identity on the one machine (none of these answers worked for me for that), but I did figure out how to be able to push to multiple different github accounts as myself.

Push as same username, but to different github accounts

  1. Set up a 2nd SSH key (like so) for your 2nd github account

  2. Change between accounts thus :

Push with my new 2nd github account

ssh-add -D
ssh-add ~/.ssh/ssh_key_for_my_2nd_account
git push

Push with my main account

ssh-add -D
ssh-add ~/.ssh/id_rsa   
git push
kris
  • 11,868
  • 9
  • 88
  • 110
6

Go to Credential Manager Go to Windows Credentials Delete the entries under Generic Credentials Try connecting again.

Deepak
  • 61
  • 1
  • 1
  • Thanks. The path is `Control Panel\All Control Panel Items\Credential Manager` -> `Windows Credentials`. – Saïd Nov 21 '17 at 03:00
5

You can add a new remote URL for the other username using git remote add origin-username https://username@github.expedia.biz/repository_name.git

After this, if you'll push using git push -u origin-username master , this will prompt you for the password.

Raman Singh
  • 329
  • 7
  • 17
5
git add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all

After this push command, a username password prompt will be opened.

User
  • 1,460
  • 14
  • 11
3

If under Windows and user Git for Windows and the manager for managing the credentials (aka Git-Credential-Manager-for-Windows Link) the problem is that there is no easy way to switch amongst users when pushing to GitHub over https using OAuth tokens.

The reason is that the token is stored as:

  • Internet Address: git:https://github.com
  • Username: Personal Access Token
  • Password: OAuth_Token

Variations of the URL in Internet Address don't work, for example:

  • git:https://username@github.com
  • git:https://github.com/username
  • ...

The solution: namespaces. This is found in the details for the configuration of the Git-Credential-Manager-for-Windows:

Quoting from it:

namespace

Sets the namespace for stored credentials.

By default the GCM uses the 'git' namespace for all stored credentials, setting this configuration value allows for control of the namespace used globally, or per host.

git config --global credential.namespace name

Now, store your credential in the Windows Credential Manager as:

  • Internet Address: git.username:https://github.com
  • Username: Personal Access Token
  • Password: OAuth_Token

Notice that we have changed: git -> git.username (where you change username to your actual username or for the sake of it, to whatever you may want as unique identifier)

Now, inside the repository where you want to use the specific entry, execute:

git config credential.namespace git.username

(Again ... replace username with your desired value)

Your .git/config will now contain:

[credential]
    namespace = git.username

Et voilá! The right credential will be pulled from the Windows Credential Store.

This, of course, doesn't change which user/e-mail is pushing. For that you have to configure the usual user.name and user.email

mementum
  • 3,153
  • 13
  • 20
  • Thank you. This worked best for me (Windows 10). No temporary deletion of other git user accounts in Windows Credentials, no "hacks". I have one global/host git user for all repositories. Except just one of them requires different credentials. If that also fits your environment, just leave everything as it is in Windows credentials, go to your "special" repository and type `git config credential.namespace git.username` as mentioned (replacing username). If you push next time, git will ask you for the new credentials for username. – ford04 Feb 18 '19 at 18:40
3

In the git bash shell, navigate to your friend's repository and type the following:

git config --local credential.username "friend's_username"

Now, the credential store will use credentials that pertain to her username instead of yours.

myQwil
  • 442
  • 3
  • 11
2

git config user.name only changes the name I commit. I still cannot push. This is how I solved it, and I think is an easy way to me.

  1. Generate a SSH key under the user name you want to push on the computer you will use https://help.github.com/articles/connecting-to-github-with-ssh/

  2. Add this key to the github user account that you want to push to https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

  3. Choose to Clone with SSH

You can push in as this user to that repo now.

Yang Fang
  • 21
  • 1
2

This example writes the value johndoe@example.com to the configuration name user.email. It uses the --global flag so this value is set for the current operating system user.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
itsme
  • 31
  • 2
1

If you have https://desktop.github.com/
then you can go to Preferences (or Options) -> Accounts
and then sign out and sign in.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
1

this, should work: git push origin local-name:remote-name

Better, for GitLab I use a second "origin", say "origin2":

git remote add origin2 ...
then

git push origin2 master

The conventional (short) git push should work implicitly as with the 1st "origin"

tontonCD
  • 320
  • 2
  • 6
  • Doesn't that command only push the branch `local-name` to the name `remote-name` on `origin`? – Andrew Falanga Oct 19 '21 at 21:55
  • Yes exactly. By the way I'm afraid I did post this response to the wrong place (too mush opened tabs here). Try something like `push myname@repositoryname` (I'm not certain because I'm not on the right computer). Also, opening the computer session with a different User would make a different git.config (name, mail) to be used. – tontonCD Oct 22 '21 at 11:03
1

I just have included additional user on:

  • repo settings,
  • Manage access,
  • invite a collaborator

and it worked for me.

Dicast
  • 69
  • 7
1

Modify the file named config located inside the .git directory which is in the root folder of the project: [PROJECT_PATH]/.git/config

I added following section at the end of this file. After that I could push to the remote with my new name.

[user]
    name = Your Name
    email = MyEmailAddress@company.com
Farzan
  • 745
  • 10
  • 25
0

The userid where the commit happens is stored in the config file.

go to the top of the repository vi .git/config

change the url line listed after "[remote "origin"] to have the appropriate userid

wainCam
  • 11
  • 2
0

If you use ssh and get

Permission to some_username/repository.git denied to Alice_username

while you don't wanna push as Alice_username, make sure Alice_username doesn't have your computer's ssh key added to its github account.

I deleted my ssh key from alice's github account and the push worked.

Achraf Amil
  • 1,275
  • 16
  • 20
0

I had a similar problem. I have a github account for work and a private one. On my mac I mostly use the my work github. I was not able to convince git to push to my private repo via the terminal. But it worked when I used the github desktop.

(I know there must be a different way of doing it, but none of the above answers helped, so this was the way of least resistance.)

0

In case of you are using Public Key Authentication in SSH protocol, you will find neither of

  1. git config --local credential.helper ""
  2. git config --local user.name and git config --local user.email

works, you have to specify private key which owned by the user you want to play as, like

➜  Lang git:(main) ✗ git config --local -l | cat
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:xyz/1A.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
user.name=xyz
user.email=xyz@post.com
credential.helper=
➜  Lang git:(main) ✗ git push
ERROR: Permission to xyz/1A.git denied to abc.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
➜  Lang git:(main) ✗ git config --local core.sshcommand 'ssh -i ~/.ssh/xyz@post.com -F /dev/null'
➜  Lang git:(main) ✗ git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 30.42 KiB | 10.14 MiB/s, done.
Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:xyz/1A.git
   660593a..2972551  main -> main
➜  Lang git:(main) ✗ 
http8086
  • 1,306
  • 16
  • 37
0

Easy fix, you can edit the file at ~/.ssh/config

From:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/private_key

To:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/other_account_private_key

Alternate between the two when switching accounts. Also to avoid changing the global name and email for the git config very time you're switching you can just settle for some generic inputs, or try locally scoped config for that here.

Osoro
  • 382
  • 3
  • 8