GitHub is a cloud-hosted Git management tool. Git is distributed version control, meaning the entire repository and history lives wherever you put it. People tend to use GitHub in their business or development workflow as a managed hosting solution for backups of their repositories. GitHub takes this even further by letting you connect with coworkers, friends, organizations, and more.
In this tutorial, you will learn how to take an existing project you are working on and push it so it also exists on GitHub.
Deploy your applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
To initialize the repo and push it to GitHub, you’ll need:
Sign in to GitHub and create a new empty repo. You can choose to either initialize a README or not. It doesn’t really matter because we’re just going to override everything in this remote repository anyway.
Warning: Through the rest of this tutorial, we’ll assume your GitHub username is sammy
and the repo you created is named my-new-project
. It is important that you replace these placeholders with your actual username and repo name.
From your terminal, run the following commands after navigating to the folder you would like to add.
Make sure you are in the root directory of the project you want to push to GitHub and run:
Note: If you already have an initialized Git repository, you can skip this command.
- git init
This step creates a hidden .git
directory in your project folder, which the git
software recognizes and uses to store all the metadata and version history for the project.
- git add -A
The git add
command is used to tell git which files to include in a commit, and the -A
(or --all
) argument means “include all”.
- git commit -m 'Added my project'
The git commit
command creates a new commit with all files that have been “added”. The -m
(or --message
) sets the message that will be included alongside the commit, used for future reference to understand the commit. In this case, the message is: 'Added my project'
.
- git remote add origin git@github.com:sammy/my-new-project.git
Note: Remember, you will need to replace the highlighted parts of the username and repo name with your own username and repo name.
In git, a “remote” refers to a remote version of the same repository, which is typically on a server somewhere (in this case, GitHub). “origin” is the default name git gives to a remote server (you can have multiple remotes), so git remote add origin
instructs git to add the URL of the default remote server for this repo.
- git push -u -f origin main
The -u
(or --set-upstream
) flag sets the remote origin
as the upstream reference. This allows you to later perform git push
and git pull
commands without having to specify an origin
since we always want GitHub in this case.
The -f
(or --force
) flag stands for force. This will automatically overwrite everything in the remote directory. We’re using it here to overwrite the default README that GitHub automatically initialized.
Note: If you did not include the default README when creating the project on GitHub, the -f
flag isn’t really necessary.
- git init
- git add -A
- git commit -m 'Added my project'
- git remote add origin git@github.com:sammy/my-new-project.git
- git push -u -f origin main
Now that you have your GitHub repo, it is as easy as 1-click to deploy this repo to make it live by using DigitalOcean App Platform.
Now, you are all set to track your code changes remotely in GitHub! As a next step, use the Introduction to GitHub and Open-Source Projects tutorial series to master Github.
You can use the Github Cheatsheet and keep it as a reference.
Once you start collaborating with others on the project, you’ll want to know how to create a pull request.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
とても、ためになりました。ありがとうございます!
This otherwise good tutorial leaves out security steps. Here, the github repo is used as a remote repository. An alternative is to treat it as an https site and use a PAT (Personal Access Token).
To set up a PAT, log into github and go to settings --> developer settings --> personal access tokens (classic). For permissions, I chose to click all in the repo category and all in the user category. You should probably check a good tutorial on PATs if you want to know more about the other permissions. With these two, I was able to push my project to the new github repo.
In the CLI instructions, replace
git remote add origin git@github.com:sammy/my-new-project.git
withgit remote add origin https://github.com/sammy/my-new-project.git
I ran into the following issue when working through this, which required me to set up my SSH keys between my local workstation and the github account. Since I tend to use specific SSH keys for different purposes, there is a little more work to do; I’ll show both standard (using default SSH keys) and extended (using specific SSH keys) approaches here.
NB: Do this before you attempt to Push to Github or if you get the following error:
Standard approach
This uses the default SSH keys for the user account in question. The drawback (in my opinion and scenario) is that my default SSH keys typically have passphrases which I don’t want to include for my CI/CD pipelines.
Extended approach
This uses a specific SSH key dedicated to this function; may be reused for others if needed. The advatage (in my opinion and scenario) is that it separates the keys used for standard activities and these dedicated SSH keys typically can exclude passphrases making my CI/CD smoother (IMHO).
#Final step
Copy the content of the .pub file created and add this to the GitHub settings.
That’s it, you’re all set!.
This comment has been deleted
Have a great day!
Thanks for sharing!
Thanks for the article - its a great writeup to address this common issue. For this to work for me, at least using https github repo paths, I had to add
after adding my commit message.
It says this:
The authenticity of host ‘github.com (13.234.210.38)’ 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. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.