The Beginner’s Guide To Git & GitHub

Objectives 

By the end of this guide, you will

  • have a working understanding of the basic Git terminology
  • be able to generate and use an SSH key to connect and authenticate to GitHub, using openSSH
  • be able to run commands to get you up and running with GitHub


Prerequisites

  •  Git

Run: sudo apt install git 

 

  •  A GitHub account

Register here 

 

  • OpenSSH

Run: sudo apt install openssh-client

 

What is Git? 

Git is an open source version control system for tracking changes to files over time so that you can revert to specific versions later if you need to. It also helps programmers to collaborate on projects and synchronize their work.

 

What is GitHub? 

GitHub is an online code hosting platform for software development projects that use git. It enables programmers to share their code files and collaborate with other developers from anywhere around the globe. 

Note: You can use Git without GitHub. 


Git Terminology

Repository

It is a directory for storing all the files and folders related to a project as well as the history of changes made to the files of the project.

A repository residing a local machine is called local repository whereas a repository that is hosted on a server is known as a remote repository.  

 

Commits

Commits are simply changes you have made since your last commit.

 

The staging environment(also known as the index)

It is an intermediate area where the files that are going to be part of the next commit are stored. For a file to be part of a commit, you must first add it to the staging environment.


Branch

A branch is a parallel version of your repository that allows you to make changes to your project without affecting the primary or master branch.

You can merge it back into the main branch when you are ready to publish changes.

 

Master

It is the repository’s primary/default branch.


Push

Push means uploading your committed changes from your local repository to a remote one (in this case GitHub).

  

Pull

Pull means downloading changes, that you or other people have made, from a remote repository to the repository on your local machine.

  

Pull request

Pull request is a way to notify the repository’s maintainers to review the commits you made to their code and, if acceptable, merge the changes into their master branch. 


Upstream branch

A local branch can be made to track a remote branch so that pushing and pulling become easier and less susceptible to mistakes. In such a case, the local branch is known as the tracking branch and the remote branch is known as the upstream branch.

 

Generating and testing an SSH key

1.  To generate key using the Ed25519 algorithm, paste the following command in your terminal:

ssh-keygen -t ed25519 -C "Enter your GitHub email address here"        

When prompted to enter a file to which to save key, press Enter to accept default file location.

You may enter a passphrase of your choice if you wish. 

Note:  Ed25519 is used as it is faster and more secure than DSA, RSA and ECDSA.  It is resilient to hash function collisions and is also strongly resistant to side-channel attacks. Moreover they provide the same level of security as the forementioned algorithms at a much shorter key length. 

 2.  Start the ssh-agent with the following command:

eval "$(ssh-agent -s)" 

ssh-agent is used to manage SSH keys and to keep track of passphrases. 

 

3.  Add SSH key to ssh-agent:

 ssh-add  ~/.ssh/id_ed25519

 

4.  Copy your SSH public key to your clipboard:   

cat ~/.ssh/id_ed25519.pub  

Copy your SSH public key content to your keyboard. Do not add any additional character, space or new line.


5.  Navigate to SSH and GPG keys on GitHub account settings.


i. Click on your profile picture on the top right corner of GitHub. Then click on Settings.

 



 

ii. Click on SSH and GPG keys.

 


6.  Click new SSH key.

7.  Enter a descriptive title for your key.

8.  Paste your public key in the key field.

9.  Click Add SSH.

10.  If prompted, enter password. 

11.  To verify your SSH key is properly authenticated to GitHub, run:

ssh -T git@github.com

 


Basic commands

1.  Create and initialize a Git repository 

mkdir myProject 

cd myProject

git init 


 

To initialize a git repository, run the git init command as shown above.

 

2.  Create a new file and check the status of your git repository

touch newFile.txt 

git status

 


git status displays the state of the working directory and the staging environment. It shows which changes have been staged and which files are not being tracked by Git.


 


3.   Add files to the staging environment



git add . 

 


git add . stages all (new, modified, deleted) files in the current directory.

git add -A stages all(new,modified, deleted) files in the entire working tree and not just the current directory.

 

4.  Configure Git username and email

git config --global user.name "Enter your name here"

git config --global user.email "Enter your email here"

 

These commands set the username and email that are associated with your Git commits.

--global specifies that the configuration is applied to the entire system and and is not specific to just that repository. If you want to use another username and email for specific projects in the future, you can run the command without the --global option when you are in those projects. 


Run git config user.name and git config user.email to view your Git username and email respectively.


5.  Create a commit

git commit -m "Your message about the commit" 

Make sure that the message is meaningful and related to the commit!

 

 

6.  Create a new branch and switch to it

git branch newBranch creates a branch called newBranch.

git checkout newBranch switches to the branch called newBranch.

 OR

git checkout -b newBranch creates a branch called newBranch and switches to it in a single command. 

  


 

git branch displays all the branches and shows which branch you are currently on.


7.  Create a new GitHub repository 

Select new repository from the + dropdown menu next to your profile picture in the top right corner.

 

 


Enter a name for your repository and click Create repository. 


8.  Push a branch to GitHub

After ensuring that SSH is selected, copy your git remote add origin command and paste it in your terminal when you are in your project folder.

 

 


git remote add origin creates an entry in your git config that specifies a name for a particular URL. In this case the name is origin and the URL is the appropriate GitHub link. However, feel free to rename the name as you wish.

git push -u origin master is used to push the master branch to the remote repository.

-u  is used to set the upstream branch.


9.  Creating a pull request

i.  Switch to the branch you created earlier if you are not already on it.

ii.  Make some changes to it and commit them.

iii.   Push the branch to remote repository.  

        git push -u origin newBranch

iv.  Open your GitHub repository and click Compare and pull request.




v.  Create pull request.


 

10.  Merge pull request 

Simply click Merge pull request.



 

 


11.  Pull changes

i.  Switch to master branch on your local repository (git checkout master).

ii.  Run git pull origin master or git pull directly if you have set the upstream branch.

 


12.  Merge two branches on your local repository

i.  Check out to the branch that you want to merge into.

ii.  Run git merge branchX ,substituting branchX with the name of the other branch.  


13.  View all commits made to a repository 

git log

Comments