Dev tools and workflow · Beginner · 16 min
How to Push to GitHub over SSH and Open a Pull Request
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Git 2.53 (Ubuntu package) · GitHub CLI 2.104 · GitHub
You will connect a local repository to a new GitHub repository over SSH, push your work, push a branch, open a pull request from the website and from the GitHub CLI, merge it, and bring the result back to your computer.
On this page
Until now your repository has lived on one computer. GitHub hosts a copy that other people can read, and that a team can review before it changes. The way work arrives is the pull request: you push a branch, ask for it to be merged into main, and others look at the change first.
This tutorial takes the notes repository from the earlier tutorials to GitHub. You push main, push a feature branch, open a pull request in the browser and with the GitHub CLI (gh), merge it, and sync your local copy. Authentication uses the SSH key from the first tutorial, so you never type a password.
Step 1 · Create an empty repository on GitHub
On github.com, choose the plus menu, then New repository. Give it a name such as notes-demo, choose Private so the practice work stays yours, and leave "Add a README file", ".gitignore" and "license" unticked. An empty repository has no commits, so your first push will not collide with a history GitHub made. Choose Create repository.
GitHub then shows the repository's addresses. Choose the SSH tab, and note the address, which has the form git@github.com:YOUR-USERNAME/notes-demo.git. You use it in the next step.
Step 2 · Connect your repository to it
A remote is a named address of another copy of the repository. By convention the main one is called origin. Replace YOUR-USERNAME with your GitHub username, and notes-demo with the name you chose:
cd ~/git-demo/notes
git remote add origin git@github.com:YOUR-USERNAME/notes-demo.git
git remote -vgit remote -v lists the address twice, once with (fetch) and once with (push). If you typed the address wrong, change it with git remote set-url origin <address>.
The git@ form is the SSH address. An https://github.com/... address also works, and then Git asks for a token instead of using your key. Stay with SSH for this series.
Step 3 · Check that SSH reaches GitHub
Before the first push, make sure your key is accepted. The first connection asks you to confirm GitHub's host key. Compare the fingerprint with the ones on GitHub's documentation page, SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU for ED25519, and answer yes if they match:
ssh -T git@github.comThe reply is Hi YOUR-USERNAME! You've successfully authenticated, but GitHub does not provide shell access. The command exits with status 1 anyway, and that is normal. If you see Permission denied (publickey), return to the last steps of the first tutorial: the key must exist, SSH must offer it for github.com, and its public half must be on your account.
Step 4 · Push main
Push your main branch and make the remote branch its upstream. After that, a plain git push and git pull know where to go:
cd ~/git-demo/notes
git push -u origin mainThe output counts the objects sent and ends with a line such as * [new branch] main -> main and branch 'main' set up to track 'origin/main'. Reload the repository page on GitHub and your file and commits are there.
Check it worked
git status
git branch -vv
git log --oneline --decorate -1git status says Your branch is up to date with 'origin/main'. git branch -vv shows [origin/main] after main, and the log line carries origin/main next to HEAD -> main. origin/main is your computer's record of where main was on the remote the last time you talked to it.
Step 5 · Push a branch for a change
Never commit to main directly when other people depend on it. Make a branch, commit the change, and push the branch. Because of the push.autoSetupRemote setting from the first tutorial, a plain git push creates the remote branch and links it:
git switch -c add-reading-list
cat > reading.txt <<'TXT'
The Pragmatic Programmer
Pro Git
TXT
git add reading.txt
git commit -m "Add a reading list"
git pushGit prints a * [new branch] add-reading-list -> add-reading-list line. In GitHub's web interface, the repository page now shows a yellow banner with a "Compare and pull request" button.
Check it worked
git branch -vvadd-reading-list is shown tracking origin/add-reading-list.
Step 6 · Open a pull request in the browser
Choose "Compare and pull request" on the banner, or open the Pull requests tab and choose New pull request. Check three things on the page:
- The base branch is
main, and the compare branch isadd-reading-list. The base is where the change will go. - The title describes the change in one line. GitHub fills it from your commit message.
- The files changed at the bottom are only the ones you meant to change.
Add a short description of what changed and why, then choose Create pull request. Reviewers read the description first, so say what a person who has not seen the code needs to know.
Step 7 · Open a pull request from the terminal
The GitHub CLI does the same from the terminal. On macOS, install it with Homebrew, then skip to the sign-in below:
brew install ghOn Ubuntu, the package in the archive is an old release, so add GitHub's own apt repository, as the GitHub CLI installation guide for Debian and Ubuntu describes. These commands add GitHub's signing key and package source, then install gh:
sudo apt install -y wget
sudo mkdir -p -m 755 /etc/apt/keyrings
out=$(mktemp)
wget -nv -O"$out" https://cli.github.com/packages/githubcli-archive-keyring.gpg
sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg < "$out" > /dev/null
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
sudo mkdir -p -m 755 /etc/apt/sources.list.d
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install -y gh
gh --versiongh --version prints gh version 2.104.0 or a later release. Because the package comes from GitHub's repository, sudo apt update && sudo apt upgrade keeps it current.
Sign in once. The command asks questions: choose GitHub.com, choose SSH as the protocol for Git operations, accept the offer to upload an SSH key or pick the one you already have, and finish in the browser when it shows a one-time code. The token gh stores is kept in your system's credential store when it has one:
gh auth login
gh auth statusgh auth status shows your account and the protocol. With that done, open a pull request for the branch you pushed. --title and --body skip the prompts, and --base names where the change should go:
gh pr create --base main --title "Add a reading list" --body "Adds reading.txt with two books to read next."The command prints the URL of the new pull request. gh pr create --fill fills the title and body from your commits, and gh pr create --draft opens it as a draft that nobody can merge yet. Open the page, or ask gh to:
gh pr view --web
gh pr statusIf you opened the pull request in the browser in the previous step, gh pr create reports that a pull request for the branch already exists. Use the one you have.
Step 8 · Review and merge
On the pull request page, the Files changed tab is where a reviewer reads the diff and leaves comments on lines. When the review is done, merge from the page. The green button offers three styles:
- Create a merge commit keeps every commit and adds a merge commit, exactly like the merges in the branches tutorial.
- Squash and merge combines all the branch's commits into a single commit on
main. - Rebase and merge replays the commits on top of
mainwithout a merge commit.
Your repository's settings can turn any of them off. For this practice, choose Create a merge commit, and then Delete branch when GitHub offers it. From the terminal, the same is gh pr merge --merge --delete-branch, and --squash and --rebase pick the other styles. Warning: merging changes main for everyone who uses the repository, so do not run it on a pull request you have not read.
Step 9 · Bring the merge back to your computer
The merge happened on GitHub, so your local main does not have it. Switch to main and pull. Your pull.ff only setting makes the pull stop with an error instead of making a surprise merge commit, and here a fast-forward is all that is needed:
git switch main
git pull
git log --oneline --graph --decorate -4The log shows the merge commit from GitHub, and main and origin/main on that commit. Your local copy of the branch is no longer needed. If you merged with gh pr merge --delete-branch, gh has already deleted the local branch and switched you to main, so skip the git branch -d line below. Otherwise delete it, and tidy the record of remote branches that GitHub deleted:
git branch -d add-reading-list
git fetch --prune
git branch -agit branch -d works because the branch's commits are in main. After a squash merge, Git cannot see that, and refuses with "not fully merged". If you are sure the pull request was merged, use git branch -D for that branch. git fetch --prune removes origin/add-reading-list from the list, because the remote branch is gone. git branch -a lists local and remote-tracking branches. It now shows main and remotes/origin/main, and remotes/origin/HEAD as well if you made your copy with git clone.
Check it worked
test -f reading.txt && echo "reading.txt is on main"
git statusThe file added in the pull request is in your working folder, and the status is clean.
Troubleshooting
Permission denied (publickey)on a push means GitHub does not accept your key. Runssh -T git@github.comand compare with the first tutorial.Repository not foundmeans the address is wrong, the repository is private and your key belongs to another account, or you lack access. Checkgit remote -vagainst the address on the repository page.! [rejected] main -> main (fetch first)ornon-fast-forwardmeans the remote has commits you do not have. Rungit pull, deal with any conflicts as in the conflict tutorial, and push again. Do not force-push to fix it.fatal: The current branch X has no upstream branchmeanspush.autoSetupRemoteis not set. Rungit push -u origin Xonce, or set the option as in the first tutorial.gh: command not foundafter the install means the apt repository step did not finish. Run the commands again and read any error fromapt update.
Step 10 · Clean up
Remove the practice repository from your computer, along with the other demo folders from this series. Warning: this deletes ~/git-demo, so skip it if you want to keep working on the notes:
cd ~
rm -rf ~/git-demoOn GitHub, delete the practice repository if you do not want it: Settings, then Danger Zone, then Delete this repository. Delete the SSH key from your account's SSH keys page if you made it only for this series, and remove it from your computer with rm ~/.ssh/id_ed25519_github ~/.ssh/id_ed25519_github.pub. Remove the Host github.com entry that the first tutorial added to ~/.ssh/config. To remove gh and its sign-in, run gh auth logout and sudo apt remove gh.
Next steps
- Keep passwords and tokens out of what you push: keep secrets out of Git.
- Use the same kind of key to reach your servers: log in to a Linux server with SSH keys.
- Run tests on every push: your first GitHub Actions workflow.
Related tutorials
- How to Install and Configure GitYou will install Git 2.53 from Ubuntu 26.04's own packages, set your name and email, make main the default branch, choose an editor and a few safe defaults, use a different email for work projects, and create an SSH key to use with GitHub.
- How to Keep Passwords and API Keys Out of GitYou will keep secrets in a git-ignored .env file, commit a safe template instead, stop a commit that contains a key with a pre-commit hook, and learn what to do when a secret has already been committed.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials