ENG | Uploading project to GitHub
A step-by-step guide on how to initialize a Git repository, add files, commit changes, and push your project to GitHub using the command line interface.
Motivation
If you’re a developer working on a project and need to store your code on a remote repository, GitHub is a popular choice. In this post, we’ll walk through the process of initializing a Git repository, adding files, committing changes, and pushing your project to GitHub using the command line interface.
Steps
- Create a new repository on Github (Repositories -> New) and select better name than
test-repo
- Possibly add README.md and license.
- Initialize repository
1 2
$ git init Initialized empty Git repository in /home/pavel/projects/2023-05-15-test/.git/
- Check status
1 2 3 4 5 6 7 8 9 10 11
$ git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) test/ junk/ nothing added to commit but untracked files present (use "git add" to track)
- Add files (well, directory inside may and may not be a good idea)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ git add test* $ git status On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test/.gitignore new file: test/CMakeLists.txt new file: test/README.md new file: test/main.cpp Untracked files: (use "git add <file>..." to include in what will be committed) junk/
- Commit changes
1 2 3 4 5 6 7
$ git commit -m "initial commit" [main (root-commit) 369bcaf] initial commit 4 files changed, 502 insertions(+) create mode 100644 test/.gitignore create mode 100644 test/CMakeLists.txt create mode 100644 test/README.md create mode 100644 test/main.cpp
- Verify
1 2 3 4 5 6
$ git log commit 369bcaf7efc01b3640e09a713a8189db267e8fae (HEAD -> main) Author: Pavel Perina <[email protected]> Date: Mon May 15 22:09:23 2023 +0100 initial commit
1 2 3 4 5 6 7
$ git status On branch main Untracked files: (use "git add <file>..." to include in what will be committed) junk/ nothing added to commit but untracked files present (use "git add" to track)
- Add remote repository
1
$ git remote add origin [email protected]:pavel-perina/test-repo
- Push to github
1 2 3 4 5 6 7 8 9 10 11
$git push -u -f origin main The authenticity of host 'github.com (140.82.121.3)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. [email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Ok. Maybe I don’t have a SSH key on github. Let’s solve it:
There is an article about generating SSH keys which has also a paragraph dedicated to GitHub access troubleshooting.
- Go to github
- Click on profile icon
- Go to Settings (close to bottom)
- Go to SSH and GPG keys
- Click on New SSH key link
- Fill in some title (My Linux pub key)
- Go to
~/.ssh
on Linux box - Find something suitable, such as
id_ecdsa.pub
- Copy content to clipboard and paste content to github page
1 2 3 4 5
$ cd ~/.ssh $ ls ... $ cat id_ecdsa.pub ecdsa-sha2-nistp256 AAAAE....efPg= pavel@...
- Try again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$ cd ~/projects/2023-05-15-test/ $ git push fatal: The current branch main has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin main To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'. $ git push -u -f origin main Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 4 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (9/9), 4.66 KiB | 1.16 MiB/s, done. Total 9 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) To github.com:pavel-perina/test-repo + 4cde543...369bcaf main -> main (forced update) branch 'main' set up to track 'origin/main'.
Troubleshooting
Debug connection
1
GIT_TRACE=1 GIT_SSH_COMMAND="ssh -v" git push
Can’t authenticate during push: remote repository set HTTPS
If you pull your repository using HTTPS and you want to push changes, you may need to change origin from HTTPS to SSH:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[pavel@marten -=- ~/dev-c/slime_mold]$ git remote -v
origin https://github.com/pavel-perina/slime_mold.git (fetch)
origin https://github.com/pavel-perina/slime_mold.git (push)
[pavel@marten -=- ~/dev-c/slime_mold]$ git remote set-url origin [email protected]:pavel-perina/slime_mold.git
[pavel@marten -=- ~/dev-c/slime_mold]$ git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 767 bytes | 767.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:pavel-perina/slime_mold.git
21b4d31..d099423 main -> main
Confirm that SSH works
1
2
[pavel@marten -=- ~/dev-c/slime_mold]$ ssh -T [email protected]
Hi pavel-perina! You've successfully authenticated, but GitHub does not provide shell access.
Summary
That’s all. It was meant to be brief.