DEV Community

Cover image for Initialize Git for a Private Repository
Sanya_Lazy
Sanya_Lazy

Posted on

Initialize Git for a Private Repository

From local to Git (Private repository)

  • First download git from official source (link)
  • Install it.
  • Now to Initialize private repository first you have to add SSH key in your local pc/laptop.

Steps to add SSH key from local to GitHub

  • Open Terminal, use default and execute this command.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" #adding git hub Email

# it ask for folder name to add in that - Add folder name
# it ask for passPhrase(password) add that

# to get public key go to created folder and open "id_rsa" with notePad, open & copy
Enter fullscreen mode Exit fullscreen mode
  • To check where it is created check in this folder
c:\Users\<yourPC>\.ssh
# example
# c:\Users\Sanya-Lazy\.ssh
Enter fullscreen mode Exit fullscreen mode
  • There you have files names as id_rsa and id_rsa.pub. open that .pub file

Add SSH Key to GitHub

  • Login to your GitHub account.
  • Go to your account settings by clicking on your profile picture in the top-right corner and select "Settings".
  • In the settings sidebar, click on "SSH and GPG keys".
  • Click on "New SSH Key" or "Add SSH Key" depending on what's available.
  • In the "Title" field, provide a descriptive label for the new key, such as "Personal laptop".
  • Set Key type to "Authentication key".
  • Paste your public key into the "key" field.
  • Click on "Add SSH key" to save the key to your GitHub account.

Easy access: Settings -> SSH and GPG keys -> Add SSH key -> Add Title(as "Laptop Name") -> Add Public key into the "Key" field -> click on "Add SSH key"
Check out this Images -

GitHub settings

GitHub SSH key

Adding Public key to GitHub SSH key

Now To add Project to Private Repository

  • Create new private repository in GitHub.
  • Now after Creating new project or with existing project, open terminal and run commands
  • To Initialize git to your project
git init
Enter fullscreen mode Exit fullscreen mode
  • Add all files to git
git add .
Enter fullscreen mode Exit fullscreen mode
  • Add first commit message
git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode
  • Add branch name
git branch -M main  # here you can use any branch name like main or master
Enter fullscreen mode Exit fullscreen mode
  • Add remote URL to your project.
git remote add origin https://github.com/<github-username>/<project-name>.git
Enter fullscreen mode Exit fullscreen mode
  • Now push to GitHub
git push -u origin main   # here use your branch name
Enter fullscreen mode Exit fullscreen mode

If above method doesn't work (use this method)

  • First check is SSH key working or not, by using this
ssh -vT git@github.com

# it asks for password, that set when creating SSH key
Enter fullscreen mode Exit fullscreen mode
  • If it shows correct then it should show GitHub username or Email
  • If GitHub username is showing then the SSH keys are working. Good
  • Now try these commands to push project to GitHub Private Repository.
  • Initialize git
git init
Enter fullscreen mode Exit fullscreen mode
  • Add files
git add .
Enter fullscreen mode Exit fullscreen mode
  • Now add commit message
git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode
  • Now before setting origin URL, first remove before added remote URL, if any
git remote remove origin
Enter fullscreen mode Exit fullscreen mode
  • Now add origin to project
git remote add origin git@github.com:<username>/<projectName>.git

# Example
# git remote add origin git@github.com:Sanya-Lazy/AwesomeProject.git
Enter fullscreen mode Exit fullscreen mode
  • Now set remote URL to project
git remote set-url origin git@github.com:<username>/<projectName>.git

# Example
# git remote set-url origin git@github.com:Sanya-Lazy/AwesomeProject.git
Enter fullscreen mode Exit fullscreen mode
  • Now push origin to GitHub
git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • To update git first you have to pull the changes and you have to push
git pull origin master
Enter fullscreen mode Exit fullscreen mode

Some Useful Git commands

  • To see remote URL
git remote -v
Enter fullscreen mode Exit fullscreen mode
  • To remove git from project
rm .git
# press "A" and hit enter
Enter fullscreen mode Exit fullscreen mode
  • If you got any erroring while adding SSH key, check for .ssh folder in Users. There you have files names as id_rsa and id_rsa.pub. open that .pub file

Happy Coding đŸ˜´ - Be Lazy

Contact DM - Twitter(X)
Contact Mail - sanya.san@myyahoo.com

Top comments (0)