Move the existing code to Github Repository
Adam C. |

Photo by Roman Synkevych on Unsplash

Says you have been working on some code for a while, and are ready to move it to Github. Here is what Github tells you how to do after you create a repository there: 

Note: I suggest choosing SSH, so you don't need to log in every time. To set this up, please follow the official doc here

echo "# dnx-tallentAPI" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:deniapps/dnx-tallentAPI.git
git push -u origin main

Here are a few issues:

  1. Since your code is almost ready, which means you should add all files, not just README.md, therefore, you don't need line #1 and line #3.
  2. By running git init, it will not specify the Git user, so the global Git user will be used. If you have only one Git account, then it's fine, but if you have multiple users, then you may see your Github repo shows a different username that you don't want. So make sure to add the user to the git config file first.

Okay, so here is the correct way:

// First "cd" to your project root
git init
vim .git/config

Add sshCommand under [core], which tell Git which account you are going to use. You don't need this if you have only one account. To set up SSH with Github, check the official doc here.  And then, add [user] info, after that it will be like this:

[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true
 ignorecase = true
 precomposeunicode = true
 sshCommand = ssh -i ~/.ssh/id_rsa_adam
[user]
 name = Adam C.
 email = adam@deniapps.com

The next step is adding all files you would like to commit to the repo. Remember to have a correct .gitignore file created before running git add .

git add .  ### WARNING! - CHECK YOUR .gitignore to make excludes the files you don't  want to coimmit
git commit -m "first commit"
git branch -M main

Again, Instead of git remote add origin https://github.com/deniapps/dnx-tallentAPI.git, I suggest using SSH, like below, so you don't need to log in every time. To set this up, please follow the official doc here.

git remote add origin git@github.com:deniapps/dnx-talentAPI.git
git push -u origin main

Note

Updated: Apr 22nd, 2022

Try to clarify some confusion at least I had.

  1. sshCommand is an option in yours “.git/config”. You do need an SSH key if you want to clone from SSH instead of HTTPS. Git command will use the default one from “~/.ssh” folder. which is id_algorithm, (ex: id_ed25519 if you don't specify one in the “.git/config”.
  2. You CAN NOT share the SSH key among different Github accounts. If you are not using the default one, then you have to specify it in the Git command, ex: git clone git@github.com:deniapps/private.git --config core.sshCommand="ssh -i ~/.ssh/id_ed25519_adam" (Note that you should provide the private key here.)
  3. ssh-keygen command with -C (-C is just for comment)
  4. If you did not set the default user.name & user.email, you will be asked for that.
  5. If you have one at the global level, then it will be used when you don't set a user for the individual repository.
  6. If you need one user for your company repo and another one for your personal project, then set the user in the repository level.  Just in your repository folder run ‘git config user.name “USERNAME”’ & ‘git config user.email “USEREMAIL”’.