Use Git on Linux machines

Table of Contents

  1. Setup Git on apt-based systems ( Debian, Ubuntu )
  2. Clone a repo 
  3. Create new local branch
  4. Pull changes from remote
  5. Push changes to GitHub

Git Setup on apt-based system

  1. Install Git if you haven’t done so
sudo apt-get -y install git

2.  Generate ssh key (Please do not use SUDO in any of the following commands)

Complete all required steps instructed by the program.

Replace $your_email_on_github with your registered email address on GitHub

cd ~
ssh-keygen -t rsa -C "$your_email_on_github" -f $HOME/.ssh/id_rsa_github

The key / certificate should already been saved at $HOME/.ssh/id_rsa_github. Use the below code to verify

cat $HOME/.ssh/id_rsa_github*

3. Upload the ssh key to your GitHub account

         First execute the below command

cat $HOME/.ssh/id_rsa_github.pub

        Copy the output (all output) start with (ssh-rsa) and end with (your email address), open https://github.com/settings/ssh/new (Take you directly to GitHub add ssh key page). 

       Paste the key into the “Key” block, name it whatever you want and press “Add SSH Key”

4. Go back to the machine, open the following file at a text editor

$HOME/.ssh/config

5. Find the following code

Host github.com
  User git
  Port 22
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa

Replace ~/.ssh/id_rsa with ~/.ssh/id_rsa_github 

Save the file

Clone a repo

  1. Find a repo at GitHub
  2. Find the Green “Clone or Download” button at the right corner
  3. click on that, make sure the title said “Clone with SSH” (else click on the blue link at right that said “use ssh” to switch)
  4. Copy the link (format like git@github.com:$user-name/$repo-name.git)
  5. go back to your linux machine, choose a desired location (cd to it), then paste the command (you might need to enter your ssh key password)
sudo git clone git@github.com:$user-name/$repo-name.git

Create a new local branch

Create a local branch at your machine (replace $branch-name with your desired branch name)

sudo git branch "$branch-name'

Pull changes from the remote repo

Git pull will do a git fetch (fetch code from the repo) and git merge (merge the codes from the remote repo to your local codes)

sudo git pull

Push codes (changes) back to GitHub

            Switch to the correct directory (the directory generated / specified when you clone the repo via git)

            Use the below command to check if there’s any changes in the local branch

sudo git status

          If there’s an instruction that tell you to use “git add”, proceed. Otherwise you have made no changes to the file on this directory.

          Use the below command to commit all changes to the remote repo (branch) (Replace $comment with a actual comment)

sudo git commit -a -m "$comment"

          Push the commits to remote origin (replace $branch with the branch name on origin/github.com)

sudo git push origin $branch

         It’s pushed,  check the remote GitHub status.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.