Home > Net >  How can I `git push` to GitHub on Ubuntu using a single command, without having to type in my email
How can I `git push` to GitHub on Ubuntu using a single command, without having to type in my email

Time:11-10

I want to git push on Ubuntu via a single command, such as:

echo -e "email\ntoken" | git push origin branchName

git push origin branchName && email && token 

But after the command I have to put in my email:

enter image description here

CodePudding user response:

How to use ssh keys to easily push to / pull from GitHub

You need to:

  1. Configure your remote to use the ssh version of the GitHub repo address instead of the http version.
  2. Generate a public/private ssh key pair, and add the public key to your GitHub account manually via your web browser.

Details

  1. Configure your remote to use the ssh version of the GitHub repo address instead of the http version. Ex: For this repo of mine: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world, use this ssh URL: [email protected]:ElectricRCAircraftGuy/eRCaGuy_hello_world.git instead of this HTTPS one: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world.git:

    # View your current remote servers and their URLs
    git remote -v
    
    # Set your `origin` remote server to use the ssh URL instead
    # of the HTTPS one
    git remote set-url origin https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world.git
    
  2. Generate a public/private ssh key pair, and add the public key to your GitHub account manually via your web browser. See my full notes on ssh stuff here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh

    # generate a public/private ssh key pair
    ssh-keygen -t ed25519 -C "[email protected]"
    
    # Ensure the ssh-agent is running (this starts the `ssh-agent`)
    eval "$(ssh-agent -s)"
    
    # Add your private key to it; update the path to your private key below, as
    # required, based on what path you interactively selected above when
    # generating the key
    ssh-add ~/.ssh/id_ed25519
    
    # Verify what keys have been added to the ssh-agent by listing
    # (`-l`) currently-added keys. 
    # A. If you see "Could not open a connection to your authentication agent.",
    # it means the `ssh-agent` has not been started yet, so you must start it
    # with `eval "$(ssh-agent -s)"`. 
    # B. If you see "The agent has no identities.", it means the ssh-agent is
    # running but you haven't added any ssh keys to it, so run `ssh-add
    # path/to/private_key` to add a key to the agent.
    ssh-add -l
    

    Now log into github in a web browser and click on your profile image --> Settings --> SSH and GPG keys (on left) --> New SSH key --> copy and paste the contents of your .pub key file (ex: run cat ~/.ssh/id_ed25519.pub on your Ubuntu machine to read the public key--adjust that path as necessary if you used a different file name) into GitHub here --> click "Add SSH key".

    Now, whenever you type git push it automatically works, using your ssh key.

References

  1. My full ssh notes: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh

CodePudding user response:

You can provide the username once as part of the https git remote address.

First run git remote -vv to get the full, current remote URL.

Then to change your existing remote, you can do a command like:

git remote set-url origin https://[email protected]/yourname/yourrepo.git

where the new part is yourname@ (substitute your github user name) and the rest of the URL should be the same as shown in git remote -vv

  • Related