DEV Community

Cover image for Git and Github for Beginners
Erasto Wamuti
Erasto Wamuti

Posted on

Git and Github for Beginners

Picture this: you are a member of a team that is working on a research paper. Each team member is given their own section to contribute to the final presentation document. This is a team project, and everyone will work on their sections at their own time, then all changes will be combined for the final paper. Does this sound familiar?
This is what software and data engineering engineers do: teamwork, version control, and collaboration on projects. But instead of using word editors, they make use of git and GitHub for project tracking and collaboration. So what is git and github?
Git is a version control tool that tracks project files, stores the histories of all the changes that a project undergoes, and keeps a log of all actions taken by all people connected to that project. This is important in big projects that teams work on for version control and future improvements.
Github on the other hand, is an online platform that enables members of a team to collect all their individual work together in one project directory and create the combined full project.

Git Installation Process

  1. Go to the Git official page and download the version that matches your computer operating system. (This use case is for Windows users).
  2. Open the setup file that was downloaded on your computer and start the installation process.
  3. Select Components: Leave the default options as selected and click on Next. Select Git Components
  4. Choosing the default editor used by Git: Select the editor you have installed on your computer from the dropdown list(Notepad, Visual Studio Code, Sublime Text, etc.). If unsure, select Notepad since it is a default Windows editor. git Editor
  5. Adjusting the name of the initial branch in new repositories: Select the second option. (change to main) Override Main
  6. Adjusting your Path environment: Select the default option selected Path Environment
  7. Choosing the SSH executable: Select the default option selected SSH Libarary
  8. Choosing HTTPS transport backend: Select the Native Windows Secure Channel Library. HTTPS Channel
  9. Configuring the line ending conversions: Leave the default option as selected(Windows Style) Line ending
  10. Configuring the terminal emulator to use Git Bash: Leave the default option as selected.(Use Mintty) Terminal emulator
  11. Choosing the default behavior of ‘git pull’: Leave the default option as selected(Fast-Forward or Merge) Default git pull
  12. Choose a credential helper: Select none Git Credential helper
  13. Configuring Extra Options: Select enable file system caching. Extra Git options
  14. Start the installation process.

Once finished, select launch Git Bash. This will open up a terminal window that resembles the Windows cmd.

Configuring Git and GitHub

Git will need to be configured so that the main user and email are saved. This is required so that Git can always record this user attached to every update posted on the project file that it tracks.
Configuration:
I. Launch Git Bash and set up your name and email by typing the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Replace the fields in quotes with your name and email.
Ii. Verify the Git installation by typing the following command on Git Bash:

git –version
Enter fullscreen mode Exit fullscreen mode

A response with a version number will appear for a completed installation. If an error appears, repeat the installation process from the setup downloaded from git main page.

Signing up for GitHub:

Get to Github and sign up for an account if you don’t have an existing one(Login if you already have an existing account). Fill out the details(email,password, username, country) and save.

Connecting Git to GitHub

As discussed earlier, GitHub is an online collaboration tool that helps teams to work on projects and store every user's contributions and history on the platform. There are more collaboration tools like GitLab and Bitbucket, but this tutorial focuses on GitHub.
You can connect to GitHub using GitBash/terminal/command line interface through HTTPS(HyperText Transfer Protocol Secure) or SSH(Secure Shell).
HTTPS method requires password authentication using access tokens every time you pass a push or pull command to a GitHub repository. You have to use a credential helper to avoid doing this for every session(extra step).
SSH, on the other hand, uses a key pair of public and private keys, and the setup is done once. A stored passphrase is used for subsequent times, hence prompting for authentication occurs only once. This makes it convenient for the many times a user performs pushes or pulls from the remote GitHub platform to their local pc.

Establishing Connection through SSH:

Step 1: Generate a pair of SSH keys using the following command on Git bash terminal.

`ssh-keygen -t rsa -b 4096 -C “your email”`

Press Enter on the terminal when prompted for file location.
Press Enter on the terminal when prompted for the passphrase(saves the key in the default location with the default passphrase)
A pair of public and private keys is successfully generated.
Enter fullscreen mode Exit fullscreen mode

Step 2: Copy the public key from the default location by typing this command on the git bash terminal.

`clip < ~/.ssh/id_rsa.pub`
Enter fullscreen mode Exit fullscreen mode

Step 3: Sign in to your GitHub account online and navigate to account settings.
Step 4: Select the ‘SSH and GPG keys’ option on the left menu.
Step 5: Select the “Add new SSH Key” option and paste the copied public key in the field provided, and name the key.

Start using Git

With these steps, Git is successfully installed and connected to GitHub. You can use git for collaboration and version control.

  • Sign in to your GitHub account and create a new repository.
  • Select SSH and copy the link to the repository.
  • On your local computer, in the Git Bash terminal, navigate to the folder you want to save your project work and create a folder. (Use the cd command, e.g., “cd Documents/Projects/NewGitProject”)
  • Run the following command to download the repository from GitHub and establish its connection to GitHub.

    `git clone <your copied repository link>`
    

The local folder is now up to date with the remote repository.

If you have a folder on your local pc and wish to create a new repository and upload it to GitHub, you can follow these steps.

  • Use the cd command to navigate on git bash terminal to the folder, then run:

    git init

  • Stage all your files in the folder to commit by running:

     `git add .`
    
  • Commit the changes and use the m flag to add a message that describes the update.

     `git commit -m "my commit message"`
    
  • Connect the local repository/directory to your remote repository on GitHub by running the command.

     `git remote add origin <replace using repository url from GitHub created>`
    
  • Push your local commits to the remote GitHub platform using;

      `git push -u origin main`
    

Every time a change is updated on GitHub by another team member, you can always perform “git pull” command on the directory on git bash terminal to download the latest version to your local pc.

These steps have helped you use basic git commands to push local updates and pull updates in the remote repository to your local pc.
There are more collaboration and version control features in git that this tutorial has not mentioned. You can explore more on this link: Git Features.

Top comments (0)