How to push code to GitHub from VS Code with command line |Download Git Linux|

GitHub is run on server side which connect your directory for publicly access. GitHub also has a private directory option to restrict the global users

Note: GitHub is a version control open-source software which manages our project source code in a well-formed manner and also hosts that in GitHub server free of cost. GitHub and Git are two servers, Git runs on the user's local system, and GitHub runs on the server-side that connects your directory for public access. GitHub also has a private directory option to restrict the global users to access your private repository but it charges some amount, GitHub's private repository option is beneficial for personal or official projects.

#1. Download GitHub on Linux Ubuntu/Debian OS: 

Step 1: Git can be downloaded on Linux via command line

tommy@gilbert~$   sudo apt-get install git
[sudo] password for tommy: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
git is install successfully the newest version (2.25.1-1ubuntu3.2).
Done....

Step 2: Verify the installation by typing :

tommy@gilbert $   git --version       
git version 2.25.1

Step 3: Add username and email for authentication your GitHub, These details will be associated with any commits that you will create:

tommy@gilbert~$ git config --global user.name "Tom Thomson"
tommy@gilbert~$
tommy@gilbert~$ git config --global user.email "username@domainname.com"

#2. Download Git on Mac OS X with Homebrew:

Step 1. Open your terminal and type the following command.Step 1. Open your terminal and type the following command.

tommy@gilbert~$  brew install git

Step 2: Verify the installation by typing :

tommy@gilbert $   git --version       
git version 2.25.1

Step 3: Add username and email for authentication your GitHub, These details will be associated with any commits that you will create:

tommy@gilbert~$ git config --global user.name "Tom Thomson"
tommy@gilbert~$
tommy@gilbert~$ git config --global user.email "username@domainname.com"

#3. How to Connect Git using SSH KEY Token Authentication in Github:

Note: Recently Git Announced to not support publishing code via username and password. It must be given authentication through SSH token configurations to push and pull code in the Github repository.

Prerequisite: First setup your Email and Username for your local git account as shown above.

Step 1: First, a check should be executed to point out any SSH key Pair is already exist in your '.ssh' directory.

tommy@gilbert~$  ls -al ~/.ssh ls    # If exist, then below files will show.

id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

Tip: If you receive an error that ~/.ssh doesn't exist, you do not have an existing SSH key pair in the default location. So, you have to create a new SSH key pair in the next step.

Step 2. Generate New SSH Public Key [Optional: if key not found in first steps then then follow this 2nd step otherwise skip this.]:

There are many alogithms that you can use to generate the ssh-key. The most two common algorithms are ususally use is 'rsa 4096' and 'ed25519'. You can use any one option below:

Option A: rsa 4096

tommy@gilbert $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"    
tommy@gilbert $

Option B: ed25519

tommy@gilbert $ ssh-keygen -t ed25519 -C "your_email@example.com"     
tommy@gilbert $

Note: At the prompt, type a secure passphrase. Or just press Enter Two or Three times to set everything by default.

Step 3: Add a newly Generated SSH key to the SSH agent.

tommy@gilbert~$   eval "$(ssh-agent -s)" 
 Agent pid 59566
tommy@gilbert~$   ssh-add ~/.ssh/id_ed25519
Added successfully.

Note: If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.

Step 4: Link your public SSH key to your Github account.

tommy@gilbert~$ cat ~/.ssh/id_ed25519.pub
 ssh-ed25519 vjftd3NsetszaC1lZDI1rtAAAII8GN9lhR42h66h456stes tommy@gmail.com

Select and copy the contents of the id_ed25519.pub or id_rsa.pub file that is displayed in the terminal. Now Login into your Github account, open settings that show the top right side, then open the SSH key panel, then click "ADD SSH KEY".

Step 5. Now check to clone any repository by using the SSH link.

tommy@gilbert~$   git clone git@github.com:username/your_repo_link.git
Cloning into 'check'......
ECDSACkey fingerprint is ssh-ed25519 vjftd3NsetszaC1lZDI1rtAAAI/Retr34a/t768djj
Are you sure you want to continue connecting (yes/no)? yes
remote: Enumerating objects :  20334 done.
remote: counting objects: 100% (20334/20334), done.
remote: Compressing objects: 100% (19434/ 19434), done.
clone complete...

#4. How To Push Code from Local Repository to Github.

    Github has four commands to publishing code on GitHub Directory such as:
  1. git init
  2. git add
  3. git commit
  4. git push
  5. git pull

Step 1. git init command.

tommy@gilbert~$ git init

Note: git init command is used to initialize your working directory into git local directory for publishing it on GitHub.

Step 2. git add command.

tommy@gilbert~$  git add .       # It will Push all the files.
tommy@gilbert~$    
tommy@gilbert~$  git add  <Single_File_name.txt>     # It will Push only a single file.

Note: Add is used to add all the code into your local git directory, don't forget to add <dot> after add, see below.

Step 3. git commit command.

tommy@gilbert~$ git commit -m "this is my 1st commit"

Note: Commit command is most important to deploy your code on the GitHub server because it generates a unique commit I'd for each developer to each commit.

Step 4. git push command.

tommy@gilbert~$ git push -u origin master

Note: Git push is a final command to publish your local git code into the GitHub server for public access.

Step 5. (Optional): git remote command. [Connect your remote directory with the local git directory]

tommy@gilbert~$  git remote add origin https://github.com/user/repo.git

Note: If you are facing an Error Message like "not any repository found to push Code". That means you have forgotten to clone the Git repo. So run this Command.

Step 6. git pull command

tommy@gilbert~$  git remote add origin https://github.com/user/repo.git

Note: update a local repository with new changes commit by other person from a GitHub repository.

How can you check which repo in Github you are pushing your code? [Doc]
How do you check which Repository of Github is connected to your local git?  


tommy@gilbert~ $     git remote -v
origin github.com: tommy/my_first_project.git  (fetch)
origin  github.com: tommy/my_first_project.git   (push)

tommy@gilbert~
$     git checkout branch_name   # move one branch to a new branch



How To Push The Code From VS Code To GitHub |

What is a GitHub? How does GitHub manage source code? 

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment